rt/emul/compact/src/main/java/java/util/Stack.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 597 emul/compact/src/main/java/java/util/Stack.java@ee8a922f4268
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@597
     1
/*
jaroslav@597
     2
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@597
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@597
     4
 *
jaroslav@597
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@597
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@597
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@597
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@597
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@597
    10
 *
jaroslav@597
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@597
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@597
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@597
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@597
    15
 * accompanied this code).
jaroslav@597
    16
 *
jaroslav@597
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@597
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@597
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@597
    20
 *
jaroslav@597
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@597
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@597
    23
 * questions.
jaroslav@597
    24
 */
jaroslav@597
    25
jaroslav@597
    26
package java.util;
jaroslav@597
    27
jaroslav@597
    28
/**
jaroslav@597
    29
 * The <code>Stack</code> class represents a last-in-first-out
jaroslav@597
    30
 * (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
jaroslav@597
    31
 * operations that allow a vector to be treated as a stack. The usual
jaroslav@597
    32
 * <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
jaroslav@597
    33
 * method to <tt>peek</tt> at the top item on the stack, a method to test
jaroslav@597
    34
 * for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
jaroslav@597
    35
 * the stack for an item and discover how far it is from the top.
jaroslav@597
    36
 * <p>
jaroslav@597
    37
 * When a stack is first created, it contains no items.
jaroslav@597
    38
 *
jaroslav@597
    39
 * <p>A more complete and consistent set of LIFO stack operations is
jaroslav@597
    40
 * provided by the {@link Deque} interface and its implementations, which
jaroslav@597
    41
 * should be used in preference to this class.  For example:
jaroslav@597
    42
 * <pre>   {@code
jaroslav@597
    43
 *   Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
jaroslav@597
    44
 *
jaroslav@597
    45
 * @author  Jonathan Payne
jaroslav@597
    46
 * @since   JDK1.0
jaroslav@597
    47
 */
jaroslav@597
    48
public
jaroslav@597
    49
class Stack<E> extends Vector<E> {
jaroslav@597
    50
    /**
jaroslav@597
    51
     * Creates an empty Stack.
jaroslav@597
    52
     */
jaroslav@597
    53
    public Stack() {
jaroslav@597
    54
    }
jaroslav@597
    55
jaroslav@597
    56
    /**
jaroslav@597
    57
     * Pushes an item onto the top of this stack. This has exactly
jaroslav@597
    58
     * the same effect as:
jaroslav@597
    59
     * <blockquote><pre>
jaroslav@597
    60
     * addElement(item)</pre></blockquote>
jaroslav@597
    61
     *
jaroslav@597
    62
     * @param   item   the item to be pushed onto this stack.
jaroslav@597
    63
     * @return  the <code>item</code> argument.
jaroslav@597
    64
     * @see     java.util.Vector#addElement
jaroslav@597
    65
     */
jaroslav@597
    66
    public E push(E item) {
jaroslav@597
    67
        addElement(item);
jaroslav@597
    68
jaroslav@597
    69
        return item;
jaroslav@597
    70
    }
jaroslav@597
    71
jaroslav@597
    72
    /**
jaroslav@597
    73
     * Removes the object at the top of this stack and returns that
jaroslav@597
    74
     * object as the value of this function.
jaroslav@597
    75
     *
jaroslav@597
    76
     * @return  The object at the top of this stack (the last item
jaroslav@597
    77
     *          of the <tt>Vector</tt> object).
jaroslav@597
    78
     * @throws  EmptyStackException  if this stack is empty.
jaroslav@597
    79
     */
jaroslav@597
    80
    public synchronized E pop() {
jaroslav@597
    81
        E       obj;
jaroslav@597
    82
        int     len = size();
jaroslav@597
    83
jaroslav@597
    84
        obj = peek();
jaroslav@597
    85
        removeElementAt(len - 1);
jaroslav@597
    86
jaroslav@597
    87
        return obj;
jaroslav@597
    88
    }
jaroslav@597
    89
jaroslav@597
    90
    /**
jaroslav@597
    91
     * Looks at the object at the top of this stack without removing it
jaroslav@597
    92
     * from the stack.
jaroslav@597
    93
     *
jaroslav@597
    94
     * @return  the object at the top of this stack (the last item
jaroslav@597
    95
     *          of the <tt>Vector</tt> object).
jaroslav@597
    96
     * @throws  EmptyStackException  if this stack is empty.
jaroslav@597
    97
     */
jaroslav@597
    98
    public synchronized E peek() {
jaroslav@597
    99
        int     len = size();
jaroslav@597
   100
jaroslav@597
   101
        if (len == 0)
jaroslav@597
   102
            throw new EmptyStackException();
jaroslav@597
   103
        return elementAt(len - 1);
jaroslav@597
   104
    }
jaroslav@597
   105
jaroslav@597
   106
    /**
jaroslav@597
   107
     * Tests if this stack is empty.
jaroslav@597
   108
     *
jaroslav@597
   109
     * @return  <code>true</code> if and only if this stack contains
jaroslav@597
   110
     *          no items; <code>false</code> otherwise.
jaroslav@597
   111
     */
jaroslav@597
   112
    public boolean empty() {
jaroslav@597
   113
        return size() == 0;
jaroslav@597
   114
    }
jaroslav@597
   115
jaroslav@597
   116
    /**
jaroslav@597
   117
     * Returns the 1-based position where an object is on this stack.
jaroslav@597
   118
     * If the object <tt>o</tt> occurs as an item in this stack, this
jaroslav@597
   119
     * method returns the distance from the top of the stack of the
jaroslav@597
   120
     * occurrence nearest the top of the stack; the topmost item on the
jaroslav@597
   121
     * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
jaroslav@597
   122
     * method is used to compare <tt>o</tt> to the
jaroslav@597
   123
     * items in this stack.
jaroslav@597
   124
     *
jaroslav@597
   125
     * @param   o   the desired object.
jaroslav@597
   126
     * @return  the 1-based position from the top of the stack where
jaroslav@597
   127
     *          the object is located; the return value <code>-1</code>
jaroslav@597
   128
     *          indicates that the object is not on the stack.
jaroslav@597
   129
     */
jaroslav@597
   130
    public synchronized int search(Object o) {
jaroslav@597
   131
        int i = lastIndexOf(o);
jaroslav@597
   132
jaroslav@597
   133
        if (i >= 0) {
jaroslav@597
   134
            return size() - i;
jaroslav@597
   135
        }
jaroslav@597
   136
        return -1;
jaroslav@597
   137
    }
jaroslav@597
   138
jaroslav@597
   139
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
jaroslav@597
   140
    private static final long serialVersionUID = 1224463164541339165L;
jaroslav@597
   141
}