ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Stack.java
Revision: 1.3
Committed: Sat Oct 1 22:07:26 2005 UTC (18 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +26 -27 lines
Log Message:
minor rewording

File Contents

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