View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4;
18  
19  import java.util.ArrayList;
20  import java.util.EmptyStackException;
21  
22  /**
23   * An implementation of the {@link java.util.Stack} API that is based on an
24   * {@code ArrayList} instead of a {@code Vector}, so it is not
25   * synchronized to protect against multithreaded access.  The implementation
26   * is therefore operates faster in environments where you do not need to
27   * worry about multiple thread contention.
28   * <p>
29   * The removal order of an {@code ArrayStack} is based on insertion
30   * order: The most recently added element is removed first.  The iteration
31   * order is <i>not</i> the same as the removal order.  The iterator returns
32   * elements from the bottom up.
33   * </p>
34   * <p>
35   * Unlike {@code Stack}, {@code ArrayStack} accepts null entries.
36   * <p>
37   * <b>Note:</b> From version 4.0 onwards, this class does not implement the
38   * removed {@code Buffer} interface anymore.
39   * </p>
40   *
41   * @param <E> the type of elements in this list
42   * @see java.util.Stack
43   * @since 1.0
44   * @deprecated use {@link java.util.ArrayDeque} instead (available from Java 1.6)
45   */
46  @Deprecated
47  public class ArrayStack<E> extends ArrayList<E> {
48  
49      /** Ensure serialization compatibility */
50      private static final long serialVersionUID = 2130079159931574599L;
51  
52      /**
53       * Constructs a new empty {@code ArrayStack}. The initial size
54       * is controlled by {@code ArrayList} and is currently 10.
55       */
56      public ArrayStack() {
57      }
58  
59      /**
60       * Constructs a new empty {@code ArrayStack} with an initial size.
61       *
62       * @param initialSize  the initial size to use
63       * @throws IllegalArgumentException  if the specified initial size
64       *  is negative
65       */
66      public ArrayStack(final int initialSize) {
67          super(initialSize);
68      }
69  
70      /**
71       * Return {@code true} if this stack is currently empty.
72       * <p>
73       * This method exists for compatibility with {@link java.util.Stack}.
74       * New users of this class should use {@code isEmpty} instead.
75       *
76       * @return true if the stack is currently empty
77       */
78      public boolean empty() {
79          return isEmpty();
80      }
81  
82      /**
83       * Returns the top item off of this stack without removing it.
84       *
85       * @return the top item on the stack
86       * @throws EmptyStackException  if the stack is empty
87       */
88      public E peek() throws EmptyStackException {
89          final int n = size();
90          if (n <= 0) {
91              throw new EmptyStackException();
92          }
93          return get(n - 1);
94      }
95  
96      /**
97       * Returns the n'th item down (zero-relative) from the top of this
98       * stack without removing it.
99       *
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 }