1 16 17 21 package org.apache.commons.lang; 22 23 37 class IntHashMap { 38 39 42 private transient Entry table[]; 43 44 47 private transient int count; 48 49 55 private int threshold; 56 57 62 private float loadFactor; 63 64 68 private static class Entry { 69 int hash; 70 int key; 71 Object value; 72 Entry next; 73 74 82 protected Entry(int hash, int key, Object value, Entry next) { 83 this.hash = hash; 84 this.key = key; 85 this.value = value; 86 this.next = next; 87 } 88 } 89 90 94 public IntHashMap() { 95 this(20, 0.75f); 96 } 97 98 106 public IntHashMap(int initialCapacity) { 107 this(initialCapacity, 0.75f); 108 } 109 110 119 public IntHashMap(int initialCapacity, float loadFactor) { 120 super(); 121 if (initialCapacity < 0) { 122 throw new IllegalArgumentException ("Illegal Capacity: " + initialCapacity); 123 } 124 if (loadFactor <= 0) { 125 throw new IllegalArgumentException ("Illegal Load: " + loadFactor); 126 } 127 if (initialCapacity == 0) { 128 initialCapacity = 1; 129 } 130 131 this.loadFactor = loadFactor; 132 table = new Entry[initialCapacity]; 133 threshold = (int) (initialCapacity * loadFactor); 134 } 135 136 141 public int size() { 142 return count; 143 } 144 145 151 public boolean isEmpty() { 152 return count == 0; 153 } 154 155 173 public boolean contains(Object value) { 174 if (value == null) { 175 throw new NullPointerException (); 176 } 177 178 Entry tab[] = table; 179 for (int i = tab.length; i-- > 0;) { 180 for (Entry e = tab[i]; e != null; e = e.next) { 181 if (e.value.equals(value)) { 182 return true; 183 } 184 } 185 } 186 return false; 187 } 188 189 201 public boolean containsValue(Object value) { 202 return contains(value); 203 } 204 205 214 public boolean containsKey(int key) { 215 Entry tab[] = table; 216 int hash = key; 217 int index = (hash & 0x7FFFFFFF) % tab.length; 218 for (Entry e = tab[index]; e != null; e = e.next) { 219 if (e.hash == hash) { 220 return true; 221 } 222 } 223 return false; 224 } 225 226 235 public Object get(int key) { 236 Entry tab[] = table; 237 int hash = key; 238 int index = (hash & 0x7FFFFFFF) % tab.length; 239 for (Entry e = tab[index]; e != null; e = e.next) { 240 if (e.hash == hash) { 241 return e.value; 242 } 243 } 244 return null; 245 } 246 247 256 protected void rehash() { 257 int oldCapacity = table.length; 258 Entry oldMap[] = table; 259 260 int newCapacity = oldCapacity * 2 + 1; 261 Entry newMap[] = new Entry[newCapacity]; 262 263 threshold = (int) (newCapacity * loadFactor); 264 table = newMap; 265 266 for (int i = oldCapacity; i-- > 0;) { 267 for (Entry old = oldMap[i]; old != null;) { 268 Entry e = old; 269 old = old.next; 270 271 int index = (e.hash & 0x7FFFFFFF) % newCapacity; 272 e.next = newMap[index]; 273 newMap[index] = e; 274 } 275 } 276 } 277 278 293 public Object put(int key, Object value) { 294 Entry tab[] = table; 296 int hash = key; 297 int index = (hash & 0x7FFFFFFF) % tab.length; 298 for (Entry e = tab[index]; e != null; e = e.next) { 299 if (e.hash == hash) { 300 Object old = e.value; 301 e.value = value; 302 return old; 303 } 304 } 305 306 if (count >= threshold) { 307 rehash(); 309 310 tab = table; 311 index = (hash & 0x7FFFFFFF) % tab.length; 312 } 313 314 Entry e = new Entry(hash, key, value, tab[index]); 316 tab[index] = e; 317 count++; 318 return null; 319 } 320 321 332 public Object remove(int key) { 333 Entry tab[] = table; 334 int hash = key; 335 int index = (hash & 0x7FFFFFFF) % tab.length; 336 for (Entry e = tab[index], prev = null; e != null; prev = e, e = e.next) { 337 if (e.hash == hash) { 338 if (prev != null) { 339 prev.next = e.next; 340 } else { 341 tab[index] = e.next; 342 } 343 count--; 344 Object oldValue = e.value; 345 e.value = null; 346 return oldValue; 347 } 348 } 349 return null; 350 } 351 352 355 public synchronized void clear() { 356 Entry tab[] = table; 357 for (int index = tab.length; --index >= 0;) { 358 tab[index] = null; 359 } 360 count = 0; 361 } 362 363 } 364 | Popular Tags |