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.commons.collections;
018    
019    import java.util.ArrayList;
020    import java.util.EmptyStackException;
021    
022    /**
023     * An implementation of the {@link java.util.Stack} API that is based on an
024     * <code>ArrayList</code> instead of a <code>Vector</code>, so it is not
025     * synchronized to protect against multi-threaded access.  The implementation
026     * is therefore operates faster in environments where you do not need to
027     * worry about multiple thread contention.
028     * <p>
029     * The removal order of an <code>ArrayStack</code> is based on insertion 
030     * order: The most recently added element is removed first.  The iteration
031     * order is <i>not</i> the same as the removal order.  The iterator returns
032     * 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     * @see java.util.Stack
038     * @since Commons Collections 1.0
039     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
040     * 
041     * @author Craig R. McClanahan
042     * @author Paul Jack
043     * @author Stephen Colebourne
044     */
045    public class ArrayStack extends ArrayList implements Buffer {
046    
047        /** Ensure serialization compatibility */    
048        private static final long serialVersionUID = 2130079159931574599L;
049    
050        /**
051         * Constructs a new empty <code>ArrayStack</code>. The initial size
052         * is controlled by <code>ArrayList</code> and is currently 10.
053         */
054        public ArrayStack() {
055            super();
056        }
057    
058        /**
059         * Constructs a new empty <code>ArrayStack</code> with an initial size.
060         * 
061         * @param initialSize  the initial size to use
062         * @throws IllegalArgumentException  if the specified initial size
063         *  is negative
064         */
065        public ArrayStack(int initialSize) {
066            super(initialSize);
067        }
068    
069        /**
070         * Return <code>true</code> if this stack is currently empty.
071         * <p>
072         * This method exists for compatibility with <code>java.util.Stack</code>.
073         * New users of this class should use <code>isEmpty</code> instead.
074         * 
075         * @return true if the stack is currently empty
076         */
077        public boolean empty() {
078            return isEmpty();
079        }
080    
081        /**
082         * Returns the top item off of this stack without removing it.
083         *
084         * @return the top item on the stack
085         * @throws EmptyStackException  if the stack is empty
086         */
087        public Object peek() throws EmptyStackException {
088            int n = size();
089            if (n <= 0) {
090                throw new EmptyStackException();
091            } else {
092                return get(n - 1);
093            }
094        }
095    
096        /**
097         * Returns the n'th item down (zero-relative) from the top of this
098         * stack without removing it.
099         *
100         * @param n  the number of items down to go
101         * @return the n'th item on the stack, zero relative
102         * @throws EmptyStackException  if there are not enough items on the
103         *  stack to satisfy this request
104         */
105        public Object peek(int n) throws EmptyStackException {
106            int m = (size() - n) - 1;
107            if (m < 0) {
108                throw new EmptyStackException();
109            } else {
110                return get(m);
111            }
112        }
113    
114        /**
115         * Pops the top item off of this stack and return it.
116         *
117         * @return the top item on the stack
118         * @throws EmptyStackException  if the stack is empty
119         */
120        public Object pop() throws EmptyStackException {
121            int n = size();
122            if (n <= 0) {
123                throw new EmptyStackException();
124            } else {
125                return remove(n - 1);
126            }
127        }
128    
129        /**
130         * Pushes a new item onto the top of this stack. The pushed item is also
131         * returned. This is equivalent to calling <code>add</code>.
132         *
133         * @param item  the item to be added
134         * @return the item just pushed
135         */
136        public Object push(Object item) {
137            add(item);
138            return item;
139        }
140    
141        /**
142         * Returns the one-based position of the distance from the top that the
143         * specified object exists on this stack, where the top-most element is
144         * considered to be at distance <code>1</code>.  If the object is not
145         * present on the stack, return <code>-1</code> instead.  The
146         * <code>equals()</code> method is used to compare to the items
147         * in this stack.
148         *
149         * @param object  the object to be searched for
150         * @return the 1-based depth into the stack of the object, or -1 if not found
151         */
152        public int search(Object object) {
153            int i = size() - 1;        // Current index
154            int n = 1;                 // Current distance
155            while (i >= 0) {
156                Object current = get(i);
157                if ((object == null && current == null) ||
158                    (object != null && object.equals(current))) {
159                    return n;
160                }
161                i--;
162                n++;
163            }
164            return -1;
165        }
166    
167        /**
168         * Returns the element on the top of the stack.
169         *
170         * @return the element on the top of the stack
171         * @throws BufferUnderflowException  if the stack is empty
172         */
173        public Object get() {
174            int size = size();
175            if (size == 0) {
176                throw new BufferUnderflowException();
177            }
178            return get(size - 1);
179        }
180    
181        /**
182         * Removes the element on the top of the stack.
183         *
184         * @return the removed element 
185         * @throws BufferUnderflowException  if the stack is empty
186         */
187        public Object remove() {
188            int size = size();
189            if (size == 0) {
190                throw new BufferUnderflowException();
191            }
192            return remove(size - 1);
193        }
194    
195    }