ArrayStack.java

  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. import java.util.ArrayList;
  19. import java.util.EmptyStackException;

  20. /**
  21.  * An implementation of the {@link java.util.Stack} API that is based on an
  22.  * {@code ArrayList} instead of a {@code Vector}, so it is not
  23.  * synchronized to protect against multithreaded access.  The implementation
  24.  * is therefore operates faster in environments where you do not need to
  25.  * worry about multiple thread contention.
  26.  * <p>
  27.  * The removal order of an {@code ArrayStack} is based on insertion
  28.  * order: The most recently added element is removed first.  The iteration
  29.  * order is <em>not</em> the same as the removal order.  The iterator returns
  30.  * elements from the bottom up.
  31.  * </p>
  32.  * <p>
  33.  * Unlike {@code Stack}, {@code ArrayStack} accepts null entries.
  34.  * <p>
  35.  * <strong>Note:</strong> From version 4.0 onwards, this class does not implement the
  36.  * removed {@code Buffer} interface anymore.
  37.  * </p>
  38.  *
  39.  * @param <E> the type of elements in this list
  40.  * @see java.util.Stack
  41.  * @since 1.0
  42.  * @deprecated use {@link java.util.ArrayDeque} instead (available from Java 1.6)
  43.  */
  44. @Deprecated
  45. public class ArrayStack<E> extends ArrayList<E> {

  46.     /** Ensure serialization compatibility */
  47.     private static final long serialVersionUID = 2130079159931574599L;

  48.     /**
  49.      * Constructs a new empty {@code ArrayStack}. The initial size
  50.      * is controlled by {@code ArrayList} and is currently 10.
  51.      */
  52.     public ArrayStack() {
  53.     }

  54.     /**
  55.      * Constructs a new empty {@code ArrayStack} with an initial size.
  56.      *
  57.      * @param initialSize  the initial size to use
  58.      * @throws IllegalArgumentException  if the specified initial size
  59.      *  is negative
  60.      */
  61.     public ArrayStack(final int initialSize) {
  62.         super(initialSize);
  63.     }

  64.     /**
  65.      * Return {@code true} if this stack is currently empty.
  66.      * <p>
  67.      * This method exists for compatibility with {@link java.util.Stack}.
  68.      * New users of this class should use {@code isEmpty} instead.
  69.      * </p>
  70.      *
  71.      * @return true if the stack is currently empty
  72.      */
  73.     public boolean empty() {
  74.         return isEmpty();
  75.     }

  76.     /**
  77.      * Returns the top item off of this stack without removing it.
  78.      *
  79.      * @return the top item on the stack
  80.      * @throws EmptyStackException  if the stack is empty
  81.      */
  82.     public E peek() throws EmptyStackException {
  83.         final int n = size();
  84.         if (n <= 0) {
  85.             throw new EmptyStackException();
  86.         }
  87.         return get(n - 1);
  88.     }

  89.     /**
  90.      * Returns the n'th item down (zero-relative) from the top of this
  91.      * stack without removing it.
  92.      *
  93.      * @param n  the number of items down to go
  94.      * @return the n'th item on the stack, zero relative
  95.      * @throws EmptyStackException  if there are not enough items on the
  96.      *  stack to satisfy this request
  97.      */
  98.     public E peek(final int n) throws EmptyStackException {
  99.         final int m = size() - n - 1;
  100.         if (m < 0) {
  101.             throw new EmptyStackException();
  102.         }
  103.         return get(m);
  104.     }

  105.     /**
  106.      * Pops the top item off of this stack and return it.
  107.      *
  108.      * @return the top item on the stack
  109.      * @throws EmptyStackException  if the stack is empty
  110.      */
  111.     public E pop() throws EmptyStackException {
  112.         final int n = size();
  113.         if (n <= 0) {
  114.             throw new EmptyStackException();
  115.         }
  116.         return remove(n - 1);
  117.     }

  118.     /**
  119.      * Pushes a new item onto the top of this stack. The pushed item is also
  120.      * returned. This is equivalent to calling {@code add}.
  121.      *
  122.      * @param item  the item to be added
  123.      * @return the item just pushed
  124.      */
  125.     public E push(final E item) {
  126.         add(item);
  127.         return item;
  128.     }

  129.     /**
  130.      * Returns the one-based position of the distance from the top that the
  131.      * specified object exists on this stack, where the top-most element is
  132.      * considered to be at distance {@code 1}.  If the object is not
  133.      * present on the stack, return {@code -1} instead.  The
  134.      * {@code equals()} method is used to compare to the items
  135.      * 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(final Object object) {
  141.         int i = size() - 1;        // Current index
  142.         int n = 1;                 // Current distance
  143.         while (i >= 0) {
  144.             final 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. }