KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > utils > BoolStack


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: BoolStack.java,v 1.10 2004/02/17 04:21:14 minchau Exp $
18  */

19 package org.apache.xml.utils;
20
21
22 /**
23  * Simple stack for boolean values.
24  * @xsl.usage internal
25  */

26 public final class BoolStack implements Cloneable JavaDoc
27 {
28
29   /** Array of boolean values */
30   private boolean m_values[];
31
32   /** Array size allocated */
33   private int m_allocatedSize;
34
35   /** Index into the array of booleans */
36   private int m_index;
37
38   /**
39    * Default constructor. Note that the default
40    * block size is very small, for small lists.
41    */

42   public BoolStack()
43   {
44     this(32);
45   }
46
47   /**
48    * Construct a IntVector, using the given block size.
49    *
50    * @param size array size to allocate
51    */

52   public BoolStack(int size)
53   {
54
55     m_allocatedSize = size;
56     m_values = new boolean[size];
57     m_index = -1;
58   }
59
60   /**
61    * Get the length of the list.
62    *
63    * @return Current length of the list
64    */

65   public final int size()
66   {
67     return m_index + 1;
68   }
69
70   /**
71    * Clears the stack.
72    *
73    */

74   public final void clear()
75   {
76     m_index = -1;
77   }
78
79   /**
80    * Pushes an item onto the top of this stack.
81    *
82    *
83    * @param val the boolean to be pushed onto this stack.
84    * @return the <code>item</code> argument.
85    */

86   public final boolean push(boolean val)
87   {
88
89     if (m_index == m_allocatedSize - 1)
90       grow();
91
92     return (m_values[++m_index] = val);
93   }
94
95   /**
96    * Removes the object at the top of this stack and returns that
97    * object as the value of this function.
98    *
99    * @return The object at the top of this stack.
100    * @throws EmptyStackException if this stack is empty.
101    */

102   public final boolean pop()
103   {
104     return m_values[m_index--];
105   }
106
107   /**
108    * Removes the object at the top of this stack and returns the
109    * next object at the top as the value of this function.
110    *
111    *
112    * @return Next object to the top or false if none there
113    */

114   public final boolean popAndTop()
115   {
116
117     m_index--;
118
119     return (m_index >= 0) ? m_values[m_index] : false;
120   }
121
122   /**
123    * Set the item at the top of this stack
124    *
125    *
126    * @param b Object to set at the top of this stack
127    */

128   public final void setTop(boolean b)
129   {
130     m_values[m_index] = b;
131   }
132
133   /**
134    * Looks at the object at the top of this stack without removing it
135    * from the stack.
136    *
137    * @return the object at the top of this stack.
138    * @throws EmptyStackException if this stack is empty.
139    */

140   public final boolean peek()
141   {
142     return m_values[m_index];
143   }
144
145   /**
146    * Looks at the object at the top of this stack without removing it
147    * from the stack. If the stack is empty, it returns false.
148    *
149    * @return the object at the top of this stack.
150    */

151   public final boolean peekOrFalse()
152   {
153     return (m_index > -1) ? m_values[m_index] : false;
154   }
155
156   /**
157    * Looks at the object at the top of this stack without removing it
158    * from the stack. If the stack is empty, it returns true.
159    *
160    * @return the object at the top of this stack.
161    */

162   public final boolean peekOrTrue()
163   {
164     return (m_index > -1) ? m_values[m_index] : true;
165   }
166
167   /**
168    * Tests if this stack is empty.
169    *
170    * @return <code>true</code> if this stack is empty;
171    * <code>false</code> otherwise.
172    */

173   public boolean isEmpty()
174   {
175     return (m_index == -1);
176   }
177
178   /**
179    * Grows the size of the stack
180    *
181    */

182   private void grow()
183   {
184
185     m_allocatedSize *= 2;
186
187     boolean newVector[] = new boolean[m_allocatedSize];
188
189     System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
190
191     m_values = newVector;
192   }
193   
194   public Object JavaDoc clone()
195     throws CloneNotSupportedException JavaDoc
196   {
197     return super.clone();
198   }
199
200 }
201
Popular Tags