Stack.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Stack.java - Class that provides a Last In First Out (LIFO)
  2. datatype, known more commonly as a Stack
  3. Copyright (C) 1998, 1999, 2001, 2004, 2005
  4. Free Software Foundation, Inc.
  5. This file is part of GNU Classpath.
  6. GNU Classpath is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10. GNU Classpath is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Classpath; see the file COPYING. If not, write to the
  16. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. 02110-1301 USA.
  18. Linking this library statically or dynamically with other modules is
  19. making a combined work based on this library. Thus, the terms and
  20. conditions of the GNU General Public License cover the whole
  21. combination.
  22. As a special exception, the copyright holders of this library give you
  23. permission to link this library with independent modules to produce an
  24. executable, regardless of the license terms of these independent
  25. modules, and to copy and distribute the resulting executable under
  26. terms of your choice, provided that you also meet, for each linked
  27. independent module, the terms and conditions of the license of that
  28. module. An independent module is a module which is not derived from
  29. or based on this library. If you modify this library, you may extend
  30. this exception to your version of the library, but you are not
  31. obligated to do so. If you do not wish to do so, delete this
  32. exception statement from your version. */
  33. package java.util;
  34. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  35. * "The Java Language Specification", ISBN 0-201-63451-1
  36. * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
  37. * Status: Believed complete and correct
  38. /**
  39. * Stack provides a Last In First Out (LIFO) data type, commonly known
  40. * as a Stack. Stack itself extends Vector and provides the additional
  41. * methods for stack manipulation (push, pop, peek). You can also seek for
  42. * the 1-based position of an element on the stack.
  43. *
  44. * @author Warren Levy (warrenl@cygnus.com)
  45. * @author Eric Blake (ebb9@email.byu.edu)
  46. * @see List
  47. * @see AbstractList
  48. * @see LinkedList
  49. * @since 1.0
  50. * @status updated to 1.4
  51. */
  52. public class Stack<T> extends Vector<T>
  53. {
  54. // We could use Vector methods internally for the following methods,
  55. // but have used Vector fields directly for efficiency (i.e. this
  56. // often reduces out duplicate bounds checking).
  57. /**
  58. * Compatible with JDK 1.0+.
  59. */
  60. private static final long serialVersionUID = 1224463164541339165L;
  61. /**
  62. * This constructor creates a new Stack, initially empty
  63. */
  64. public Stack()
  65. {
  66. }
  67. /**
  68. * Pushes an Object onto the top of the stack. This method is effectively
  69. * the same as addElement(item).
  70. *
  71. * @param item the Object to push onto the stack
  72. * @return the Object pushed onto the stack
  73. * @see Vector#addElement(Object)
  74. */
  75. public T push(T item)
  76. {
  77. // When growing the Stack, use the Vector routines in case more
  78. // memory is needed.
  79. // Note: spec indicates that this method *always* returns obj passed in!
  80. addElement(item);
  81. return item;
  82. }
  83. /**
  84. * Pops an item from the stack and returns it. The item popped is
  85. * removed from the Stack.
  86. *
  87. * @return the Object popped from the stack
  88. * @throws EmptyStackException if the stack is empty
  89. */
  90. @SuppressWarnings("unchecked")
  91. public synchronized T pop()
  92. {
  93. if (elementCount == 0)
  94. throw new EmptyStackException();
  95. modCount++;
  96. T obj = (T) elementData[--elementCount];
  97. // Set topmost element to null to assist the gc in cleanup.
  98. elementData[elementCount] = null;
  99. return obj;
  100. }
  101. /**
  102. * Returns the top Object on the stack without removing it.
  103. *
  104. * @return the top Object on the stack
  105. * @throws EmptyStackException if the stack is empty
  106. */
  107. @SuppressWarnings("unchecked")
  108. public synchronized T peek()
  109. {
  110. if (elementCount == 0)
  111. throw new EmptyStackException();
  112. return (T) elementData[elementCount - 1];
  113. }
  114. /**
  115. * Tests if the stack is empty.
  116. *
  117. * @return true if the stack contains no items, false otherwise
  118. */
  119. public synchronized boolean empty()
  120. {
  121. return elementCount == 0;
  122. }
  123. /**
  124. * Returns the position of an Object on the stack, with the top
  125. * most Object being at position 1, and each Object deeper in the
  126. * stack at depth + 1.
  127. *
  128. * @param o The object to search for
  129. * @return The 1 based depth of the Object, or -1 if the Object
  130. * is not on the stack
  131. */
  132. public synchronized int search(Object o)
  133. {
  134. int i = elementCount;
  135. while (--i >= 0)
  136. if (equals(o, elementData[i]))
  137. return elementCount - i;
  138. return -1;
  139. }
  140. }