1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/util/Stack.java Mon Jan 28 13:28:02 2013 +0100
1.3 @@ -0,0 +1,141 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.util;
1.30 +
1.31 +/**
1.32 + * The <code>Stack</code> class represents a last-in-first-out
1.33 + * (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
1.34 + * operations that allow a vector to be treated as a stack. The usual
1.35 + * <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
1.36 + * method to <tt>peek</tt> at the top item on the stack, a method to test
1.37 + * for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
1.38 + * the stack for an item and discover how far it is from the top.
1.39 + * <p>
1.40 + * When a stack is first created, it contains no items.
1.41 + *
1.42 + * <p>A more complete and consistent set of LIFO stack operations is
1.43 + * provided by the {@link Deque} interface and its implementations, which
1.44 + * should be used in preference to this class. For example:
1.45 + * <pre> {@code
1.46 + * Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
1.47 + *
1.48 + * @author Jonathan Payne
1.49 + * @since JDK1.0
1.50 + */
1.51 +public
1.52 +class Stack<E> extends Vector<E> {
1.53 + /**
1.54 + * Creates an empty Stack.
1.55 + */
1.56 + public Stack() {
1.57 + }
1.58 +
1.59 + /**
1.60 + * Pushes an item onto the top of this stack. This has exactly
1.61 + * the same effect as:
1.62 + * <blockquote><pre>
1.63 + * addElement(item)</pre></blockquote>
1.64 + *
1.65 + * @param item the item to be pushed onto this stack.
1.66 + * @return the <code>item</code> argument.
1.67 + * @see java.util.Vector#addElement
1.68 + */
1.69 + public E push(E item) {
1.70 + addElement(item);
1.71 +
1.72 + return item;
1.73 + }
1.74 +
1.75 + /**
1.76 + * Removes the object at the top of this stack and returns that
1.77 + * object as the value of this function.
1.78 + *
1.79 + * @return The object at the top of this stack (the last item
1.80 + * of the <tt>Vector</tt> object).
1.81 + * @throws EmptyStackException if this stack is empty.
1.82 + */
1.83 + public synchronized E pop() {
1.84 + E obj;
1.85 + int len = size();
1.86 +
1.87 + obj = peek();
1.88 + removeElementAt(len - 1);
1.89 +
1.90 + return obj;
1.91 + }
1.92 +
1.93 + /**
1.94 + * Looks at the object at the top of this stack without removing it
1.95 + * from the stack.
1.96 + *
1.97 + * @return the object at the top of this stack (the last item
1.98 + * of the <tt>Vector</tt> object).
1.99 + * @throws EmptyStackException if this stack is empty.
1.100 + */
1.101 + public synchronized E peek() {
1.102 + int len = size();
1.103 +
1.104 + if (len == 0)
1.105 + throw new EmptyStackException();
1.106 + return elementAt(len - 1);
1.107 + }
1.108 +
1.109 + /**
1.110 + * Tests if this stack is empty.
1.111 + *
1.112 + * @return <code>true</code> if and only if this stack contains
1.113 + * no items; <code>false</code> otherwise.
1.114 + */
1.115 + public boolean empty() {
1.116 + return size() == 0;
1.117 + }
1.118 +
1.119 + /**
1.120 + * Returns the 1-based position where an object is on this stack.
1.121 + * If the object <tt>o</tt> occurs as an item in this stack, this
1.122 + * method returns the distance from the top of the stack of the
1.123 + * occurrence nearest the top of the stack; the topmost item on the
1.124 + * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
1.125 + * method is used to compare <tt>o</tt> to the
1.126 + * items in this stack.
1.127 + *
1.128 + * @param o the desired object.
1.129 + * @return the 1-based position from the top of the stack where
1.130 + * the object is located; the return value <code>-1</code>
1.131 + * indicates that the object is not on the stack.
1.132 + */
1.133 + public synchronized int search(Object o) {
1.134 + int i = lastIndexOf(o);
1.135 +
1.136 + if (i >= 0) {
1.137 + return size() - i;
1.138 + }
1.139 + return -1;
1.140 + }
1.141 +
1.142 + /** use serialVersionUID from JDK 1.0.2 for interoperability */
1.143 + private static final long serialVersionUID = 1224463164541339165L;
1.144 +}