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