1 2 12 package com.versant.core.util; 13 14 import java.io.IOException ; 15 import java.io.Serializable ; 16 17 22 public final class IntObjectHashMap implements Serializable { 23 24 27 private static final int DEFAULT_INITIAL_CAPACITY = 16; 28 29 34 private static final int MAXIMUM_CAPACITY = 1 << 30; 35 36 39 private static final float DEFAULT_LOAD_FACTOR = 0.75f; 40 41 44 private transient Entry[] table; 45 46 49 private transient int size; 50 51 56 private int threshold; 57 58 63 private final float loadFactor; 64 65 74 public IntObjectHashMap(int initialCapacity, float loadFactor) { 75 if (initialCapacity < 0) { 76 throw new IllegalArgumentException ("Illegal initial capacity: " + 77 initialCapacity); 78 } 79 if (initialCapacity > MAXIMUM_CAPACITY) { 80 initialCapacity = MAXIMUM_CAPACITY; 81 } 82 if (loadFactor <= 0 || Float.isNaN(loadFactor)) { 83 throw new IllegalArgumentException ("Illegal load factor: " + 84 loadFactor); 85 } 86 87 int capacity = 1; 89 while (capacity < initialCapacity) { 90 capacity <<= 1; 91 } 92 93 this.loadFactor = loadFactor; 94 threshold = (int)(capacity * loadFactor); 95 table = new Entry[capacity]; 96 } 97 98 105 public IntObjectHashMap(int initialCapacity) { 106 this(initialCapacity, DEFAULT_LOAD_FACTOR); 107 } 108 109 113 public IntObjectHashMap() { 114 this.loadFactor = DEFAULT_LOAD_FACTOR; 115 threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); 116 table = new Entry[DEFAULT_INITIAL_CAPACITY]; 117 } 118 119 138 public String toString() { 139 StringBuffer buf = new StringBuffer (); 140 buf.append("{"); 141 for (int i = 0; i < table.length; i++) { 142 Entry e = table[i]; 143 for (; e != null; e = e.next) { 144 int key = e.key; 145 Object value = e.getValue(); 146 buf.append(key + "=" + (value == this ? "(this Map)" : value)); 147 } 148 } 149 buf.append("}"); 150 return buf.toString(); 151 } 152 153 158 public int size() { 159 return size; 160 } 161 162 167 public boolean isEmpty() { 168 return size == 0; 169 } 170 171 183 public Object get(int key) { 184 int i = key & (table.length - 1); 185 Entry e = table[i]; 186 while (true) { 187 if (e == null) { 188 return e; 189 } 190 if (key == e.key) { 191 return e.value; 192 } 193 e = e.next; 194 } 195 } 196 197 205 public boolean containsKey(int key) { 206 int i = key & (table.length - 1); 207 Entry e = table[i]; 208 while (e != null) { 209 if (key == e.key) { 210 return true; 211 } 212 e = e.next; 213 } 214 return false; 215 } 216 217 229 public Object put(int key, Object value) { 230 int i = key & (table.length - 1); 231 for (Entry e = table[i]; e != null; e = e.next) { 232 if (key == e.key) { 233 Object oldValue = e.value; 234 e.value = value; 235 return oldValue; 236 } 237 } 238 addEntry(key, value, i); 239 return null; 240 } 241 242 248 private void putForCreate(int key, Object value) { 249 int i = key & (table.length - 1); 250 251 256 for (Entry e = table[i]; e != null; e = e.next) { 257 if (key == e.key) { 258 e.value = value; 259 return; 260 } 261 } 262 createEntry(key, value, i); 263 } 264 265 279 private void resize(int newCapacity) { 280 Entry[] oldTable = table; 281 int oldCapacity = oldTable.length; 282 if (oldCapacity == MAXIMUM_CAPACITY) { 283 threshold = Integer.MAX_VALUE; 284 return; 285 } 286 287 Entry[] newTable = new Entry[newCapacity]; 288 transfer(newTable); 289 table = newTable; 290 threshold = (int)(newCapacity * loadFactor); 291 } 292 293 296 private void transfer(Entry[] newTable) { 297 Entry[] src = table; 298 int newCapacity = newTable.length; 299 for (int j = 0; j < src.length; j++) { 300 Entry e = src[j]; 301 if (e != null) { 302 src[j] = null; 303 do { 304 Entry next = e.next; 305 int i = e.key & (newCapacity - 1); 306 e.next = newTable[i]; 307 newTable[i] = e; 308 e = next; 309 } while (e != null); 310 } 311 } 312 } 313 314 323 public Object remove(int key) { 324 Entry e = removeEntryForKey(key); 325 return e == null ? e : e.value; 326 } 327 328 333 private Entry removeEntryForKey(int key) { 334 int i = key & (table.length - 1); 335 Entry prev = table[i]; 336 Entry e = prev; 337 338 while (e != null) { 339 Entry next = e.next; 340 if (key == e.key) { 341 size--; 342 if (prev == e) { 343 table[i] = next; 344 } else { 345 prev.next = next; 346 } 347 return e; 348 } 349 prev = e; 350 e = next; 351 } 352 353 return e; 354 } 355 356 359 public void clear() { 360 Entry tab[] = table; 361 for (int i = 0; i < tab.length; i++) { 362 tab[i] = null; 363 } 364 size = 0; 365 } 366 367 375 public boolean containsValue(Object value) { 376 Entry tab[] = table; 377 for (int i = 0; i < tab.length; i++) { 378 for (Entry e = tab[i]; e != null; e = e.next) { 379 if (value.equals(e.value)) { 380 return true; 381 } 382 } 383 } 384 return false; 385 } 386 387 private static class Entry { 388 389 final int key; 390 Object value; 391 Entry next; 392 393 396 public Entry(int k, Object v, Entry n) { 397 value = v; 398 next = n; 399 key = k; 400 } 401 402 public Object getValue() { 403 return value; 404 } 405 406 public Object setValue(Object newValue) { 407 Object oldValue = value; 408 value = newValue; 409 return oldValue; 410 } 411 412 public boolean equals(Object o) { 413 if (!(o instanceof Entry)) { 414 return false; 415 } 416 Entry e = (Entry)o; 417 if (key == e.key) { 418 if (value == e.value || (value != null && value.equals(e.value))) { 419 return true; 420 } 421 } 422 return false; 423 } 424 425 public int hashCode() { 426 return key ^ (value == null ? 0 : value.hashCode()); 427 } 428 429 public String toString() { 430 return key + "=" + getValue(); 431 } 432 433 } 434 435 442 private void addEntry(int key, Object value, int bucketIndex) { 443 table[bucketIndex] = new Entry(key, value, table[bucketIndex]); 444 if (size++ >= threshold) { 445 resize(2 * table.length); 446 } 447 } 448 449 457 private void createEntry(int key, Object value, int bucketIndex) { 458 table[bucketIndex] = new Entry(key, value, table[bucketIndex]); 459 size++; 460 } 461 462 474 private void writeObject(java.io.ObjectOutputStream s) 475 throws IOException { 476 s.defaultWriteObject(); 478 479 s.writeInt(table.length); 481 482 s.writeInt(size); 484 485 int c = 0; 487 for (int i = 0; c < size && i < table.length; i++) { 488 Entry e = table[i]; 489 for (; e != null; e = e.next, ++c) { 490 s.writeInt(e.key); 491 s.writeObject(e.getValue()); 492 } 493 } 494 } 495 496 500 private void readObject(java.io.ObjectInputStream s) 501 throws IOException , ClassNotFoundException { 502 s.defaultReadObject(); 504 505 int numBuckets = s.readInt(); 507 table = new Entry[numBuckets]; 508 509 int size = s.readInt(); 511 512 for (int i = 0; i < size; i++) { 514 int key = s.readInt(); 515 Object value = s.readObject(); 516 putForCreate(key, value); 517 } 518 } 519 520 } 521 | Popular Tags |