KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
25  * ArrayListStack.java
26  * Author: darpan.dinker@sun.com
27  * Created on June 21, 2002, 3:42 PM
28  *
29  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
30  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
31  * All rights reserved.
32  *
33  * This software is the confidential and proprietary information
34  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
35  * You shall not disclose such Confidential Information and shall
36  * use it only in accordance with the terms of the license
37  * agreement you entered into with iPlanet/Sun Microsystems.
38  */

39
40 package com.sun.enterprise.util.collection;
41
42 import java.util.ArrayList JavaDoc;
43
44 /**
45  * The <code>ArrayListStack</code> class represents a last-in-first-out
46  * (LIFO) stack of objects. It encapsulates class <tt>ArrayList</tt> with four
47  * operations that allow a list to be treated as a stack. The usual
48  * <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
49  * method to <tt>peek</tt> at the top item on the stack, and a method to test
50  * for whether the stack is <tt>empty</tt>
51  * <p>
52  * When a stack is first created, it contains no items.
53  * @author Darpan Dinker, $Author: tcfujii $
54  * @version $Revision: 1.3 $ on $Date: 2005/12/25 04:12:09 $
55  */

56 public class ArrayListStack {
57     private int curIndex;
58     private ArrayList JavaDoc list;
59     
60     /** Creates a stack with the given initial size */
61     public ArrayListStack(int size) {
62         curIndex = 0;
63         list = new ArrayList JavaDoc(size);
64     }
65     
66     /** Creates a stack with a default size */
67     public ArrayListStack() {
68         this(20);
69     }
70     
71     /**
72      * Provides the current size of the stack.
73      * @return int return the current size.
74      */

75     public int size() {
76         return curIndex;
77     }
78     
79     /**
80      * Pushes an item onto the top of this stack. This method will internally
81      * add elements to the <tt>ArrayList</tt> if the stack is full.
82      * @param obj the object to be pushed onto this stack.
83      * @see java.util.ArrayList#add
84      */

85     public void push(Object JavaDoc obj) {
86         list.add(curIndex, obj);
87         curIndex += 1;
88     }
89     
90     /**
91      * Removes the object at the top of this stack and returns that
92      * object as the value of this function.
93      * @return The object at the top of this stack (the last item
94      * of the <tt>ArrayList</tt> object). Null if stack is empty.
95      */

96     public Object JavaDoc pop() {
97         if (curIndex > 0) {
98             curIndex -= 1;
99             return list.remove(curIndex);
100         }
101         return null;
102     }
103     
104     /**
105      * Tests if this stack is empty.
106      * @return <code>true</code> if and only if this stack contains
107      * no items; <code>false</code> otherwise.
108      */

109     public boolean empty() {
110         return curIndex == 0;
111     }
112     
113     /**
114      * Looks at the object at the top of this stack without removing it
115      * from the stack.
116      * @return the object at the top of this stack (the last item
117      * of the <tt>ArrayList</tt> object). Null if stack is empty.
118      */

119     public Object JavaDoc peek() {
120         Object JavaDoc top = null;
121         if (curIndex > 0) {
122             top = list.get(curIndex - 1);
123         }
124         return top;
125     }
126 }
127
Popular Tags