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