1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xml.internal.utils;
23 
24 
25 /**
26  * Simple stack for boolean values.
27  * @xsl.usage internal
28  */
29 public final class BoolStack implements Cloneable
30 {
31 
32   /** Array of boolean values          */
33   private boolean m_values[];
34 
35   /** Array size allocated           */
36   private int m_allocatedSize;
37 
38   /** Index into the array of booleans          */
39   private int m_index;
40 
41   /**
42    * Default constructor.  Note that the default
43    * block size is very small, for small lists.
44    */
BoolStack()45   public BoolStack()
46   {
47     this(32);
48   }
49 
50   /**
51    * Construct a IntVector, using the given block size.
52    *
53    * @param size array size to allocate
54    */
BoolStack(int size)55   public BoolStack(int size)
56   {
57 
58     m_allocatedSize = size;
59     m_values = new boolean[size];
60     m_index = -1;
61   }
62 
63   /**
64    * Get the length of the list.
65    *
66    * @return Current length of the list
67    */
size()68   public final int size()
69   {
70     return m_index + 1;
71   }
72 
73   /**
74    * Clears the stack.
75    *
76    */
clear()77   public final void clear()
78   {
79         m_index = -1;
80   }
81 
82   /**
83    * Pushes an item onto the top of this stack.
84    *
85    *
86    * @param val the boolean to be pushed onto this stack.
87    * @return  the <code>item</code> argument.
88    */
push(boolean val)89   public final boolean push(boolean val)
90   {
91 
92     if (m_index == m_allocatedSize - 1)
93       grow();
94 
95     return (m_values[++m_index] = val);
96   }
97 
98   /**
99    * Removes the object at the top of this stack and returns that
100    * object as the value of this function.
101    *
102    * @return     The object at the top of this stack.
103    * @throws  EmptyStackException  if this stack is empty.
104    */
pop()105   public final boolean pop()
106   {
107     return m_values[m_index--];
108   }
109 
110   /**
111    * Removes the object at the top of this stack and returns the
112    * next object at the top as the value of this function.
113    *
114    *
115    * @return Next object to the top or false if none there
116    */
popAndTop()117   public final boolean popAndTop()
118   {
119 
120     m_index--;
121 
122     return (m_index >= 0) ? m_values[m_index] : false;
123   }
124 
125   /**
126    * Set the item at the top of this stack
127    *
128    *
129    * @param b Object to set at the top of this stack
130    */
setTop(boolean b)131   public final void setTop(boolean b)
132   {
133     m_values[m_index] = b;
134   }
135 
136   /**
137    * Looks at the object at the top of this stack without removing it
138    * from the stack.
139    *
140    * @return     the object at the top of this stack.
141    * @throws  EmptyStackException  if this stack is empty.
142    */
peek()143   public final boolean peek()
144   {
145     return m_values[m_index];
146   }
147 
148   /**
149    * Looks at the object at the top of this stack without removing it
150    * from the stack.  If the stack is empty, it returns false.
151    *
152    * @return     the object at the top of this stack.
153    */
peekOrFalse()154   public final boolean peekOrFalse()
155   {
156     return (m_index > -1) ? m_values[m_index] : false;
157   }
158 
159   /**
160    * Looks at the object at the top of this stack without removing it
161    * from the stack.  If the stack is empty, it returns true.
162    *
163    * @return     the object at the top of this stack.
164    */
peekOrTrue()165   public final boolean peekOrTrue()
166   {
167     return (m_index > -1) ? m_values[m_index] : true;
168   }
169 
170   /**
171    * Tests if this stack is empty.
172    *
173    * @return  <code>true</code> if this stack is empty;
174    *          <code>false</code> otherwise.
175    */
isEmpty()176   public boolean isEmpty()
177   {
178     return (m_index == -1);
179   }
180 
181   /**
182    * Grows the size of the stack
183    *
184    */
grow()185   private void grow()
186   {
187 
188     m_allocatedSize *= 2;
189 
190     boolean newVector[] = new boolean[m_allocatedSize];
191 
192     System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
193 
194     m_values = newVector;
195   }
196 
clone()197   public Object clone()
198     throws CloneNotSupportedException
199   {
200     return super.clone();
201   }
202 
203 }
204