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 <em>not</em> 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 * <strong>Note:</strong> 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 * </p> 76 * 77 * @return true if the stack is currently empty 78 */ 79 public boolean empty() { 80 return isEmpty(); 81 } 82 83 /** 84 * Returns the top item off of this stack without removing it. 85 * 86 * @return the top item on the stack 87 * @throws EmptyStackException if the stack is empty 88 */ 89 public E peek() throws EmptyStackException { 90 final int n = size(); 91 if (n <= 0) { 92 throw new EmptyStackException(); 93 } 94 return get(n - 1); 95 } 96 97 /** 98 * Returns the n'th item down (zero-relative) from the top of this 99 * stack without removing it. 100 * 101 * @param n the number of items down to go 102 * @return the n'th item on the stack, zero relative 103 * @throws EmptyStackException if there are not enough items on the 104 * stack to satisfy this request 105 */ 106 public E peek(final int n) throws EmptyStackException { 107 final int m = size() - n - 1; 108 if (m < 0) { 109 throw new EmptyStackException(); 110 } 111 return get(m); 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 E pop() throws EmptyStackException { 121 final int n = size(); 122 if (n <= 0) { 123 throw new EmptyStackException(); 124 } 125 return remove(n - 1); 126 } 127 128 /** 129 * Pushes a new item onto the top of this stack. The pushed item is also 130 * returned. This is equivalent to calling {@code add}. 131 * 132 * @param item the item to be added 133 * @return the item just pushed 134 */ 135 public E push(final E item) { 136 add(item); 137 return item; 138 } 139 140 /** 141 * Returns the one-based position of the distance from the top that the 142 * specified object exists on this stack, where the top-most element is 143 * considered to be at distance {@code 1}. If the object is not 144 * present on the stack, return {@code -1} instead. The 145 * {@code equals()} method is used to compare to the items 146 * in this stack. 147 * 148 * @param object the object to be searched for 149 * @return the 1-based depth into the stack of the object, or -1 if not found 150 */ 151 public int search(final Object object) { 152 int i = size() - 1; // Current index 153 int n = 1; // Current distance 154 while (i >= 0) { 155 final Object current = get(i); 156 if (object == null && current == null || 157 object != null && object.equals(current)) { 158 return n; 159 } 160 i--; 161 n++; 162 } 163 return -1; 164 } 165 166 }