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: StringToStringTableVector.java,v 1.5 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 StringToStringTables, optimized
  22. * for small lists.
  23. * @xsl.usage internal
  24. */
  25. public class StringToStringTableVector
  26. {
  27. /** Size of blocks to allocate */
  28. private int m_blocksize;
  29. /** Array of StringToStringTable objects */
  30. private StringToStringTable m_map[];
  31. /** Number of StringToStringTable objects in this array */
  32. private int m_firstFree = 0;
  33. /** Size of this array */
  34. private int m_mapSize;
  35. /**
  36. * Default constructor. Note that the default
  37. * block size is very small, for small lists.
  38. */
  39. public StringToStringTableVector()
  40. {
  41. m_blocksize = 8;
  42. m_mapSize = m_blocksize;
  43. m_map = new StringToStringTable[m_blocksize];
  44. }
  45. /**
  46. * Construct a StringToStringTableVector, using the given block size.
  47. *
  48. * @param blocksize Size of blocks to allocate
  49. */
  50. public StringToStringTableVector(int blocksize)
  51. {
  52. m_blocksize = blocksize;
  53. m_mapSize = blocksize;
  54. m_map = new StringToStringTable[blocksize];
  55. }
  56. /**
  57. * Get the length of the list.
  58. *
  59. * @return Number of StringToStringTable objects in the list
  60. */
  61. public final int getLength()
  62. {
  63. return m_firstFree;
  64. }
  65. /**
  66. * Get the length of the list.
  67. *
  68. * @return Number of StringToStringTable objects in the list
  69. */
  70. public final int size()
  71. {
  72. return m_firstFree;
  73. }
  74. /**
  75. * Append a StringToStringTable object onto the vector.
  76. *
  77. * @param value StringToStringTable object to add
  78. */
  79. public final void addElement(StringToStringTable value)
  80. {
  81. if ((m_firstFree + 1) >= m_mapSize)
  82. {
  83. m_mapSize += m_blocksize;
  84. StringToStringTable newMap[] = new StringToStringTable[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. * Given a string, find the last added occurance value
  93. * that matches the key.
  94. *
  95. * @param key String to look up
  96. *
  97. * @return the last added occurance value that matches the key
  98. * or null if not found.
  99. */
  100. public final String get(String key)
  101. {
  102. for (int i = m_firstFree - 1; i >= 0; --i)
  103. {
  104. String nsuri = m_map[i].get(key);
  105. if (nsuri != null)
  106. return nsuri;
  107. }
  108. return null;
  109. }
  110. /**
  111. * Given a string, find out if there is a value in this table
  112. * that matches the key.
  113. *
  114. * @param key String to look for
  115. *
  116. * @return True if the string was found in table, null if not
  117. */
  118. public final boolean containsKey(String key)
  119. {
  120. for (int i = m_firstFree - 1; i >= 0; --i)
  121. {
  122. if (m_map[i].get(key) != null)
  123. return true;
  124. }
  125. return false;
  126. }
  127. /**
  128. * Remove the last element.
  129. */
  130. public final void removeLastElem()
  131. {
  132. if (m_firstFree > 0)
  133. {
  134. m_map[m_firstFree] = null;
  135. m_firstFree--;
  136. }
  137. }
  138. /**
  139. * Get the nth element.
  140. *
  141. * @param i Index of element to find
  142. *
  143. * @return The StringToStringTable object at the given index
  144. */
  145. public final StringToStringTable elementAt(int i)
  146. {
  147. return m_map[i];
  148. }
  149. /**
  150. * Tell if the table contains the given StringToStringTable.
  151. *
  152. * @param s The StringToStringTable to find
  153. *
  154. * @return True if the StringToStringTable is found
  155. */
  156. public final boolean contains(StringToStringTable s)
  157. {
  158. for (int i = 0; i < m_firstFree; i++)
  159. {
  160. if (m_map[i].equals(s))
  161. return true;
  162. }
  163. return false;
  164. }
  165. }