src/java.base/share/classes/java/util/Stack.java
author jboes
Thu, 21 Nov 2019 09:10:21 +0000
changeset 59201 b24f4caa1411
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
8234335: Remove line break in class declaration in java.base Summary: Remove line break in class declarations where applicable Reviewed-by: rriggs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 1994, 2019, 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
/**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    29
 * The {@code Stack} class represents a last-in-first-out
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    30
 * (LIFO) stack of objects. It extends class {@code Vector} with five
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * operations that allow a vector to be treated as a stack. The usual
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    32
 * {@code push} and {@code pop} operations are provided, as well as a
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    33
 * method to {@code peek} at the top item on the stack, a method to test
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    34
 * for whether the stack is {@code empty}, and a method to {@code search}
2
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
 */
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 57956
diff changeset
    48
public class Stack<E> extends Vector<E> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Creates an empty Stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public Stack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Pushes an item onto the top of this stack. This has exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * the same effect as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * addElement(item)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param   item   the item to be pushed onto this stack.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    62
     * @return  the {@code item} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @see     java.util.Vector#addElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public E push(E item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        addElement(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Removes the object at the top of this stack and returns that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * object as the value of this function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    75
     * @return  The object at the top of this stack (the last item
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    76
     *          of the {@code Vector} object).
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    77
     * @throws  EmptyStackException  if this stack is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public synchronized E pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        E       obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int     len = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        obj = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        removeElementAt(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Looks at the object at the top of this stack without removing it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * from the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    93
     * @return  the object at the top of this stack (the last item
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    94
     *          of the {@code Vector} object).
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    95
     * @throws  EmptyStackException  if this stack is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public synchronized E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int     len = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new EmptyStackException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return elementAt(len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Tests if this stack is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   108
     * @return  {@code true} if and only if this stack contains
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   109
     *          no items; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public boolean empty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Returns the 1-based position where an object is on this stack.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   117
     * If the object {@code o} occurs as an item in this stack, this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * method returns the distance from the top of the stack of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * occurrence nearest the top of the stack; the topmost item on the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   120
     * stack is considered to be at distance {@code 1}. The {@code equals}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   121
     * method is used to compare {@code o} to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * items in this stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param   o   the desired object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return  the 1-based position from the top of the stack where
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   126
     *          the object is located; the return value {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *          indicates that the object is not on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public synchronized int search(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int i = lastIndexOf(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return size() - i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
   139
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static final long serialVersionUID = 1224463164541339165L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}