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: StringToIntTable.java,v 1.6 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. /**
  21. * A very simple lookup table that stores a list of strings, the even
  22. * number strings being keys, and the odd number strings being values.
  23. * @xsl.usage internal
  24. */
  25. public class StringToIntTable
  26. {
  27. public static final int INVALID_KEY = -10000;
  28. /** Block size to allocate */
  29. private int m_blocksize;
  30. /** Array of strings this table points to. Associated with ints
  31. * in m_values */
  32. private String m_map[];
  33. /** Array of ints this table points. Associated with strings from
  34. * m_map. */
  35. private int m_values[];
  36. /** Number of ints in the table */
  37. private int m_firstFree = 0;
  38. /** Size of this table */
  39. private int m_mapSize;
  40. /**
  41. * Default constructor. Note that the default
  42. * block size is very small, for small lists.
  43. */
  44. public StringToIntTable()
  45. {
  46. m_blocksize = 8;
  47. m_mapSize = m_blocksize;
  48. m_map = new String[m_blocksize];
  49. m_values = new int[m_blocksize];
  50. }
  51. /**
  52. * Construct a StringToIntTable, using the given block size.
  53. *
  54. * @param blocksize Size of block to allocate
  55. */
  56. public StringToIntTable(int blocksize)
  57. {
  58. m_blocksize = blocksize;
  59. m_mapSize = blocksize;
  60. m_map = new String[blocksize];
  61. m_values = new int[m_blocksize];
  62. }
  63. /**
  64. * Get the length of the list.
  65. *
  66. * @return the length of the list
  67. */
  68. public final int getLength()
  69. {
  70. return m_firstFree;
  71. }
  72. /**
  73. * Append a string onto the vector.
  74. *
  75. * @param key String to append
  76. * @param value The int value of the string
  77. */
  78. public final void put(String key, int value)
  79. {
  80. if ((m_firstFree + 1) >= m_mapSize)
  81. {
  82. m_mapSize += m_blocksize;
  83. String newMap[] = new String[m_mapSize];
  84. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  85. m_map = newMap;
  86. int newValues[] = new int[m_mapSize];
  87. System.arraycopy(m_values, 0, newValues, 0, m_firstFree + 1);
  88. m_values = newValues;
  89. }
  90. m_map[m_firstFree] = key;
  91. m_values[m_firstFree] = value;
  92. m_firstFree++;
  93. }
  94. /**
  95. * Tell if the table contains the given string.
  96. *
  97. * @param key String to look for
  98. *
  99. * @return The String's int value
  100. *
  101. */
  102. public final int get(String key)
  103. {
  104. for (int i = 0; i < m_firstFree; i++)
  105. {
  106. if (m_map[i].equals(key))
  107. return m_values[i];
  108. }
  109. return INVALID_KEY;
  110. }
  111. /**
  112. * Tell if the table contains the given string. Ignore case.
  113. *
  114. * @param key String to look for
  115. *
  116. * @return The string's int value
  117. */
  118. public final int getIgnoreCase(String key)
  119. {
  120. if (null == key)
  121. return INVALID_KEY;
  122. for (int i = 0; i < m_firstFree; i++)
  123. {
  124. if (m_map[i].equalsIgnoreCase(key))
  125. return m_values[i];
  126. }
  127. return INVALID_KEY;
  128. }
  129. /**
  130. * Tell if the table contains the given string.
  131. *
  132. * @param key String to look for
  133. *
  134. * @return True if the string is in the table
  135. */
  136. public final boolean contains(String key)
  137. {
  138. for (int i = 0; i < m_firstFree; i++)
  139. {
  140. if (m_map[i].equals(key))
  141. return true;
  142. }
  143. return false;
  144. }
  145. /**
  146. * Return array of keys in the table.
  147. *
  148. * @return Array of strings
  149. */
  150. public final String[] keys()
  151. {
  152. String [] keysArr = new String[m_firstFree];
  153. for (int i = 0; i < m_firstFree; i++)
  154. {
  155. keysArr[i] = m_map[i];
  156. }
  157. return keysArr;
  158. }
  159. }