1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. import java.util.EmptyStackException;
  59. /**
  60. * <meta name="usage" content="internal"/>
  61. * Simple stack for boolean values.
  62. */
  63. public final class BoolStack implements Cloneable
  64. {
  65. /** Array of boolean values */
  66. private boolean m_values[];
  67. /** Array size allocated */
  68. private int m_allocatedSize;
  69. /** Index into the array of booleans */
  70. private int m_index;
  71. /**
  72. * Default constructor. Note that the default
  73. * block size is very small, for small lists.
  74. */
  75. public BoolStack()
  76. {
  77. this(32);
  78. }
  79. /**
  80. * Construct a IntVector, using the given block size.
  81. *
  82. * @param size array size to allocate
  83. */
  84. public BoolStack(int size)
  85. {
  86. m_allocatedSize = size;
  87. m_values = new boolean[size];
  88. m_index = -1;
  89. }
  90. /**
  91. * Get the length of the list.
  92. *
  93. * @return Current length of the list
  94. */
  95. public final int size()
  96. {
  97. return m_index + 1;
  98. }
  99. /**
  100. * Pushes an item onto the top of this stack.
  101. *
  102. *
  103. * @param val the boolean to be pushed onto this stack.
  104. * @return the <code>item</code> argument.
  105. */
  106. public final boolean push(boolean val)
  107. {
  108. if (m_index == m_allocatedSize - 1)
  109. grow();
  110. return (m_values[++m_index] = val);
  111. }
  112. /**
  113. * Removes the object at the top of this stack and returns that
  114. * object as the value of this function.
  115. *
  116. * @return The object at the top of this stack.
  117. * @throws EmptyStackException if this stack is empty.
  118. */
  119. public final boolean pop()
  120. {
  121. return m_values[m_index--];
  122. }
  123. /**
  124. * Removes the object at the top of this stack and returns the
  125. * next object at the top as the value of this function.
  126. *
  127. *
  128. * @return Next object to the top or false if none there
  129. */
  130. public final boolean popAndTop()
  131. {
  132. m_index--;
  133. return (m_index >= 0) ? m_values[m_index] : false;
  134. }
  135. /**
  136. * Set the item at the top of this stack
  137. *
  138. *
  139. * @param b Object to set at the top of this stack
  140. */
  141. public final void setTop(boolean b)
  142. {
  143. m_values[m_index] = b;
  144. }
  145. /**
  146. * Looks at the object at the top of this stack without removing it
  147. * from the stack.
  148. *
  149. * @return the object at the top of this stack.
  150. * @throws EmptyStackException if this stack is empty.
  151. */
  152. public final boolean peek()
  153. {
  154. return m_values[m_index];
  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 false.
  159. *
  160. * @return the object at the top of this stack.
  161. */
  162. public final boolean peekOrFalse()
  163. {
  164. return (m_index > -1) ? m_values[m_index] : false;
  165. }
  166. /**
  167. * Looks at the object at the top of this stack without removing it
  168. * from the stack. If the stack is empty, it returns true.
  169. *
  170. * @return the object at the top of this stack.
  171. */
  172. public final boolean peekOrTrue()
  173. {
  174. return (m_index > -1) ? m_values[m_index] : true;
  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. * Grows the size of the stack
  188. *
  189. */
  190. private void grow()
  191. {
  192. m_allocatedSize *= 2;
  193. boolean newVector[] = new boolean[m_allocatedSize];
  194. System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
  195. m_values = newVector;
  196. }
  197. public Object clone()
  198. throws CloneNotSupportedException
  199. {
  200. return super.clone();
  201. }
  202. }