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 com.sun.org.apache.xml.internal.utils;
  20. /**
  21. * Simple stack for boolean values.
  22. * @xsl.usage internal
  23. */
  24. public final class BoolStack implements Cloneable
  25. {
  26. /** Array of boolean values */
  27. private boolean m_values[];
  28. /** Array size allocated */
  29. private int m_allocatedSize;
  30. /** Index into the array of booleans */
  31. private int m_index;
  32. /**
  33. * Default constructor. Note that the default
  34. * block size is very small, for small lists.
  35. */
  36. public BoolStack()
  37. {
  38. this(32);
  39. }
  40. /**
  41. * Construct a IntVector, using the given block size.
  42. *
  43. * @param size array size to allocate
  44. */
  45. public BoolStack(int size)
  46. {
  47. m_allocatedSize = size;
  48. m_values = new boolean[size];
  49. m_index = -1;
  50. }
  51. /**
  52. * Get the length of the list.
  53. *
  54. * @return Current length of the list
  55. */
  56. public final int size()
  57. {
  58. return m_index + 1;
  59. }
  60. /**
  61. * Clears the stack.
  62. *
  63. */
  64. public final void clear()
  65. {
  66. m_index = -1;
  67. }
  68. /**
  69. * Pushes an item onto the top of this stack.
  70. *
  71. *
  72. * @param val the boolean to be pushed onto this stack.
  73. * @return the <code>item</code> argument.
  74. */
  75. public final boolean push(boolean val)
  76. {
  77. if (m_index == m_allocatedSize - 1)
  78. grow();
  79. return (m_values[++m_index] = val);
  80. }
  81. /**
  82. * Removes the object at the top of this stack and returns that
  83. * object as the value of this function.
  84. *
  85. * @return The object at the top of this stack.
  86. * @throws EmptyStackException if this stack is empty.
  87. */
  88. public final boolean pop()
  89. {
  90. return m_values[m_index--];
  91. }
  92. /**
  93. * Removes the object at the top of this stack and returns the
  94. * next object at the top as the value of this function.
  95. *
  96. *
  97. * @return Next object to the top or false if none there
  98. */
  99. public final boolean popAndTop()
  100. {
  101. m_index--;
  102. return (m_index >= 0) ? m_values[m_index] : false;
  103. }
  104. /**
  105. * Set the item at the top of this stack
  106. *
  107. *
  108. * @param b Object to set at the top of this stack
  109. */
  110. public final void setTop(boolean b)
  111. {
  112. m_values[m_index] = b;
  113. }
  114. /**
  115. * Looks at the object at the top of this stack without removing it
  116. * from the stack.
  117. *
  118. * @return the object at the top of this stack.
  119. * @throws EmptyStackException if this stack is empty.
  120. */
  121. public final boolean peek()
  122. {
  123. return m_values[m_index];
  124. }
  125. /**
  126. * Looks at the object at the top of this stack without removing it
  127. * from the stack. If the stack is empty, it returns false.
  128. *
  129. * @return the object at the top of this stack.
  130. */
  131. public final boolean peekOrFalse()
  132. {
  133. return (m_index > -1) ? m_values[m_index] : false;
  134. }
  135. /**
  136. * Looks at the object at the top of this stack without removing it
  137. * from the stack. If the stack is empty, it returns true.
  138. *
  139. * @return the object at the top of this stack.
  140. */
  141. public final boolean peekOrTrue()
  142. {
  143. return (m_index > -1) ? m_values[m_index] : true;
  144. }
  145. /**
  146. * Tests if this stack is empty.
  147. *
  148. * @return <code>true</code> if this stack is empty;
  149. * <code>false</code> otherwise.
  150. */
  151. public boolean isEmpty()
  152. {
  153. return (m_index == -1);
  154. }
  155. /**
  156. * Grows the size of the stack
  157. *
  158. */
  159. private void grow()
  160. {
  161. m_allocatedSize *= 2;
  162. boolean newVector[] = new boolean[m_allocatedSize];
  163. System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
  164. m_values = newVector;
  165. }
  166. public Object clone()
  167. throws CloneNotSupportedException
  168. {
  169. return super.clone();
  170. }
  171. }