1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections;
  17. import java.util.ArrayList;
  18. import java.util.EmptyStackException;
  19. /**
  20. * An implementation of the {@link java.util.Stack} API that is based on an
  21. * <code>ArrayList</code> instead of a <code>Vector</code>, so it is not
  22. * synchronized to protect against multi-threaded access. The implementation
  23. * is therefore operates faster in environments where you do not need to
  24. * worry about multiple thread contention.
  25. * <p>
  26. * The removal order of an <code>ArrayStack</code> is based on insertion
  27. * order: The most recently added element is removed first. The iteration
  28. * order is <i>not</i> the same as the removal order. The iterator returns
  29. * elements from the bottom up, whereas the {@link #remove()} method removes
  30. * them from the top down.
  31. * <p>
  32. * Unlike <code>Stack</code>, <code>ArrayStack</code> accepts null entries.
  33. * <p>
  34. * <strong>Note:</strong> this class should be bytecode-identical to the
  35. * version in commons collections. This is required to allow backwards
  36. * compability with both previous versions of BeanUtils and also allow
  37. * coexistance with both collections 2.1 and 3.0.
  38. *
  39. * @see java.util.Stack
  40. * @since Commons Collections 1.0
  41. * @version $Revision: 1.1 $ $Date: 2004/05/10 19:50:13 $
  42. *
  43. * @author Craig R. McClanahan
  44. * @author Paul Jack
  45. * @author Stephen Colebourne
  46. */
  47. public class ArrayStack extends ArrayList implements Buffer {
  48. /** Ensure serialization compatibility */
  49. private static final long serialVersionUID = 2130079159931574599L;
  50. /**
  51. * Constructs a new empty <code>ArrayStack</code>. The initial size
  52. * is controlled by <code>ArrayList</code> and is currently 10.
  53. */
  54. public ArrayStack() {
  55. super();
  56. }
  57. /**
  58. * Constructs a new empty <code>ArrayStack</code> with an initial size.
  59. *
  60. * @param initialSize the initial size to use
  61. * @throws IllegalArgumentException if the specified initial size
  62. * is negative
  63. */
  64. public ArrayStack(int initialSize) {
  65. super(initialSize);
  66. }
  67. /**
  68. * Return <code>true</code> if this stack is currently empty.
  69. * <p>
  70. * This method exists for compatibility with <code>java.util.Stack</code>.
  71. * New users of this class should use <code>isEmpty</code> instead.
  72. *
  73. * @return true if the stack is currently empty
  74. */
  75. public boolean empty() {
  76. return isEmpty();
  77. }
  78. /**
  79. * Returns the top item off of this stack without removing it.
  80. *
  81. * @return the top item on the stack
  82. * @throws EmptyStackException if the stack is empty
  83. */
  84. public Object peek() throws EmptyStackException {
  85. int n = size();
  86. if (n <= 0) {
  87. throw new EmptyStackException();
  88. } else {
  89. return get(n - 1);
  90. }
  91. }
  92. /**
  93. * Returns the n'th item down (zero-relative) from the top of this
  94. * stack without removing it.
  95. *
  96. * @param n the number of items down to go
  97. * @return the n'th item on the stack, zero relative
  98. * @throws EmptyStackException if there are not enough items on the
  99. * stack to satisfy this request
  100. */
  101. public Object peek(int n) throws EmptyStackException {
  102. int m = (size() - n) - 1;
  103. if (m < 0) {
  104. throw new EmptyStackException();
  105. } else {
  106. return get(m);
  107. }
  108. }
  109. /**
  110. * Pops the top item off of this stack and return it.
  111. *
  112. * @return the top item on the stack
  113. * @throws EmptyStackException if the stack is empty
  114. */
  115. public Object pop() throws EmptyStackException {
  116. int n = size();
  117. if (n <= 0) {
  118. throw new EmptyStackException();
  119. } else {
  120. return remove(n - 1);
  121. }
  122. }
  123. /**
  124. * Pushes a new item onto the top of this stack. The pushed item is also
  125. * returned. This is equivalent to calling <code>add</code>.
  126. *
  127. * @param item the item to be added
  128. * @return the item just pushed
  129. */
  130. public Object push(Object item) {
  131. add(item);
  132. return item;
  133. }
  134. /**
  135. * Returns the one-based position of the distance from the top that the
  136. * specified object exists on this stack, where the top-most element is
  137. * considered to be at distance <code>1</code>. If the object is not
  138. * present on the stack, return <code>-1</code> instead. The
  139. * <code>equals()</code> method is used to compare to the items
  140. * in this stack.
  141. *
  142. * @param object the object to be searched for
  143. * @return the 1-based depth into the stack of the object, or -1 if not found
  144. */
  145. public int search(Object object) {
  146. int i = size() - 1; // Current index
  147. int n = 1; // Current distance
  148. while (i >= 0) {
  149. Object current = get(i);
  150. if ((object == null && current == null) ||
  151. (object != null && object.equals(current))) {
  152. return n;
  153. }
  154. i--;
  155. n++;
  156. }
  157. return -1;
  158. }
  159. /**
  160. * Returns the element on the top of the stack.
  161. *
  162. * @return the element on the top of the stack
  163. * @throws BufferUnderflowException if the stack is empty
  164. */
  165. public Object get() {
  166. int size = size();
  167. if (size == 0) {
  168. throw new BufferUnderflowException();
  169. }
  170. return get(size - 1);
  171. }
  172. /**
  173. * Removes the element on the top of the stack.
  174. *
  175. * @return the removed element
  176. * @throws BufferUnderflowException if the stack is empty
  177. */
  178. public Object remove() {
  179. int size = size();
  180. if (size == 0) {
  181. throw new BufferUnderflowException();
  182. }
  183. return remove(size - 1);
  184. }
  185. }