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 */
017package org.apache.commons.collections4;
018
019import java.util.ArrayList;
020import java.util.EmptyStackException;
021
022/**
023 * An implementation of the {@link java.util.Stack} API that is based on an
024 * {@code ArrayList} instead of a {@code Vector}, so it is not
025 * synchronized to protect against multithreaded 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} 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.
033 * </p>
034 * <p>
035 * Unlike {@code Stack}, {@code ArrayStack} accepts null entries.
036 * <p>
037 * <b>Note:</b> From version 4.0 onwards, this class does not implement the
038 * removed {@code Buffer} interface anymore.
039 * </p>
040 *
041 * @param <E> the type of elements in this list
042 * @see java.util.Stack
043 * @since 1.0
044 * @deprecated use {@link java.util.ArrayDeque} instead (available from Java 1.6)
045 */
046@Deprecated
047public class ArrayStack<E> extends ArrayList<E> {
048
049    /** Ensure serialization compatibility */
050    private static final long serialVersionUID = 2130079159931574599L;
051
052    /**
053     * Constructs a new empty {@code ArrayStack}. The initial size
054     * is controlled by {@code ArrayList} and is currently 10.
055     */
056    public ArrayStack() {
057    }
058
059    /**
060     * Constructs a new empty {@code ArrayStack} with an initial size.
061     *
062     * @param initialSize  the initial size to use
063     * @throws IllegalArgumentException  if the specified initial size
064     *  is negative
065     */
066    public ArrayStack(final int initialSize) {
067        super(initialSize);
068    }
069
070    /**
071     * Return {@code true} if this stack is currently empty.
072     * <p>
073     * This method exists for compatibility with {@link java.util.Stack}.
074     * New users of this class should use {@code isEmpty} instead.
075     *
076     * @return true if the stack is currently empty
077     */
078    public boolean empty() {
079        return isEmpty();
080    }
081
082    /**
083     * Returns the top item off of this stack without removing it.
084     *
085     * @return the top item on the stack
086     * @throws EmptyStackException  if the stack is empty
087     */
088    public E peek() throws EmptyStackException {
089        final int n = size();
090        if (n <= 0) {
091            throw new EmptyStackException();
092        }
093        return get(n - 1);
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 E peek(final int n) throws EmptyStackException {
106        final int m = size() - n - 1;
107        if (m < 0) {
108            throw new EmptyStackException();
109        }
110        return get(m);
111    }
112
113    /**
114     * Pops the top item off of this stack and return it.
115     *
116     * @return the top item on the stack
117     * @throws EmptyStackException  if the stack is empty
118     */
119    public E pop() throws EmptyStackException {
120        final int n = size();
121        if (n <= 0) {
122            throw new EmptyStackException();
123        }
124        return remove(n - 1);
125    }
126
127    /**
128     * Pushes a new item onto the top of this stack. The pushed item is also
129     * returned. This is equivalent to calling {@code add}.
130     *
131     * @param item  the item to be added
132     * @return the item just pushed
133     */
134    public E push(final E item) {
135        add(item);
136        return item;
137    }
138
139    /**
140     * Returns the one-based position of the distance from the top that the
141     * specified object exists on this stack, where the top-most element is
142     * considered to be at distance {@code 1}.  If the object is not
143     * present on the stack, return {@code -1} instead.  The
144     * {@code equals()} method is used to compare to the items
145     * in this stack.
146     *
147     * @param object  the object to be searched for
148     * @return the 1-based depth into the stack of the object, or -1 if not found
149     */
150    public int search(final Object object) {
151        int i = size() - 1;        // Current index
152        int n = 1;                 // Current distance
153        while (i >= 0) {
154            final Object current = get(i);
155            if (object == null && current == null ||
156                object != null && object.equals(current)) {
157                return n;
158            }
159            i--;
160            n++;
161        }
162        return -1;
163    }
164
165}