Source Code Cross Referenced for Stack.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1994-2005 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.util;
027
028        /**
029         * The <code>Stack</code> class represents a last-in-first-out
030         * (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
031         * operations that allow a vector to be treated as a stack. The usual
032         * <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
033         * method to <tt>peek</tt> at the top item on the stack, a method to test
034         * for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
035         * the stack for an item and discover how far it is from the top.
036         * <p>
037         * When a stack is first created, it contains no items.
038         *
039         * <p>A more complete and consistent set of LIFO stack operations is
040         * provided by the {@link Deque} interface and its implementations, which
041         * should be used in preference to this class.  For example:
042         * <pre>   {@code
043         *   Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
044         *
045         * @author  Jonathan Payne
046         * @version 1.36, 05/05/07
047         * @since   JDK1.0
048         */
049        public class Stack<E> extends Vector<E> {
050            /**
051             * Creates an empty Stack.
052             */
053            public Stack() {
054            }
055
056            /**
057             * Pushes an item onto the top of this stack. This has exactly
058             * the same effect as:
059             * <blockquote><pre>
060             * addElement(item)</pre></blockquote>
061             *
062             * @param   item   the item to be pushed onto this stack.
063             * @return  the <code>item</code> argument.
064             * @see     java.util.Vector#addElement
065             */
066            public E push(E item) {
067                addElement(item);
068
069                return item;
070            }
071
072            /**
073             * Removes the object at the top of this stack and returns that
074             * object as the value of this function.
075             *
076             * @return     The object at the top of this stack (the last item
077             *             of the <tt>Vector</tt> object).
078             * @exception  EmptyStackException  if this stack is empty.
079             */
080            public synchronized E pop() {
081                E obj;
082                int len = size();
083
084                obj = peek();
085                removeElementAt(len - 1);
086
087                return obj;
088            }
089
090            /**
091             * Looks at the object at the top of this stack without removing it
092             * from the stack.
093             *
094             * @return     the object at the top of this stack (the last item
095             *             of the <tt>Vector</tt> object).
096             * @exception  EmptyStackException  if this stack is empty.
097             */
098            public synchronized E peek() {
099                int len = size();
100
101                if (len == 0)
102                    throw new EmptyStackException();
103                return elementAt(len - 1);
104            }
105
106            /**
107             * Tests if this stack is empty.
108             *
109             * @return  <code>true</code> if and only if this stack contains
110             *          no items; <code>false</code> otherwise.
111             */
112            public boolean empty() {
113                return size() == 0;
114            }
115
116            /**
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, this
119             * method returns the distance from the top of the stack of the
120             * occurrence nearest the top of the stack; the topmost item on the
121             * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
122             * method is used to compare <tt>o</tt> to the
123             * items in this stack.
124             *
125             * @param   o   the desired object.
126             * @return  the 1-based position from the top of the stack where
127             *          the object is located; the return value <code>-1</code>
128             *          indicates that the object is not on the stack.
129             */
130            public synchronized int search(Object o) {
131                int i = lastIndexOf(o);
132
133                if (i >= 0) {
134                    return size() - i;
135                }
136                return -1;
137            }
138
139            /** use serialVersionUID from JDK 1.0.2 for interoperability */
140            private static final long serialVersionUID = 1224463164541339165L;
141        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.