KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serializer > 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.1.4.1 2005/09/08 11:03:08 suresh_emailid Exp $
18  */

19 package com.sun.org.apache.xml.internal.serializer.utils;
20
21
22 /**
23  * Simple stack for boolean values.
24  *
25  * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
26  * It exists to cut the serializers dependancy on that package.
27  * A minor changes from that package are:
28  * doesn't implement Clonable
29  *
30  * This class is not a public API, it is only public because it is
31  * used in com.sun.org.apache.xml.internal.serializer.
32  *
33  * @xsl.usage internal
34  */

35 public final class BoolStack
36 {
37
38   /** Array of boolean values */
39   private boolean m_values[];
40
41   /** Array size allocated */
42   private int m_allocatedSize;
43
44   /** Index into the array of booleans */
45   private int m_index;
46
47   /**
48    * Default constructor. Note that the default
49    * block size is very small, for small lists.
50    */

51   public BoolStack()
52   {
53     this(32);
54   }
55
56   /**
57    * Construct a IntVector, using the given block size.
58    *
59    * @param size array size to allocate
60    */

61   public BoolStack(int size)
62   {
63
64     m_allocatedSize = size;
65     m_values = new boolean[size];
66     m_index = -1;
67   }
68
69   /**
70    * Get the length of the list.
71    *
72    * @return Current length of the list
73    */

74   public final int size()
75   {
76     return m_index + 1;
77   }
78
79   /**
80    * Clears the stack.
81    *
82    */

83   public final void clear()
84   {
85     m_index = -1;
86   }
87
88   /**
89    * Pushes an item onto the top of this stack.
90    *
91    *
92    * @param val the boolean to be pushed onto this stack.
93    * @return the <code>item</code> argument.
94    */

95   public final boolean push(boolean val)
96   {
97
98     if (m_index == m_allocatedSize - 1)
99       grow();
100
101     return (m_values[++m_index] = val);
102   }
103
104   /**
105    * Removes the object at the top of this stack and returns that
106    * object as the value of this function.
107    *
108    * @return The object at the top of this stack.
109    * @throws EmptyStackException if this stack is empty.
110    */

111   public final boolean pop()
112   {
113     return m_values[m_index--];
114   }
115
116   /**
117    * Removes the object at the top of this stack and returns the
118    * next object at the top as the value of this function.
119    *
120    *
121    * @return Next object to the top or false if none there
122    */

123   public final boolean popAndTop()
124   {
125
126     m_index--;
127
128     return (m_index >= 0) ? m_values[m_index] : false;
129   }
130
131   /**
132    * Set the item at the top of this stack
133    *
134    *
135    * @param b Object to set at the top of this stack
136    */

137   public final void setTop(boolean b)
138   {
139     m_values[m_index] = b;
140   }
141
142   /**
143    * Looks at the object at the top of this stack without removing it
144    * from the stack.
145    *
146    * @return the object at the top of this stack.
147    * @throws EmptyStackException if this stack is empty.
148    */

149   public final boolean peek()
150   {
151     return m_values[m_index];
152   }
153
154   /**
155    * Looks at the object at the top of this stack without removing it
156    * from the stack. If the stack is empty, it returns false.
157    *
158    * @return the object at the top of this stack.
159    */

160   public final boolean peekOrFalse()
161   {
162     return (m_index > -1) ? m_values[m_index] : false;
163   }
164
165   /**
166    * Looks at the object at the top of this stack without removing it
167    * from the stack. If the stack is empty, it returns true.
168    *
169    * @return the object at the top of this stack.
170    */

171   public final boolean peekOrTrue()
172   {
173     return (m_index > -1) ? m_values[m_index] : true;
174   }
175
176   /**
177    * Tests if this stack is empty.
178    *
179    * @return <code>true</code> if this stack is empty;
180    * <code>false</code> otherwise.
181    */

182   public boolean isEmpty()
183   {
184     return (m_index == -1);
185   }
186
187   /**
188    * Grows the size of the stack
189    *
190    */

191   private void grow()
192   {
193
194     m_allocatedSize *= 2;
195
196     boolean newVector[] = new boolean[m_allocatedSize];
197
198     System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
199
200     m_values = newVector;
201   }
202 }
203
Popular Tags