1 16 19 package com.sun.org.apache.xml.internal.utils; 20 21 26 public class StringToIntTable 27 { 28 29 public static final int INVALID_KEY = -10000; 30 31 32 private int m_blocksize; 33 34 36 private String m_map[]; 37 38 40 private int m_values[]; 41 42 43 private int m_firstFree = 0; 44 45 46 private int m_mapSize; 47 48 52 public StringToIntTable() 53 { 54 55 m_blocksize = 8; 56 m_mapSize = m_blocksize; 57 m_map = new String [m_blocksize]; 58 m_values = new int[m_blocksize]; 59 } 60 61 66 public StringToIntTable(int blocksize) 67 { 68 69 m_blocksize = blocksize; 70 m_mapSize = blocksize; 71 m_map = new String [blocksize]; 72 m_values = new int[m_blocksize]; 73 } 74 75 80 public final int getLength() 81 { 82 return m_firstFree; 83 } 84 85 91 public final void put(String key, int value) 92 { 93 94 if ((m_firstFree + 1) >= m_mapSize) 95 { 96 m_mapSize += m_blocksize; 97 98 String newMap[] = new String [m_mapSize]; 99 100 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1); 101 102 m_map = newMap; 103 104 int newValues[] = new int[m_mapSize]; 105 106 System.arraycopy(m_values, 0, newValues, 0, m_firstFree + 1); 107 108 m_values = newValues; 109 } 110 111 m_map[m_firstFree] = key; 112 m_values[m_firstFree] = value; 113 114 m_firstFree++; 115 } 116 117 125 public final int get(String key) 126 { 127 128 for (int i = 0; i < m_firstFree; i++) 129 { 130 if (m_map[i].equals(key)) 131 return m_values[i]; 132 } 133 134 return INVALID_KEY; 135 } 136 137 144 public final int getIgnoreCase(String key) 145 { 146 147 if (null == key) 148 return INVALID_KEY; 149 150 for (int i = 0; i < m_firstFree; i++) 151 { 152 if (m_map[i].equalsIgnoreCase(key)) 153 return m_values[i]; 154 } 155 156 return INVALID_KEY; 157 } 158 159 166 public final boolean contains(String key) 167 { 168 169 for (int i = 0; i < m_firstFree; i++) 170 { 171 if (m_map[i].equals(key)) 172 return true; 173 } 174 175 return false; 176 } 177 178 183 public final String [] keys() 184 { 185 String [] keysArr = new String [m_firstFree]; 186 187 for (int i = 0; i < m_firstFree; i++) 188 { 189 keysArr[i] = m_map[i]; 190 } 191 192 return keysArr; 193 } 194 } 195 | Popular Tags |