1 38 39 40 package com.sun.xml.fastinfoset.util; 41 42 import java.util.HashMap ; 43 import java.util.Map ; 44 import com.sun.xml.fastinfoset.CommonResourceBundle; 45 46 public class StringIntMap extends KeyIntMap { 47 48 protected StringIntMap _readOnlyMap; 49 50 protected static class Entry extends BaseEntry { 51 final String _key; 52 Entry _next; 53 54 public Entry(String key, int hash, int value, Entry next) { 55 super(hash, value); 56 _key = key; 57 _next = next; 58 } 59 } 60 61 protected Entry[] _table; 62 63 public StringIntMap(int initialCapacity, float loadFactor) { 64 super(initialCapacity, loadFactor); 65 66 _table = new Entry[_capacity]; 67 } 68 69 public StringIntMap(int initialCapacity) { 70 this(initialCapacity, DEFAULT_LOAD_FACTOR); 71 } 72 73 public StringIntMap() { 74 this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); 75 } 76 77 public void clear() { 78 for (int i = 0; i < _table.length; i++) { 79 _table[i] = null; 80 } 81 _size = 0; 82 } 83 84 public void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) { 85 if (!(readOnlyMap instanceof StringIntMap)) { 86 throw new IllegalArgumentException (CommonResourceBundle.getInstance(). 87 getString("message.illegalClass", new Object []{readOnlyMap})); 88 } 89 90 setReadOnlyMap((StringIntMap)readOnlyMap, clear); 91 } 92 93 public final void setReadOnlyMap(StringIntMap readOnlyMap, boolean clear) { 94 _readOnlyMap = readOnlyMap; 95 if (_readOnlyMap != null) { 96 _readOnlyMapSize = _readOnlyMap.size(); 97 98 if (clear) { 99 clear(); 100 } 101 } else { 102 _readOnlyMapSize = 0; 103 } 104 } 105 106 public final int obtainIndex(String key) { 107 final int hash = hashHash(key.hashCode()); 108 109 if (_readOnlyMap != null) { 110 final int index = _readOnlyMap.get(key, hash); 111 if (index != -1) { 112 return index; 113 } 114 } 115 116 final int tableIndex = indexFor(hash, _table.length); 117 for (Entry e = _table[tableIndex]; e != null; e = e._next) { 118 if (e._hash == hash && eq(key, e._key)) { 119 return e._value; 120 } 121 } 122 123 addEntry(key, hash, _size + _readOnlyMapSize, tableIndex); 124 return NOT_PRESENT; 125 } 126 127 public final void add(String key) { 128 final int hash = hashHash(key.hashCode()); 129 final int tableIndex = indexFor(hash, _table.length); 130 addEntry(key, hash, _size + _readOnlyMapSize, tableIndex); 131 } 132 133 public final int get(String key) { 134 return get(key, hashHash(key.hashCode())); 135 } 136 137 private final int get(String key, int hash) { 138 if (_readOnlyMap != null) { 139 final int i = _readOnlyMap.get(key, hash); 140 if (i != -1) { 141 return i; 142 } 143 } 144 145 final int tableIndex = indexFor(hash, _table.length); 146 for (Entry e = _table[tableIndex]; e != null; e = e._next) { 147 if (e._hash == hash && eq(key, e._key)) { 148 return e._value; 149 } 150 } 151 152 return -1; 153 } 154 155 156 private final void addEntry(String key, int hash, int value, int bucketIndex) { 157 Entry e = _table[bucketIndex]; 158 _table[bucketIndex] = new Entry(key, hash, value, e); 159 if (_size++ >= _threshold) { 160 resize(2 * _table.length); 161 } 162 } 163 164 protected final void resize(int newCapacity) { 165 _capacity = newCapacity; 166 Entry[] oldTable = _table; 167 int oldCapacity = oldTable.length; 168 if (oldCapacity == MAXIMUM_CAPACITY) { 169 _threshold = Integer.MAX_VALUE; 170 return; 171 } 172 173 Entry[] newTable = new Entry[_capacity]; 174 transfer(newTable); 175 _table = newTable; 176 _threshold = (int)(_capacity * _loadFactor); 177 } 178 179 private final void transfer(Entry[] newTable) { 180 Entry[] src = _table; 181 int newCapacity = newTable.length; 182 for (int j = 0; j < src.length; j++) { 183 Entry e = src[j]; 184 if (e != null) { 185 src[j] = null; 186 do { 187 Entry next = e._next; 188 int i = indexFor(e._hash, newCapacity); 189 e._next = newTable[i]; 190 newTable[i] = e; 191 e = next; 192 } while (e != null); 193 } 194 } 195 } 196 197 private final boolean eq(String x, String y) { 198 return x == y || x.equals(y); 199 } 200 } 201 | Popular Tags |