KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > TIntStack


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3
//
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
8
//
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
///////////////////////////////////////////////////////////////////////////////
18

19
20 package gnu.trove;
21
22 /**
23  * A stack of int primitives, backed by a TIntArrayList.
24  *
25  * Created: Tue Jan 1 10:30:35 2002
26  *
27  * @author Eric D. Friedman
28  * @version $Id: TIntStack.java,v 1.1 2002/01/01 18:51:23 ericdf Exp $
29  */

30
31 public class TIntStack {
32
33     /** the list used to hold the stack values. */
34     protected TIntArrayList _list;
35
36     public static final int DEFAULT_CAPACITY = TIntArrayList.DEFAULT_CAPACITY;
37     
38     /**
39      * Creates a new <code>TIntStack</code> instance with the default
40      * capacity.
41      */

42     public TIntStack() {
43         this(DEFAULT_CAPACITY);
44     }
45
46     /**
47      * Creates a new <code>TIntStack</code> instance with the
48      * specified capacity.
49      *
50      * @param capacity the initial depth of the stack
51      */

52     public TIntStack(int capacity) {
53         _list = new TIntArrayList(capacity);
54     }
55
56     /**
57      * Pushes the value onto the top of the stack.
58      *
59      * @param val an <code>int</code> value
60      */

61     public void push(int val) {
62         _list.add(val);
63     }
64
65     /**
66      * Removes and returns the value at the top of the stack.
67      *
68      * @return an <code>int</code> value
69      */

70     public int pop() {
71         return _list.remove(_list.size() - 1);
72     }
73
74     /**
75      * Returns the value at the top of the stack.
76      *
77      * @return an <code>int</code> value
78      */

79     public int peek() {
80         return _list.get(_list.size() - 1);
81     }
82
83     /**
84      * Returns the current depth of the stack.
85      *
86      * @return an <code>int</code> value
87      */

88     public int size() {
89         return _list.size();
90     }
91
92     /**
93      * Clears the stack, reseting its capacity to the default.
94      */

95     public void clear() {
96         _list.clear(DEFAULT_CAPACITY);
97     }
98
99     /**
100      * Clears the stack without releasing its internal capacity allocation.
101      */

102     public void reset() {
103         _list.reset();
104     }
105 } // TIntStack
106
Popular Tags