/** * Pushes a new item onto the top of this stack. The pushed item is also * returned. This is equivalent to calling <code>add</code>. * * @param item the item to be added * @return the item just pushed */ public E push(final E item) { add(item); return item; }
/** * Return <code>true</code> if this stack is currently empty. * <p> * This method exists for compatibility with <code>java.util.Stack</code>. * New users of this class should use <code>isEmpty</code> instead. * * @return true if the stack is currently empty */ public boolean empty() { return isEmpty(); }
/** * Returns the top item off of this stack without removing it. * * @return the top item on the stack * @throws EmptyStackException if the stack is empty */ public E peek() throws EmptyStackException { final int n = size(); if (n <= 0) { throw new EmptyStackException(); } else { return get(n - 1); } }
/** * Returns the n'th item down (zero-relative) from the top of this * stack without removing it. * * @param n the number of items down to go * @return the n'th item on the stack, zero relative * @throws EmptyStackException if there are not enough items on the * stack to satisfy this request */ public E peek(final int n) throws EmptyStackException { final int m = (size() - n) - 1; if (m < 0) { throw new EmptyStackException(); } else { return get(m); } }
/** * Returns the one-based position of the distance from the top that the * specified object exists on this stack, where the top-most element is * considered to be at distance <code>1</code>. If the object is not * present on the stack, return <code>-1</code> instead. The * <code>equals()</code> method is used to compare to the items * in this stack. * * @param object the object to be searched for * @return the 1-based depth into the stack of the object, or -1 if not found */ public int search(final Object object) { int i = size() - 1; // Current index int n = 1; // Current distance while (i >= 0) { final Object current = get(i); if ((object == null && current == null) || (object != null && object.equals(current))) { return n; } i--; n++; } return -1; }