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: StringVector.java,v 1.7 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. /**
  21. * A very simple table that stores a list of strings, optimized
  22. * for small lists.
  23. * @xsl.usage internal
  24. */
  25. public class StringVector implements java.io.Serializable
  26. {
  27. /** @serial Size of blocks to allocate */
  28. protected int m_blocksize;
  29. /** @serial Array of strings this contains */
  30. protected String m_map[];
  31. /** @serial Number of strings this contains */
  32. protected int m_firstFree = 0;
  33. /** @serial Size of the array */
  34. protected int m_mapSize;
  35. /**
  36. * Default constructor. Note that the default
  37. * block size is very small, for small lists.
  38. */
  39. public StringVector()
  40. {
  41. m_blocksize = 8;
  42. m_mapSize = m_blocksize;
  43. m_map = new String[m_blocksize];
  44. }
  45. /**
  46. * Construct a StringVector, using the given block size.
  47. *
  48. * @param blocksize Size of the blocks to allocate
  49. */
  50. public StringVector(int blocksize)
  51. {
  52. m_blocksize = blocksize;
  53. m_mapSize = blocksize;
  54. m_map = new String[blocksize];
  55. }
  56. /**
  57. * Get the length of the list.
  58. *
  59. * @return Number of strings in the list
  60. */
  61. public int getLength()
  62. {
  63. return m_firstFree;
  64. }
  65. /**
  66. * Get the length of the list.
  67. *
  68. * @return Number of strings in the list
  69. */
  70. public final int size()
  71. {
  72. return m_firstFree;
  73. }
  74. /**
  75. * Append a string onto the vector.
  76. *
  77. * @param value Sting to add to the vector
  78. */
  79. public final void addElement(String value)
  80. {
  81. if ((m_firstFree + 1) >= m_mapSize)
  82. {
  83. m_mapSize += m_blocksize;
  84. String newMap[] = new String[m_mapSize];
  85. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  86. m_map = newMap;
  87. }
  88. m_map[m_firstFree] = value;
  89. m_firstFree++;
  90. }
  91. /**
  92. * Get the nth element.
  93. *
  94. * @param i Index of string to find
  95. *
  96. * @return String at given index
  97. */
  98. public final String elementAt(int i)
  99. {
  100. return m_map[i];
  101. }
  102. /**
  103. * Tell if the table contains the given string.
  104. *
  105. * @param s String to look for
  106. *
  107. * @return True if the string is in this table
  108. */
  109. public final boolean contains(String s)
  110. {
  111. if (null == s)
  112. return false;
  113. for (int i = 0; i < m_firstFree; i++)
  114. {
  115. if (m_map[i].equals(s))
  116. return true;
  117. }
  118. return false;
  119. }
  120. /**
  121. * Tell if the table contains the given string. Ignore case.
  122. *
  123. * @param s String to find
  124. *
  125. * @return True if the String is in this vector
  126. */
  127. public final boolean containsIgnoreCase(String s)
  128. {
  129. if (null == s)
  130. return false;
  131. for (int i = 0; i < m_firstFree; i++)
  132. {
  133. if (m_map[i].equalsIgnoreCase(s))
  134. return true;
  135. }
  136. return false;
  137. }
  138. /**
  139. * Tell if the table contains the given string.
  140. *
  141. * @param s String to push into the vector
  142. */
  143. public final void push(String s)
  144. {
  145. if ((m_firstFree + 1) >= m_mapSize)
  146. {
  147. m_mapSize += m_blocksize;
  148. String newMap[] = new String[m_mapSize];
  149. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  150. m_map = newMap;
  151. }
  152. m_map[m_firstFree] = s;
  153. m_firstFree++;
  154. }
  155. /**
  156. * Pop the tail of this vector.
  157. *
  158. * @return The String last added to this vector or null not found.
  159. * The string is removed from the vector.
  160. */
  161. public final String pop()
  162. {
  163. if (m_firstFree <= 0)
  164. return null;
  165. m_firstFree--;
  166. String s = m_map[m_firstFree];
  167. m_map[m_firstFree] = null;
  168. return s;
  169. }
  170. /**
  171. * Get the string at the tail of this vector without popping.
  172. *
  173. * @return The string at the tail of this vector.
  174. */
  175. public final String peek()
  176. {
  177. return (m_firstFree <= 0) ? null : m_map[m_firstFree - 1];
  178. }
  179. }