jdk/src/java.base/share/classes/java/util/Stack.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 25859 3317bb8137f4
child 32108 aa5490a167ee
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7518
diff changeset
     2
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The <code>Stack</code> class represents a last-in-first-out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * operations that allow a vector to be treated as a stack. The usual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * method to <tt>peek</tt> at the top item on the stack, a method to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the stack for an item and discover how far it is from the top.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * When a stack is first created, it contains no items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>A more complete and consistent set of LIFO stack operations is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * provided by the {@link Deque} interface and its implementations, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * should be used in preference to this class.  For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <pre>   {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *   Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  Jonathan Payne
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 7668
diff changeset
    46
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class Stack<E> extends Vector<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Creates an empty Stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public Stack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Pushes an item onto the top of this stack. This has exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * the same effect as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * addElement(item)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param   item   the item to be pushed onto this stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @return  the <code>item</code> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @see     java.util.Vector#addElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public E push(E item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        addElement(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Removes the object at the top of this stack and returns that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * object as the value of this function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    76
     * @return  The object at the top of this stack (the last item
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    77
     *          of the <tt>Vector</tt> object).
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    78
     * @throws  EmptyStackException  if this stack is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public synchronized E pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        E       obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int     len = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        obj = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        removeElementAt(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Looks at the object at the top of this stack without removing it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * from the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    94
     * @return  the object at the top of this stack (the last item
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    95
     *          of the <tt>Vector</tt> object).
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    96
     * @throws  EmptyStackException  if this stack is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public synchronized E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int     len = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new EmptyStackException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return elementAt(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Tests if this stack is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @return  <code>true</code> if and only if this stack contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *          no items; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public boolean empty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Returns the 1-based position where an object is on this stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * If the object <tt>o</tt> occurs as an item in this stack, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * method returns the distance from the top of the stack of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * occurrence nearest the top of the stack; the topmost item on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * method is used to compare <tt>o</tt> to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * items in this stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param   o   the desired object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @return  the 1-based position from the top of the stack where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *          the object is located; the return value <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *          indicates that the object is not on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public synchronized int search(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int i = lastIndexOf(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return size() - i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static final long serialVersionUID = 1224463164541339165L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}