1. /*
  2. * @(#)Stack.java 1.24 00/02/02
  3. *
  4. * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.util;
  11. /**
  12. * The <code>Stack</code> class represents a last-in-first-out
  13. * (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
  14. * operations that allow a vector to be treated as a stack. The usual
  15. * <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
  16. * method to <tt>peek</tt> at the top item on the stack, a method to test
  17. * for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
  18. * the stack for an item and discover how far it is from the top.
  19. * <p>
  20. * When a stack is first created, it contains no items.
  21. *
  22. * @author Jonathan Payne
  23. * @version 1.24, 02/02/00
  24. * @since JDK1.0
  25. */
  26. public
  27. class Stack extends Vector {
  28. /**
  29. * Creates an empty Stack.
  30. */
  31. public Stack() {
  32. }
  33. /**
  34. * Pushes an item onto the top of this stack. This has exactly
  35. * the same effect as:
  36. * <blockquote><pre>
  37. * addElement(item)</pre></blockquote>
  38. *
  39. * @param item the item to be pushed onto this stack.
  40. * @return the <code>item</code> argument.
  41. * @see java.util.Vector#addElement
  42. */
  43. public Object push(Object item) {
  44. addElement(item);
  45. return item;
  46. }
  47. /**
  48. * Removes the object at the top of this stack and returns that
  49. * object as the value of this function.
  50. *
  51. * @return The object at the top of this stack (the last item
  52. * of the <tt>Vector</tt> object).
  53. * @exception EmptyStackException if this stack is empty.
  54. */
  55. public synchronized Object pop() {
  56. Object obj;
  57. int len = size();
  58. obj = peek();
  59. removeElementAt(len - 1);
  60. return obj;
  61. }
  62. /**
  63. * Looks at the object at the top of this stack without removing it
  64. * from the stack.
  65. *
  66. * @return the object at the top of this stack (the last item
  67. * of the <tt>Vector</tt> object).
  68. * @exception EmptyStackException if this stack is empty.
  69. */
  70. public synchronized Object peek() {
  71. int len = size();
  72. if (len == 0)
  73. throw new EmptyStackException();
  74. return elementAt(len - 1);
  75. }
  76. /**
  77. * Tests if this stack is empty.
  78. *
  79. * @return <code>true</code> if and only if this stack contains
  80. * no items; <code>false</code> otherwise.
  81. */
  82. public boolean empty() {
  83. return size() == 0;
  84. }
  85. /**
  86. * Returns the 1-based position where an object is on this stack.
  87. * If the object <tt>o</tt> occurs as an item in this stack, this
  88. * method returns the distance from the top of the stack of the
  89. * occurrence nearest the top of the stack; the topmost item on the
  90. * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
  91. * method is used to compare <tt>o</tt> to the
  92. * items in this stack.
  93. *
  94. * @param o the desired object.
  95. * @return the 1-based position from the top of the stack where
  96. * the object is located; the return value <code>-1</code>
  97. * indicates that the object is not on the stack.
  98. */
  99. public synchronized int search(Object o) {
  100. int i = lastIndexOf(o);
  101. if (i >= 0) {
  102. return size() - i;
  103. }
  104. return -1;
  105. }
  106. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  107. private static final long serialVersionUID = 1224463164541339165L;
  108. }