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

Java Source Code / Java Documentation
1. JDK Core
2. JDK Modules
3. JDK Modules com.sun
4. JDK Modules com.sun.java
5. JDK Modules Platform
6. JDK Modules sun
7. Open Source Build
8. Open Source Graphic Library
9. Open Source IDE Eclipse
10. Open Source J2EE
11. Open Source JDBC Driver
12. Open Source Library
13. Open Source Library Database
14. Open Source Net
15. Open Source Script
16. Science
17. Security
18. Sevlet Container
19. SUN GlassFish
20. Swing Library
21. Web Services apache cxf 2.0.1
22. Web Services AXIS2
23. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Source Code / Java Documentation » 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:        }
w___w_w.___ja___v_a___2_s__.__c__o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.