1/*2 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4 *5 * This code is free software; you can redistribute it and/or modify it6 * under the terms of the GNU General Public License version 2 only, as7 * published by the Free Software Foundation. Oracle designates this8 * particular file as subject to the "Classpath" exception as provided9 * by Oracle in the LICENSE file that accompanied this code.10 *11 * This code is distributed in the hope that it will be useful, but WITHOUT12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14 * version 2 for more details (a copy is included in the LICENSE file that15 * accompanied this code).16 *17 * You should have received a copy of the GNU General Public License version18 * 2 along with this work; if not, write to the Free Software Foundation,19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.20 *21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA22 * or visit www.oracle.com if you need additional information or have any23 * questions.24 */2526package java.util;
2728/**29 * The <code>Stack</code> class represents a last-in-first-out30 * (LIFO) stack of objects. It extends class <tt>Vector</tt> with five31 * operations that allow a vector to be treated as a stack. The usual32 * <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a33 * method to <tt>peek</tt> at the top item on the stack, a method to test34 * for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>35 * the stack for an item and discover how far it is from the top.36 * <p>37 * When a stack is first created, it contains no items.38 *39 * <p>A more complete and consistent set of LIFO stack operations is40 * provided by the {@link Deque} interface and its implementations, which41 * should be used in preference to this class. For example:42 * <pre> {@code43 * Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>44 *45 * @author Jonathan Payne46 * @since JDK1.047 */48public49class Stack<E> extends Vector<E> {
50/**51 * Creates an empty Stack.52 */53publicStack() {
54 }
5556/**57 * Pushes an item onto the top of this stack. This has exactly58 * the same effect as:59 * <blockquote><pre>60 * addElement(item)</pre></blockquote>61 *62 * @param item the item to be pushed onto this stack.63 * @return the <code>item</code> argument.64 * @see java.util.Vector#addElement65 */66public E push(E item) {
67 addElement(item);
6869return item;
70 }
7172/**73 * Removes the object at the top of this stack and returns that74 * object as the value of this function.75 *76 * @return The object at the top of this stack (the last item77 * of the <tt>Vector</tt> object).78 * @throws EmptyStackException if this stack is empty.79 */80publicsynchronized E pop() {
81 E obj;
82int len = size();
8384 obj = peek();
85 removeElementAt(len - 1);
8687return obj;
88 }
8990/**91 * Looks at the object at the top of this stack without removing it92 * from the stack.93 *94 * @return the object at the top of this stack (the last item95 * of the <tt>Vector</tt> object).96 * @throws EmptyStackException if this stack is empty.97 */98publicsynchronized E peek() {
99int len = size();
100101if (len == 0)
102thrownewEmptyStackException();
103return elementAt(len - 1);
104 }
105106/**107 * Tests if this stack is empty.108 *109 * @return <code>true</code> if and only if this stack contains110 * no items; <code>false</code> otherwise.111 */112publicboolean empty() {
113return size() == 0;
114 }
115116/**117 * Returns the 1-based position where an object is on this stack.118 * If the object <tt>o</tt> occurs as an item in this stack, this119 * method returns the distance from the top of the stack of the120 * occurrence nearest the top of the stack; the topmost item on the121 * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>122 * method is used to compare <tt>o</tt> to the123 * items in this stack.124 *125 * @param o the desired object.126 * @return the 1-based position from the top of the stack where127 * the object is located; the return value <code>-1</code>128 * indicates that the object is not on the stack.129 */130publicsynchronizedint search(Object o) {
131int i = lastIndexOf(o);
132133if (i >= 0) {
134return size() - i;
135 }
136return -1;
137 }
138139/** use serialVersionUID from JDK 1.0.2 for interoperability */140privatestaticfinallong serialVersionUID = 1224463164541339165L;
141 }