001: /*
002: * $Id: ArrayListStack.java 459319 2006-02-14 20:54:57Z jonl $
003: * $Revision: 459319 $ $Date: 2006-02-14 21:54:57 +0100 (Tue, 14 Feb 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.util.collections;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.EmptyStackException;
023:
024: /**
025: * A faster, smaller stack implementation. ArrayListStack is final and
026: * unsynchronized (the JDK's methods are synchronized). In addition you can set
027: * the initial capacity if you want via the ArrayListStack(int) constructor.
028: *
029: * @author Jonathan Locke
030: */
031: public final class ArrayListStack extends ArrayList {
032: private static final long serialVersionUID = 1L;
033:
034: /**
035: * Construct.
036: *
037: * @param initialCapacity
038: * Initial capacity of the stack
039: */
040: public ArrayListStack(final int initialCapacity) {
041: super (initialCapacity);
042: }
043:
044: /**
045: * Construct.
046: */
047: public ArrayListStack() {
048: this (10);
049: }
050:
051: /**
052: * Construct.
053: *
054: * @param collection
055: * The collection to add
056: */
057: public ArrayListStack(final Collection collection) {
058: super (collection);
059: }
060:
061: /**
062: * Pushes an item onto the top of this stack.
063: *
064: * @param item
065: * the item to be pushed onto this stack.
066: */
067: public final void push(final Object item) {
068: add(item);
069: }
070:
071: /**
072: * Removes the object at the top of this stack and returns that object.
073: *
074: * @return The object at the top of this stack
075: * @exception EmptyStackException
076: * If this stack is empty.
077: */
078: public final Object pop() {
079: final Object top = peek();
080: remove(size() - 1);
081: return top;
082: }
083:
084: /**
085: * Looks at the object at the top of this stack without removing it.
086: *
087: * @return The object at the top of this stack
088: * @exception EmptyStackException
089: * If this stack is empty.
090: */
091: public final Object peek() {
092: int size = size();
093: if (size == 0) {
094: throw new EmptyStackException();
095: }
096: return get(size - 1);
097: }
098:
099: /**
100: * Tests if this stack is empty.
101: *
102: * @return <code>true</code> if and only if this stack contains no items;
103: * <code>false</code> otherwise.
104: */
105: public final boolean empty() {
106: return size() == 0;
107: }
108:
109: /**
110: * Returns the 1-based position where an object is on this stack. If the
111: * object <tt>o</tt> occurs as an item in this stack, this method returns
112: * the distance from the top of the stack of the occurrence nearest the top
113: * of the stack; the topmost item on the stack is considered to be at
114: * distance <tt>1</tt>. The <tt>equals</tt> method is used to compare
115: * <tt>o</tt> to the items in this stack.
116: *
117: * @param o
118: * the desired object.
119: * @return the 1-based position from the top of the stack where the object
120: * is located; the return value <code>-1</code> indicates that the
121: * object is not on the stack.
122: */
123: public final int search(final Object o) {
124: int i = lastIndexOf(o);
125: if (i >= 0) {
126: return size() - i;
127: }
128: return -1;
129: }
130: }
|