KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > collection > FastStack


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 //NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * Filename: FastStack.java
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42  
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/collection/FastStack.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:11 $
47  */

48  
49 package com.sun.enterprise.util.collection;
50
51 import java.util.ArrayList JavaDoc;
52
53
54 /**
55 * The Stack class represents a last-in-first-out (LIFO) stack of objects. The implementation is backed by
56 * java.util.ArrayList. Unlike java.util.Stack, it does not extend class Vector and hence
57 * all methods in the stack are unsynchronized. The usual push and pop operations are provided, as
58 * well as a method to peek at the top item on the stack, a method to test for whether the stack is empty.
59 * When a stack is first created, it contains no items.
60 */

61 public class FastStack {
62     
63     private ArrayList JavaDoc stack;
64     
65     /**
66     * Create a stack with no elements in it.
67     */

68     public FastStack() {
69         stack = new ArrayList JavaDoc(4);
70     }
71     
72     /**
73     * Tests if this stack is empty.
74     * @return true if and only if this stack contains no items; false otherwise.
75     */

76     public boolean empty() {
77         return (stack.size() == 0);
78     }
79     
80     /**
81     * Tests if this stack is empty.
82     * @return true if and only if this stack contains no items; false otherwise.
83     */

84     public boolean isEmpty() {
85         return (stack.size() == 0);
86     }
87     
88     /**
89     * Looks at the object at the top of this stack without removing it from the stack.
90     * @return the object at the top of this stack (the last item of the Vector object).
91     * @exception java.util.EmptyStackException if the stack is empty.
92     */

93     public Object JavaDoc peek() {
94         if (stack.size() == 0)
95             throw new java.util.EmptyStackException JavaDoc();
96         return stack.get(stack.size()-1);
97     }
98     
99     /**
100     * Pushes the object at the top of this stack.
101     * @param object the item to be pushed into the stack.
102     */

103     public void push(Object JavaDoc object) {
104         stack.add(object);
105     }
106     
107     /**
108     * Pops the object that is at the top of this stack.
109     * @return The object at the top of the stack
110     * @exception java.util.EmptyStackException if the stack is empty.
111     */

112     public Object JavaDoc pop() {
113         if (stack.size() == 0)
114             throw new java.util.EmptyStackException JavaDoc();
115         return stack.remove(stack.size()-1);
116     }
117     
118     /**
119     * Gets the current size of the stack.
120     * @return The number of entries in the stack.
121     */

122     public int size() {
123         return stack.size();
124     }
125         
126 }
Popular Tags