/[classpath]/classpath/java/util/Stack.java
ViewVC logotype

Diff of /classpath/java/util/Stack.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by mark, Sun Feb 18 15:39:58 2001 UTC revision 1.7 by ericb, Mon Oct 22 03:46:07 2001 UTC
# Line 32  package java.util; Line 32  package java.util;
32   * "The Java Language Specification", ISBN 0-201-63451-1   * "The Java Language Specification", ISBN 0-201-63451-1
33   * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.   * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
34   * Status:  Believed complete and correct   * Status:  Believed complete and correct
  */  
35    
36  /**  /**
37   * Stack provides a Last In First Out (LIFO) data type, commonly known   * Stack provides a Last In First Out (LIFO) data type, commonly known
38   * as a Stack.     * as a Stack.  Stack itself extends Vector and provides the additional
39   *   * methods for stack manipulation (push, pop, peek). You can also seek for
40   * Stack itself extends Vector and provides the additional methods   * the 1-based position of an element on the stack.
  * for stack manipulation (push, pop, peek).  
41   *   *
42   * @author Warren Levy <warrenl@cygnus.com>   * @author Warren Levy <warrenl@cygnus.com>
43   * @date August 20, 1998.   * @author Eric Blake <ebb9@email.byu.edu>
44     * @see List
45     * @see AbstractList
46     * @see LinkedList
47     * @since 1.0
48     * @status updated to 1.4
49   */   */
50  public class Stack extends Vector  public class Stack extends Vector
51  {  {
52    // Could use Vector methods internally for the following methods    // We could use Vector methods internally for the following methods,
53    // but have used Vector fields directly for efficiency (i.e. this    // but have used Vector fields directly for efficiency (i.e. this
54    // often reduces out duplicate bounds checking).    // often reduces out duplicate bounds checking).
55    
56      /**
57       * Compatible with JDK 1.0+.
58       */
59    private static final long serialVersionUID = 1224463164541339165L;    private static final long serialVersionUID = 1224463164541339165L;
60    
61    /**    /**
# Line 57  public class Stack extends Vector Line 63  public class Stack extends Vector
63     */     */
64    public Stack()    public Stack()
65    {    {
     super();  
66    }    }
67    
68    /**    /**
69     * Pushes an Object onto the top of the stack.  This method is effectively     * Pushes an Object onto the top of the stack.  This method is effectively
70     * the same as addElement(item)     * the same as addElement(item).
71     *     *
72     * @param item the Object to push onto the stack     * @param item the Object to push onto the stack
73     * @returns the Object pushed onto the stack     * @return the Object pushed onto the stack
74     * @see java.util.Vector#addElement(java.util.Object)     * @see Vector#addElement(Object)
75     */     */
76    public Object push(Object item)    public Object push(Object item)
77    {    {
# Line 80  public class Stack extends Vector Line 85  public class Stack extends Vector
85    
86    /**    /**
87     * Pops an item from the stack and returns it.  The item popped is     * Pops an item from the stack and returns it.  The item popped is
88     * removed from the Stack     * removed from the Stack.
89     *     *
90     * @returns the Object popped from the stack     * @return the Object popped from the stack
91       * @throws EmptyStackException if the stack is empty
92     */     */
93    public synchronized Object pop()    public synchronized Object pop()
94    {    {
95      if (elementCount == 0)      if (elementCount == 0)
96        throw new EmptyStackException();        throw new EmptyStackException();
97    
98        modCount++;
99      Object obj = elementData[--elementCount];      Object obj = elementData[--elementCount];
100    
101      // Set topmost element to null to assist the gc in cleanup      // Set topmost element to null to assist the gc in cleanup.
102      elementData[elementCount] = null;      elementData[elementCount] = null;
103      return obj;      return obj;
104    }    }
105    
106    /**    /**
107     * Returns the top Object on the stack without removing it     * Returns the top Object on the stack without removing it.
108     *     *
109     * @returns the top Object on the stack     * @return the top Object on the stack
110       * @throws EmptyStackException if the stack is empty
111     */     */
112    public synchronized Object peek()    public synchronized Object peek()
113    {    {
# Line 110  public class Stack extends Vector Line 118  public class Stack extends Vector
118    }    }
119    
120    /**    /**
121     * Tests if the stack is empty     * Tests if the stack is empty.
122     *     *
123     * @returns true if the stack contains no items, false otherwise     * @return true if the stack contains no items, false otherwise
124     */     */
125    public boolean empty()    public synchronized boolean empty()
126    {    {
127      return elementCount == 0;      return elementCount == 0;
128    }    }
# Line 122  public class Stack extends Vector Line 130  public class Stack extends Vector
130    /**    /**
131     * Returns the position of an Object on the stack, with the top     * Returns the position of an Object on the stack, with the top
132     * most Object being at position 1, and each Object deeper in the     * most Object being at position 1, and each Object deeper in the
133     * stack at depth + 1     * stack at depth + 1.
134     *     *
135     * @param o The object to search for     * @param o The object to search for
136     * @returns The 1 based depth of the Object, or -1 if the Object     * @return The 1 based depth of the Object, or -1 if the Object
137     * is not on the stack.     *         is not on the stack
138     */     */
139    public synchronized int search(Object o)    public synchronized int search(Object o)
140    {    {
141      for (int i = elementCount-1; i >=0; --i)      for (int i = elementCount - 1; i >=0; --i)
142        if (elementData[i].equals(o))        if (elementData[i].equals(o))
143          return elementCount - i;          return elementCount - i;
144    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26