001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of 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,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.util;
020:
021: import java.util.ArrayList;
022: import java.util.EmptyStackException;
023:
024: /**
025: * An implementation of the {@link java.util.Stack} API that is based on an <code>ArrayList</code>
026: * instead of a <code>Vector</code>, so it is not synchronized to protect against multi-threaded
027: * access. The implementation is therefore operates faster in environments where you do not need to
028: * worry about multiple thread contention.
029: * <p/>
030: * The removal order of an <code>ArrayStack</code> is based on insertion order: The most recently
031: * added element is removed first. The iteration order is <i>not</i> the same as the removal order.
032: * The iterator returns elements from the bottom up, whereas the {@link #remove()} method removes
033: * them from the top down.
034: * <p/>
035: * Unlike <code>Stack</code>, <code>ArrayStack</code> accepts null entries.
036: */
037: public class ArrayStack extends ArrayList {
038:
039: /** Ensure serialization compatibility */
040: private static final long serialVersionUID = 2130079159931574599L;
041:
042: /**
043: * Constructs a new empty <code>ArrayStack</code>. The initial size is controlled by
044: * <code>ArrayList</code> and is currently 10.
045: */
046: public ArrayStack() {
047: super ();
048: }
049:
050: /**
051: * Constructs a new empty <code>ArrayStack</code> with an initial size.
052: *
053: * @param initialSize the initial size to use
054: * @throws IllegalArgumentException if the specified initial size is negative
055: */
056: public ArrayStack(int initialSize) {
057: super (initialSize);
058: }
059:
060: /**
061: * Return <code>true</code> if this stack is currently empty.
062: * <p/>
063: * This method exists for compatibility with <code>java.util.Stack</code>. New users of this
064: * class should use <code>isEmpty</code> instead.
065: *
066: * @return true if the stack is currently empty
067: */
068: public boolean empty() {
069: return isEmpty();
070: }
071:
072: /**
073: * Returns the top item off of this stack without removing it.
074: *
075: * @return the top item on the stack
076: * @throws EmptyStackException if the stack is empty
077: */
078: public Object peek() throws EmptyStackException {
079: int n = size();
080: if (n <= 0) {
081: throw new EmptyStackException();
082: } else {
083: return get(n - 1);
084: }
085: }
086:
087: /**
088: * Returns the n'th item down (zero-relative) from the top of this stack without removing it.
089: *
090: * @param n the number of items down to go
091: * @return the n'th item on the stack, zero relative
092: * @throws EmptyStackException if there are not enough items on the stack to satisfy this
093: * request
094: */
095: public Object peek(int n) throws EmptyStackException {
096: int m = (size() - n) - 1;
097: if (m < 0) {
098: throw new EmptyStackException();
099: } else {
100: return get(m);
101: }
102: }
103:
104: /**
105: * Pops the top item off of this stack and return it.
106: *
107: * @return the top item on the stack
108: * @throws EmptyStackException if the stack is empty
109: */
110: public Object pop() throws EmptyStackException {
111: int n = size();
112: if (n <= 0) {
113: throw new EmptyStackException();
114: } else {
115: return remove(n - 1);
116: }
117: }
118:
119: /**
120: * Pushes a new item onto the top of this stack. The pushed item is also returned. This is
121: * equivalent to calling <code>add</code>.
122: *
123: * @param item the item to be added
124: * @return the item just pushed
125: */
126: public Object push(Object item) {
127: add(item);
128: return item;
129: }
130:
131: /**
132: * Returns the one-based position of the distance from the top that the specified object exists
133: * on this stack, where the top-most element is considered to be at distance <code>1</code>. If
134: * the object is not present on the stack, return <code>-1</code> instead. The
135: * <code>equals()</code> method is used to compare to the items in this stack.
136: *
137: * @param object the object to be searched for
138: * @return the 1-based depth into the stack of the object, or -1 if not found
139: */
140: public int search(Object object) {
141: int i = size() - 1; // Current index
142: int n = 1; // Current distance
143: while (i >= 0) {
144: Object current = get(i);
145: if ((object == null && current == null)
146: || (object != null && object.equals(current))) {
147: return n;
148: }
149: i--;
150: n++;
151: }
152: return -1;
153: }
154:
155: /**
156: * Returns the element on the top of the stack.
157: *
158: * @return the element on the top of the stack
159: * @throws EmptyStackException if the stack is empty
160: */
161: public Object get() {
162: int size = size();
163: if (size == 0) {
164: throw new EmptyStackException();
165: }
166: return get(size - 1);
167: }
168:
169: /**
170: * Removes the element on the top of the stack.
171: *
172: * @return the removed element
173: * @throws EmptyStackException if the stack is empty
174: */
175: public Object remove() {
176: int size = size();
177: if (size == 0) {
178: throw new EmptyStackException();
179: }
180: return remove(size - 1);
181: }
182:
183: }
|