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.collections;
    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</code> instead of a <code>Vector</code>, so it is not
    25   * synchronized to protect against multi-threaded 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</code> 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, whereas the {@link #remove()} method removes
    33   * them from the top down.
    34   * <p>
    35   * Unlike <code>Stack</code>, <code>ArrayStack</code> accepts null entries.
    36   * <p>
    37   * <strong>Note:</strong> this class should be bytecode-identical to the
    38   * version in commons collections. This is required to allow backwards
    39   * compability with both previous versions of BeanUtils and also allow
    40   * coexistance with both collections 2.1 and 3.0.
    41   *
    42   * @see java.util.Stack
    43   * @since Commons Collections 1.0
    44   * @version $Revision: 555824 $ $Date: 2007-07-13 01:27:15 +0100 (Fri, 13 Jul 2007) $
    45   *
    46   * @author Craig R. McClanahan
    47   * @author Paul Jack
    48   * @author Stephen Colebourne
    49   */
    50  public class ArrayStack extends ArrayList implements Buffer {
    51
    52      /*** Ensure serialization compatibility */
    53      private static final long serialVersionUID = 2130079159931574599L;
    54
    55      /***
    56       * Constructs a new empty <code>ArrayStack</code>. The initial size
    57       * is controlled by <code>ArrayList</code> and is currently 10.
    58       */
    59      public ArrayStack() {
    60          super();
    61      }
    62
    63      /***
    64       * Constructs a new empty <code>ArrayStack</code> with an initial size.
    65       *
    66       * @param initialSize  the initial size to use
    67       * @throws IllegalArgumentException  if the specified initial size
    68       *  is negative
    69       */
    70      public ArrayStack(int initialSize) {
    71          super(initialSize);
    72      }
    73
    74      /***
    75       * Return <code>true</code> if this stack is currently empty.
    76       * <p>
    77       * This method exists for compatibility with <code>java.util.Stack</code>.
    78       * New users of this class should use <code>isEmpty</code> instead.
    79       *
    80       * @return true if the stack is currently empty
    81       */
    82      public boolean empty() {
    83          return isEmpty();
    84      }
    85
    86      /***
    87       * Returns the top item off of this stack without removing it.
    88       *
    89       * @return the top item on the stack
    90       * @throws EmptyStackException  if the stack is empty
    91       */
    92      public Object peek() throws EmptyStackException {
    93          int n = size();
    94          if (n <= 0) {
    95              throw new EmptyStackException();
    96          } else {
    97              return get(n - 1);
    98          }
    99      }
    100
    101     /***
    102      * Returns the n'th item down (zero-relative) from the top of this
    103      * stack without removing it.
    104      *
    105      * @param n  the number of items down to go
    106      * @return the n'th item on the stack, zero relative
    107      * @throws EmptyStackException  if there are not enough items on the
    108      *  stack to satisfy this request
    109      */
    110     public Object peek(int n) throws EmptyStackException {
    111         int m = (size() - n) - 1;
    112         if (m < 0) {
    113             throw new EmptyStackException();
    114         } else {
    115             return get(m);
    116         }
    117     }
    118
    119     /***
    120      * Pops the top item off of this stack and return it.
    121      *
    122      * @return the top item on the stack
    123      * @throws EmptyStackException  if the stack is empty
    124      */
    125     public Object pop() throws EmptyStackException {
    126         int n = size();
    127         if (n <= 0) {
    128             throw new EmptyStackException();
    129         } else {
    130             return remove(n - 1);
    131         }
    132     }
    133
    134     /***
    135      * Pushes a new item onto the top of this stack. The pushed item is also
    136      * returned. This is equivalent to calling <code>add</code>.
    137      *
    138      * @param item  the item to be added
    139      * @return the item just pushed
    140      */
    141     public Object push(Object item) {
    142         add(item);
    143         return item;
    144     }
    145
    146     /***
    147      * Returns the one-based position of the distance from the top that the
    148      * specified object exists on this stack, where the top-most element is
    149      * considered to be at distance <code>1</code>.  If the object is not
    150      * present on the stack, return <code>-1</code> instead.  The
    151      * <code>equals()</code> method is used to compare to the items
    152      * in this stack.
    153      *
    154      * @param object  the object to be searched for
    155      * @return the 1-based depth into the stack of the object, or -1 if not found
    156      */
    157     public int search(Object object) {
    158         int i = size() - 1;        // Current index
    159         int n = 1;                 // Current distance
    160         while (i >= 0) {
    161             Object current = get(i);
    162             if ((object == null && current == null) ||
    163                 (object != null && object.equals(current))) {
    164                 return n;
    165             }
    166             i--;
    167             n++;
    168         }
    169         return -1;
    170     }
    171
    172     /***
    173      * Returns the element on the top of the stack.
    174      *
    175      * @return the element on the top of the stack
    176      * @throws BufferUnderflowException  if the stack is empty
    177      */
    178     public Object get() {
    179         int size = size();
    180         if (size == 0) {
    181             throw new BufferUnderflowException();
    182         }
    183         return get(size - 1);
    184     }
    185
    186     /***
    187      * Removes the element on the top of the stack.
    188      *
    189      * @return the removed element
    190      * @throws BufferUnderflowException  if the stack is empty
    191      */
    192     public Object remove() {
    193         int size = size();
    194         if (size == 0) {
    195             throw new BufferUnderflowException();
    196         }
    197         return remove(size - 1);
    198     }
    199
    200 }