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$
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import java.util.EmptyStackException;
  21. /**
  22. * Implement a stack of simple integers.
  23. *
  24. * %OPT%
  25. * This is currently based on ObjectVector, which permits fast acess but pays a
  26. * heavy recopying penalty if/when its size is increased. If we expect deep
  27. * stacks, we should consider a version based on ChunkedObjectVector.
  28. * @xsl.usage internal
  29. */
  30. public class ObjectStack extends ObjectVector
  31. {
  32. /**
  33. * Default constructor. Note that the default
  34. * block size is very small, for small lists.
  35. */
  36. public ObjectStack()
  37. {
  38. super();
  39. }
  40. /**
  41. * Construct a ObjectVector, using the given block size.
  42. *
  43. * @param blocksize Size of block to allocate
  44. */
  45. public ObjectStack(int blocksize)
  46. {
  47. super(blocksize);
  48. }
  49. /**
  50. * Copy constructor for ObjectStack
  51. *
  52. * @param v ObjectStack to copy
  53. */
  54. public ObjectStack (ObjectStack v)
  55. {
  56. super(v);
  57. }
  58. /**
  59. * Pushes an item onto the top of this stack.
  60. *
  61. * @param i the int to be pushed onto this stack.
  62. * @return the <code>item</code> argument.
  63. */
  64. public Object push(Object i)
  65. {
  66. if ((m_firstFree + 1) >= m_mapSize)
  67. {
  68. m_mapSize += m_blocksize;
  69. Object newMap[] = new Object[m_mapSize];
  70. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  71. m_map = newMap;
  72. }
  73. m_map[m_firstFree] = i;
  74. m_firstFree++;
  75. return i;
  76. }
  77. /**
  78. * Removes the object at the top of this stack and returns that
  79. * object as the value of this function.
  80. *
  81. * @return The object at the top of this stack.
  82. */
  83. public Object pop()
  84. {
  85. Object val = m_map[--m_firstFree];
  86. m_map[m_firstFree] = null;
  87. return val;
  88. }
  89. /**
  90. * Quickly pops a number of items from the stack.
  91. */
  92. public void quickPop(int n)
  93. {
  94. m_firstFree -= n;
  95. }
  96. /**
  97. * Looks at the object at the top of this stack without removing it
  98. * from the stack.
  99. *
  100. * @return the object at the top of this stack.
  101. * @throws EmptyStackException if this stack is empty.
  102. */
  103. public Object peek()
  104. {
  105. try {
  106. return m_map[m_firstFree - 1];
  107. }
  108. catch (ArrayIndexOutOfBoundsException e)
  109. {
  110. throw new EmptyStackException();
  111. }
  112. }
  113. /**
  114. * Looks at the object at the position the stack counting down n items.
  115. *
  116. * @param n The number of items down, indexed from zero.
  117. * @return the object at n items down.
  118. * @throws EmptyStackException if this stack is empty.
  119. */
  120. public Object peek(int n)
  121. {
  122. try {
  123. return m_map[m_firstFree-(1+n)];
  124. }
  125. catch (ArrayIndexOutOfBoundsException e)
  126. {
  127. throw new EmptyStackException();
  128. }
  129. }
  130. /**
  131. * Sets an object at a the top of the statck
  132. *
  133. *
  134. * @param val object to set at the top
  135. * @throws EmptyStackException if this stack is empty.
  136. */
  137. public void setTop(Object val)
  138. {
  139. try {
  140. m_map[m_firstFree - 1] = val;
  141. }
  142. catch (ArrayIndexOutOfBoundsException e)
  143. {
  144. throw new EmptyStackException();
  145. }
  146. }
  147. /**
  148. * Tests if this stack is empty.
  149. *
  150. * @return <code>true</code> if this stack is empty;
  151. * <code>false</code> otherwise.
  152. * @since JDK1.0
  153. */
  154. public boolean empty()
  155. {
  156. return m_firstFree == 0;
  157. }
  158. /**
  159. * Returns where an object is on this stack.
  160. *
  161. * @param o the desired object.
  162. * @return the distance from the top of the stack where the object is]
  163. * located; the return value <code>-1</code> indicates that the
  164. * object is not on the stack.
  165. * @since JDK1.0
  166. */
  167. public int search(Object o)
  168. {
  169. int i = lastIndexOf(o);
  170. if (i >= 0)
  171. {
  172. return size() - i;
  173. }
  174. return -1;
  175. }
  176. /**
  177. * Returns clone of current ObjectStack
  178. *
  179. * @return clone of current ObjectStack
  180. */
  181. public Object clone()
  182. throws CloneNotSupportedException
  183. {
  184. return (ObjectStack) super.clone();
  185. }
  186. }