001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.io;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.EmptyStackException;
022:
023: /**
024: * TODO document me.
025: *
026: * @author jcompagner
027: */
028: final class HandleArrayListStack extends ArrayList {
029: private static final long serialVersionUID = 1L;
030:
031: /**
032: * Construct.
033: */
034: public HandleArrayListStack() {
035: this (10);
036: }
037:
038: /**
039: * Construct.
040: *
041: * @param collection
042: * The collection to add
043: */
044: public HandleArrayListStack(final Collection collection) {
045: super (collection);
046: }
047:
048: /**
049: * Construct.
050: *
051: * @param initialCapacity
052: * Initial capacity of the stack
053: */
054: public HandleArrayListStack(final int initialCapacity) {
055: super (initialCapacity);
056: }
057:
058: /**
059: * Tests if this stack is empty.
060: *
061: * @return <code>true</code> if and only if this stack contains no items;
062: * <code>false</code> otherwise.
063: */
064: public final boolean empty() {
065: return size() == 0;
066: }
067:
068: /**
069: * @see java.util.ArrayList#indexOf(java.lang.Object)
070: */
071: public int indexOf(Object elem) {
072: int size = size();
073: if (elem == null) {
074: for (int i = 0; i < size; i++) {
075: if (get(i) == null) {
076: return i;
077: }
078: }
079: } else {
080: for (int i = 0; i < size; i++) {
081: if (elem == get(i)) {
082: return i;
083: }
084: }
085: }
086: return -1;
087: }
088:
089: /**
090: * @see java.util.ArrayList#lastIndexOf(java.lang.Object)
091: */
092: public int lastIndexOf(Object elem) {
093: if (elem == null) {
094: for (int i = size() - 1; i >= 0; i--) {
095: if (get(i) == null) {
096: return i;
097: }
098: }
099: } else {
100: for (int i = size() - 1; i >= 0; i--) {
101: if (elem == get(i)) {
102: return i;
103: }
104: }
105: }
106: return -1;
107: }
108:
109: /**
110: * Looks at the object at the top of this stack without removing it.
111: *
112: * @return The object at the top of this stack
113: * @exception EmptyStackException
114: * If this stack is empty.
115: */
116: public final Object peek() {
117: int size = size();
118: if (size == 0) {
119: throw new EmptyStackException();
120: }
121: return get(size - 1);
122: }
123:
124: /**
125: * Removes the object at the top of this stack and returns that object.
126: *
127: * @return The object at the top of this stack
128: * @exception EmptyStackException
129: * If this stack is empty.
130: */
131: public final Object pop() {
132: final Object top = peek();
133: remove(size() - 1);
134: return top;
135: }
136:
137: /**
138: * Pushes an item onto the top of this stack.
139: *
140: * @param item
141: * the item to be pushed onto this stack.
142: */
143: public final void push(final Object item) {
144: add(item);
145: }
146:
147: /**
148: * Returns the 1-based position where an object is on this stack. If the
149: * object <tt>o</tt> occurs as an item in this stack, this method returns
150: * the distance from the top of the stack of the occurrence nearest the top
151: * of the stack; the topmost item on the stack is considered to be at
152: * distance <tt>1</tt>. The <tt>equals</tt> method is used to compare
153: * <tt>o</tt> to the items in this stack.
154: *
155: * @param o
156: * the desired object.
157: * @return the 1-based position from the top of the stack where the object
158: * is located; the return value <code>-1</code> indicates that the
159: * object is not on the stack.
160: */
161: public final int search(final Object o) {
162: int i = lastIndexOf(o);
163: if (i >= 0) {
164: return size() - i;
165: }
166: return -1;
167: }
168: }
|