167 * The load factor for the hash table.
168 */
169 private final float loadFactor;
170
171 /**
172 * Reference queue for cleared WeakEntries
173 */
174 private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
175
176 /**
177 * The number of times this WeakHashMap has been structurally modified.
178 * Structural modifications are those that change the number of
179 * mappings in the map or otherwise modify its internal structure
180 * (e.g., rehash). This field is used to make iterators on
181 * Collection-views of the map fail-fast.
182 *
183 * @see ConcurrentModificationException
184 */
185 int modCount;
186
187 @SuppressWarnings("unchecked")
188 private Entry<K,V>[] newTable(int n) {
189 return (Entry<K,V>[]) new Entry[n];
190 }
191
192 /**
193 * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
194 * capacity and the given load factor.
195 *
196 * @param initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
197 * @param loadFactor The load factor of the <tt>WeakHashMap</tt>
198 * @throws IllegalArgumentException if the initial capacity is negative,
199 * or if the load factor is nonpositive.
200 */
201 public WeakHashMap(int initialCapacity, float loadFactor) {
202 if (initialCapacity < 0)
203 throw new IllegalArgumentException("Illegal Initial Capacity: "+
204 initialCapacity);
205 if (initialCapacity > MAXIMUM_CAPACITY)
206 initialCapacity = MAXIMUM_CAPACITY;
207
208 if (loadFactor <= 0 || Float.isNaN(loadFactor))
209 throw new IllegalArgumentException("Illegal Load factor: "+
210 loadFactor);
211 int capacity = 1;
212 while (capacity < initialCapacity)
213 capacity <<= 1;
214 table = newTable(capacity);
215 this.loadFactor = loadFactor;
216 threshold = (int)(capacity * loadFactor);
217 }
218
219 /**
220 * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
221 * capacity and the default load factor (0.75).
222 *
223 * @param initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
224 * @throws IllegalArgumentException if the initial capacity is negative
225 */
226 public WeakHashMap(int initialCapacity) {
227 this(initialCapacity, DEFAULT_LOAD_FACTOR);
228 }
229
230 /**
231 * Constructs a new, empty <tt>WeakHashMap</tt> with the default initial
232 * capacity (16) and load factor (0.75).
233 */
234 public WeakHashMap() {
235 this.loadFactor = DEFAULT_LOAD_FACTOR;
236 threshold = DEFAULT_INITIAL_CAPACITY;
237 table = newTable(DEFAULT_INITIAL_CAPACITY);
238 }
239
240 /**
241 * Constructs a new <tt>WeakHashMap</tt> with the same mappings as the
242 * specified map. The <tt>WeakHashMap</tt> is created with the default
243 * load factor (0.75) and an initial capacity sufficient to hold the
244 * mappings in the specified map.
245 *
246 * @param m the map whose mappings are to be placed in this map
247 * @throws NullPointerException if the specified map is null
248 * @since 1.3
249 */
250 public WeakHashMap(Map<? extends K, ? extends V> m) {
251 this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1, 16),
252 DEFAULT_LOAD_FACTOR);
253 putAll(m);
254 }
255
256 // internal utilities
257
258 /**
259 * Value representing null keys inside tables.
260 */
261 private static final Object NULL_KEY = new Object();
262
263 /**
264 * Use NULL_KEY for key if it is null.
265 */
266 private static Object maskNull(Object key) {
267 return (key == null) ? NULL_KEY : key;
268 }
269
270 /**
271 * Returns internal representation of null key back to caller as null.
272 */
273 static Object unmaskNull(Object key) {
274 return (key == NULL_KEY) ? null : key;
275 }
276
277 /**
278 * Checks for equality of non-null reference x and possibly-null y. By
279 * default uses Object.equals.
280 */
281 private static boolean eq(Object x, Object y) {
282 return x == y || x.equals(y);
283 }
284
285 /**
286 * Returns index for hash code h.
287 */
288 private static int indexFor(int h, int length) {
289 return h & (length-1);
290 }
291
292 /**
293 * Expunges stale entries from the table.
294 */
295 private void expungeStaleEntries() {
296 for (Object x; (x = queue.poll()) != null; ) {
297 synchronized (queue) {
298 @SuppressWarnings("unchecked")
299 Entry<K,V> e = (Entry<K,V>) x;
300 int i = indexFor(e.hash, table.length);
301
302 Entry<K,V> prev = table[i];
303 Entry<K,V> p = prev;
304 while (p != null) {
305 Entry<K,V> next = p.next;
354
355 /**
356 * Returns the value to which the specified key is mapped,
357 * or {@code null} if this map contains no mapping for the key.
358 *
359 * <p>More formally, if this map contains a mapping from a key
360 * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
361 * key.equals(k))}, then this method returns {@code v}; otherwise
362 * it returns {@code null}. (There can be at most one such mapping.)
363 *
364 * <p>A return value of {@code null} does not <i>necessarily</i>
365 * indicate that the map contains no mapping for the key; it's also
366 * possible that the map explicitly maps the key to {@code null}.
367 * The {@link #containsKey containsKey} operation may be used to
368 * distinguish these two cases.
369 *
370 * @see #put(Object, Object)
371 */
372 public V get(Object key) {
373 Object k = maskNull(key);
374 int h = HashMap.hash(k.hashCode());
375 Entry<K,V>[] tab = getTable();
376 int index = indexFor(h, tab.length);
377 Entry<K,V> e = tab[index];
378 while (e != null) {
379 if (e.hash == h && eq(k, e.get()))
380 return e.value;
381 e = e.next;
382 }
383 return null;
384 }
385
386 /**
387 * Returns <tt>true</tt> if this map contains a mapping for the
388 * specified key.
389 *
390 * @param key The key whose presence in this map is to be tested
391 * @return <tt>true</tt> if there is a mapping for <tt>key</tt>;
392 * <tt>false</tt> otherwise
393 */
394 public boolean containsKey(Object key) {
395 return getEntry(key) != null;
396 }
397
398 /**
399 * Returns the entry associated with the specified key in this map.
400 * Returns null if the map contains no mapping for this key.
401 */
402 Entry<K,V> getEntry(Object key) {
403 Object k = maskNull(key);
404 int h = HashMap.hash(k.hashCode());
405 Entry<K,V>[] tab = getTable();
406 int index = indexFor(h, tab.length);
407 Entry<K,V> e = tab[index];
408 while (e != null && !(e.hash == h && eq(k, e.get())))
409 e = e.next;
410 return e;
411 }
412
413 /**
414 * Associates the specified value with the specified key in this map.
415 * If the map previously contained a mapping for this key, the old
416 * value is replaced.
417 *
418 * @param key key with which the specified value is to be associated.
419 * @param value value to be associated with the specified key.
420 * @return the previous value associated with <tt>key</tt>, or
421 * <tt>null</tt> if there was no mapping for <tt>key</tt>.
422 * (A <tt>null</tt> return can also indicate that the map
423 * previously associated <tt>null</tt> with <tt>key</tt>.)
424 */
425 public V put(K key, V value) {
426 Object k = maskNull(key);
427 int h = HashMap.hash(k.hashCode());
428 Entry<K,V>[] tab = getTable();
429 int i = indexFor(h, tab.length);
430
431 for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
432 if (h == e.hash && eq(k, e.get())) {
433 V oldValue = e.value;
434 if (value != oldValue)
435 e.value = value;
436 return oldValue;
437 }
438 }
439
440 modCount++;
441 Entry<K,V> e = tab[i];
442 tab[i] = new Entry<>(k, value, queue, h, e);
443 if (++size >= threshold)
444 resize(tab.length * 2);
445 return null;
446 }
447
451 * number of keys in this map reaches its threshold.
452 *
453 * If current capacity is MAXIMUM_CAPACITY, this method does not
454 * resize the map, but sets threshold to Integer.MAX_VALUE.
455 * This has the effect of preventing future calls.
456 *
457 * @param newCapacity the new capacity, MUST be a power of two;
458 * must be greater than current capacity unless current
459 * capacity is MAXIMUM_CAPACITY (in which case value
460 * is irrelevant).
461 */
462 void resize(int newCapacity) {
463 Entry<K,V>[] oldTable = getTable();
464 int oldCapacity = oldTable.length;
465 if (oldCapacity == MAXIMUM_CAPACITY) {
466 threshold = Integer.MAX_VALUE;
467 return;
468 }
469
470 Entry<K,V>[] newTable = newTable(newCapacity);
471 transfer(oldTable, newTable);
472 table = newTable;
473
474 /*
475 * If ignoring null elements and processing ref queue caused massive
476 * shrinkage, then restore old table. This should be rare, but avoids
477 * unbounded expansion of garbage-filled tables.
478 */
479 if (size >= threshold / 2) {
480 threshold = (int)(newCapacity * loadFactor);
481 } else {
482 expungeStaleEntries();
483 transfer(newTable, oldTable);
484 table = oldTable;
485 }
486 }
487
488 /** Transfers all entries from src to dest tables */
489 private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) {
490 for (int j = 0; j < src.length; ++j) {
491 Entry<K,V> e = src[j];
492 src[j] = null;
493 while (e != null) {
494 Entry<K,V> next = e.next;
495 Object key = e.get();
496 if (key == null) {
497 e.next = null; // Help GC
498 e.value = null; // " "
499 size--;
500 } else {
501 int i = indexFor(e.hash, dest.length);
502 e.next = dest[i];
503 dest[i] = e;
504 }
505 e = next;
506 }
507 }
508 }
509
510 /**
511 * Copies all of the mappings from the specified map to this map.
512 * These mappings will replace any mappings that this map had for any
513 * of the keys currently in the specified map.
514 *
515 * @param m mappings to be stored in this map.
516 * @throws NullPointerException if the specified map is null.
517 */
518 public void putAll(Map<? extends K, ? extends V> m) {
519 int numKeysToBeAdded = m.size();
520 if (numKeysToBeAdded == 0)
549 * More formally, if this map contains a mapping from key <tt>k</tt> to
550 * value <tt>v</tt> such that <code>(key==null ? k==null :
551 * key.equals(k))</code>, that mapping is removed. (The map can contain
552 * at most one such mapping.)
553 *
554 * <p>Returns the value to which this map previously associated the key,
555 * or <tt>null</tt> if the map contained no mapping for the key. A
556 * return value of <tt>null</tt> does not <i>necessarily</i> indicate
557 * that the map contained no mapping for the key; it's also possible
558 * that the map explicitly mapped the key to <tt>null</tt>.
559 *
560 * <p>The map will not contain a mapping for the specified key once the
561 * call returns.
562 *
563 * @param key key whose mapping is to be removed from the map
564 * @return the previous value associated with <tt>key</tt>, or
565 * <tt>null</tt> if there was no mapping for <tt>key</tt>
566 */
567 public V remove(Object key) {
568 Object k = maskNull(key);
569 int h = HashMap.hash(k.hashCode());
570 Entry<K,V>[] tab = getTable();
571 int i = indexFor(h, tab.length);
572 Entry<K,V> prev = tab[i];
573 Entry<K,V> e = prev;
574
575 while (e != null) {
576 Entry<K,V> next = e.next;
577 if (h == e.hash && eq(k, e.get())) {
578 modCount++;
579 size--;
580 if (prev == e)
581 tab[i] = next;
582 else
583 prev.next = next;
584 return e.value;
585 }
586 prev = e;
587 e = next;
588 }
589
590 return null;
591 }
592
593 /** Special version of remove needed by Entry set */
594 boolean removeMapping(Object o) {
595 if (!(o instanceof Map.Entry))
596 return false;
597 Entry<K,V>[] tab = getTable();
598 Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
599 Object k = maskNull(entry.getKey());
600 int h = HashMap.hash(k.hashCode());
601 int i = indexFor(h, tab.length);
602 Entry<K,V> prev = tab[i];
603 Entry<K,V> e = prev;
604
605 while (e != null) {
606 Entry<K,V> next = e.next;
607 if (h == e.hash && e.equals(entry)) {
608 modCount++;
609 size--;
610 if (prev == e)
611 tab[i] = next;
612 else
613 prev.next = next;
614 return true;
615 }
616 prev = e;
617 e = next;
618 }
619
620 return false;
662 }
663
664 /**
665 * Special-case code for containsValue with null argument
666 */
667 private boolean containsNullValue() {
668 Entry<K,V>[] tab = getTable();
669 for (int i = tab.length; i-- > 0;)
670 for (Entry<K,V> e = tab[i]; e != null; e = e.next)
671 if (e.value==null)
672 return true;
673 return false;
674 }
675
676 /**
677 * The entries in this hash table extend WeakReference, using its main ref
678 * field as the key.
679 */
680 private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> {
681 V value;
682 final int hash;
683 Entry<K,V> next;
684
685 /**
686 * Creates new entry.
687 */
688 Entry(Object key, V value,
689 ReferenceQueue<Object> queue,
690 int hash, Entry<K,V> next) {
691 super(key, queue);
692 this.value = value;
693 this.hash = hash;
694 this.next = next;
695 }
696
697 @SuppressWarnings("unchecked")
698 public K getKey() {
699 return (K) WeakHashMap.unmaskNull(get());
700 }
701
702 public V getValue() {
|
167 * The load factor for the hash table.
168 */
169 private final float loadFactor;
170
171 /**
172 * Reference queue for cleared WeakEntries
173 */
174 private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
175
176 /**
177 * The number of times this WeakHashMap has been structurally modified.
178 * Structural modifications are those that change the number of
179 * mappings in the map or otherwise modify its internal structure
180 * (e.g., rehash). This field is used to make iterators on
181 * Collection-views of the map fail-fast.
182 *
183 * @see ConcurrentModificationException
184 */
185 int modCount;
186
187 /**
188 * The default threshold of capacity above which alternate hashing is
189 * used. Alternative hashing reduces the incidence of collisions due to
190 * weak hash code calculation.
191 * <p/>
192 * This value may be overridden by defining the system property
193 * {@code java.util.althashing.threshold} to an integer value. A property
194 * value of {@code 1} forces alternative hashing to be used at all times
195 * whereas {@code 2147483648 } ({@code Integer.MAX_VALUE}) value ensures
196 * that alternative hashing is never used.
197 */
198 static final int ALTERNATE_HASHING_THRESHOLD_DEFAULT = 0;
199
200 /**
201 * holds values which can't be initialized until after VM is booted.
202 */
203 private static class Holder {
204
205 /**
206 * Table capacity above which to switch to use alternate hashing.
207 */
208 static final int ALTERNATE_HASHING_THRESHOLD;
209
210 static {
211 String altThreshold = java.security.AccessController.doPrivileged(
212 new sun.security.action.GetPropertyAction(
213 "jdk.map.althashing.threshold"));
214
215 int threshold;
216 try {
217 threshold = (null != altThreshold)
218 ? Integer.parseInt(altThreshold)
219 : ALTERNATE_HASHING_THRESHOLD_DEFAULT;
220
221 if(threshold == -1) {
222 threshold = Integer.MAX_VALUE;
223 }
224
225 if(threshold < 0) {
226 throw new IllegalArgumentException("value must be positive integer.");
227 }
228 } catch(IllegalArgumentException failed) {
229 throw new Error("Illegal value for 'jdk.map.althashing.threshold'", failed);
230 }
231 ALTERNATE_HASHING_THRESHOLD = threshold;
232 }
233 }
234
235 /**
236 * If {@code true} then perform alternate hashing to reduce the incidence of
237 * collisions due to weak hash code calculation.
238 */
239 transient boolean useAltHashing;
240
241 /**
242 * A randomizing value associated with this instance that is applied to
243 * hash code of keys to make hash collisions harder to find.
244 */
245 transient final int hashSeed = sun.misc.Hashing.randomHashSeed(this);
246
247 @SuppressWarnings("unchecked")
248 private Entry<K,V>[] newTable(int n) {
249 return (Entry<K,V>[]) new Entry[n];
250 }
251
252 /**
253 * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
254 * capacity and the given load factor.
255 *
256 * @param initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
257 * @param loadFactor The load factor of the <tt>WeakHashMap</tt>
258 * @throws IllegalArgumentException if the initial capacity is negative,
259 * or if the load factor is nonpositive.
260 */
261 public WeakHashMap(int initialCapacity, float loadFactor) {
262 if (initialCapacity < 0)
263 throw new IllegalArgumentException("Illegal Initial Capacity: "+
264 initialCapacity);
265 if (initialCapacity > MAXIMUM_CAPACITY)
266 initialCapacity = MAXIMUM_CAPACITY;
267
268 if (loadFactor <= 0 || Float.isNaN(loadFactor))
269 throw new IllegalArgumentException("Illegal Load factor: "+
270 loadFactor);
271 int capacity = 1;
272 while (capacity < initialCapacity)
273 capacity <<= 1;
274 table = newTable(capacity);
275 this.loadFactor = loadFactor;
276 threshold = (int)(capacity * loadFactor);
277 useAltHashing = sun.misc.VM.isBooted() &&
278 (capacity >= Holder.ALTERNATE_HASHING_THRESHOLD);
279 }
280
281 /**
282 * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
283 * capacity and the default load factor (0.75).
284 *
285 * @param initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
286 * @throws IllegalArgumentException if the initial capacity is negative
287 */
288 public WeakHashMap(int initialCapacity) {
289 this(initialCapacity, DEFAULT_LOAD_FACTOR);
290 }
291
292 /**
293 * Constructs a new, empty <tt>WeakHashMap</tt> with the default initial
294 * capacity (16) and load factor (0.75).
295 */
296 public WeakHashMap() {
297 this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
298 }
299
300 /**
301 * Constructs a new <tt>WeakHashMap</tt> with the same mappings as the
302 * specified map. The <tt>WeakHashMap</tt> is created with the default
303 * load factor (0.75) and an initial capacity sufficient to hold the
304 * mappings in the specified map.
305 *
306 * @param m the map whose mappings are to be placed in this map
307 * @throws NullPointerException if the specified map is null
308 * @since 1.3
309 */
310 public WeakHashMap(Map<? extends K, ? extends V> m) {
311 this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
312 DEFAULT_INITIAL_CAPACITY),
313 DEFAULT_LOAD_FACTOR);
314 putAll(m);
315 }
316
317 // internal utilities
318
319 /**
320 * Value representing null keys inside tables.
321 */
322 private static final Object NULL_KEY = new Object();
323
324 /**
325 * Use NULL_KEY for key if it is null.
326 */
327 private static Object maskNull(Object key) {
328 return (key == null) ? NULL_KEY : key;
329 }
330
331 /**
332 * Returns internal representation of null key back to caller as null.
333 */
334 static Object unmaskNull(Object key) {
335 return (key == NULL_KEY) ? null : key;
336 }
337
338 /**
339 * Checks for equality of non-null reference x and possibly-null y. By
340 * default uses Object.equals.
341 */
342 private static boolean eq(Object x, Object y) {
343 return x == y || x.equals(y);
344 }
345
346 /**
347 * Retrieve object hash code and applies a supplemental hash function to the
348 * result hash, which defends against poor quality hash functions. This is
349 * critical because HashMap uses power-of-two length hash tables, that
350 * otherwise encounter collisions for hashCodes that do not differ
351 * in lower bits. Note: Null keys always map to hash 0, thus index 0.
352 */
353 int hash(Object k) {
354 if (null == k) {
355 return 0;
356 }
357
358 int h;
359 if (useAltHashing) {
360 h = hashSeed;
361 if (k instanceof String) {
362 return h ^ sun.misc.Hashing.stringHash32((String) k);
363 } else {
364 h ^= k.hashCode();
365 }
366 } else {
367 h = k.hashCode();
368 }
369
370 // This function ensures that hashCodes that differ only by
371 // constant multiples at each bit position have a bounded
372 // number of collisions (approximately 8 at default load factor).
373 h ^= (h >>> 20) ^ (h >>> 12);
374 return h ^ (h >>> 7) ^ (h >>> 4);
375 }
376
377 /**
378 * Returns index for hash code h.
379 */
380 private static int indexFor(int h, int length) {
381 return h & (length-1);
382 }
383
384 /**
385 * Expunges stale entries from the table.
386 */
387 private void expungeStaleEntries() {
388 for (Object x; (x = queue.poll()) != null; ) {
389 synchronized (queue) {
390 @SuppressWarnings("unchecked")
391 Entry<K,V> e = (Entry<K,V>) x;
392 int i = indexFor(e.hash, table.length);
393
394 Entry<K,V> prev = table[i];
395 Entry<K,V> p = prev;
396 while (p != null) {
397 Entry<K,V> next = p.next;
446
447 /**
448 * Returns the value to which the specified key is mapped,
449 * or {@code null} if this map contains no mapping for the key.
450 *
451 * <p>More formally, if this map contains a mapping from a key
452 * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
453 * key.equals(k))}, then this method returns {@code v}; otherwise
454 * it returns {@code null}. (There can be at most one such mapping.)
455 *
456 * <p>A return value of {@code null} does not <i>necessarily</i>
457 * indicate that the map contains no mapping for the key; it's also
458 * possible that the map explicitly maps the key to {@code null}.
459 * The {@link #containsKey containsKey} operation may be used to
460 * distinguish these two cases.
461 *
462 * @see #put(Object, Object)
463 */
464 public V get(Object key) {
465 Object k = maskNull(key);
466 int h = hash(k);
467 Entry<K,V>[] tab = getTable();
468 int index = indexFor(h, tab.length);
469 Entry<K,V> e = tab[index];
470 while (e != null) {
471 if (e.hash == h && eq(k, e.get()))
472 return e.value;
473 e = e.next;
474 }
475 return null;
476 }
477
478 /**
479 * Returns <tt>true</tt> if this map contains a mapping for the
480 * specified key.
481 *
482 * @param key The key whose presence in this map is to be tested
483 * @return <tt>true</tt> if there is a mapping for <tt>key</tt>;
484 * <tt>false</tt> otherwise
485 */
486 public boolean containsKey(Object key) {
487 return getEntry(key) != null;
488 }
489
490 /**
491 * Returns the entry associated with the specified key in this map.
492 * Returns null if the map contains no mapping for this key.
493 */
494 Entry<K,V> getEntry(Object key) {
495 Object k = maskNull(key);
496 int h = hash(k);
497 Entry<K,V>[] tab = getTable();
498 int index = indexFor(h, tab.length);
499 Entry<K,V> e = tab[index];
500 while (e != null && !(e.hash == h && eq(k, e.get())))
501 e = e.next;
502 return e;
503 }
504
505 /**
506 * Associates the specified value with the specified key in this map.
507 * If the map previously contained a mapping for this key, the old
508 * value is replaced.
509 *
510 * @param key key with which the specified value is to be associated.
511 * @param value value to be associated with the specified key.
512 * @return the previous value associated with <tt>key</tt>, or
513 * <tt>null</tt> if there was no mapping for <tt>key</tt>.
514 * (A <tt>null</tt> return can also indicate that the map
515 * previously associated <tt>null</tt> with <tt>key</tt>.)
516 */
517 public V put(K key, V value) {
518 Object k = maskNull(key);
519 int h = hash(k);
520 Entry<K,V>[] tab = getTable();
521 int i = indexFor(h, tab.length);
522
523 for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
524 if (h == e.hash && eq(k, e.get())) {
525 V oldValue = e.value;
526 if (value != oldValue)
527 e.value = value;
528 return oldValue;
529 }
530 }
531
532 modCount++;
533 Entry<K,V> e = tab[i];
534 tab[i] = new Entry<>(k, value, queue, h, e);
535 if (++size >= threshold)
536 resize(tab.length * 2);
537 return null;
538 }
539
543 * number of keys in this map reaches its threshold.
544 *
545 * If current capacity is MAXIMUM_CAPACITY, this method does not
546 * resize the map, but sets threshold to Integer.MAX_VALUE.
547 * This has the effect of preventing future calls.
548 *
549 * @param newCapacity the new capacity, MUST be a power of two;
550 * must be greater than current capacity unless current
551 * capacity is MAXIMUM_CAPACITY (in which case value
552 * is irrelevant).
553 */
554 void resize(int newCapacity) {
555 Entry<K,V>[] oldTable = getTable();
556 int oldCapacity = oldTable.length;
557 if (oldCapacity == MAXIMUM_CAPACITY) {
558 threshold = Integer.MAX_VALUE;
559 return;
560 }
561
562 Entry<K,V>[] newTable = newTable(newCapacity);
563 boolean oldAltHashing = useAltHashing;
564 useAltHashing |= sun.misc.VM.isBooted() &&
565 (newCapacity >= Holder.ALTERNATE_HASHING_THRESHOLD);
566 boolean rehash = oldAltHashing ^ useAltHashing;
567 transfer(oldTable, newTable, rehash);
568 table = newTable;
569
570 /*
571 * If ignoring null elements and processing ref queue caused massive
572 * shrinkage, then restore old table. This should be rare, but avoids
573 * unbounded expansion of garbage-filled tables.
574 */
575 if (size >= threshold / 2) {
576 threshold = (int)(newCapacity * loadFactor);
577 } else {
578 expungeStaleEntries();
579 transfer(newTable, oldTable, false);
580 table = oldTable;
581 }
582 }
583
584 /** Transfers all entries from src to dest tables */
585 private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest, boolean rehash) {
586 for (int j = 0; j < src.length; ++j) {
587 Entry<K,V> e = src[j];
588 src[j] = null;
589 while (e != null) {
590 Entry<K,V> next = e.next;
591 Object key = e.get();
592 if (key == null) {
593 e.next = null; // Help GC
594 e.value = null; // " "
595 size--;
596 } else {
597 if(rehash) {
598 e.hash = hash(key);
599 }
600 int i = indexFor(e.hash, dest.length);
601 e.next = dest[i];
602 dest[i] = e;
603 }
604 e = next;
605 }
606 }
607 }
608
609 /**
610 * Copies all of the mappings from the specified map to this map.
611 * These mappings will replace any mappings that this map had for any
612 * of the keys currently in the specified map.
613 *
614 * @param m mappings to be stored in this map.
615 * @throws NullPointerException if the specified map is null.
616 */
617 public void putAll(Map<? extends K, ? extends V> m) {
618 int numKeysToBeAdded = m.size();
619 if (numKeysToBeAdded == 0)
648 * More formally, if this map contains a mapping from key <tt>k</tt> to
649 * value <tt>v</tt> such that <code>(key==null ? k==null :
650 * key.equals(k))</code>, that mapping is removed. (The map can contain
651 * at most one such mapping.)
652 *
653 * <p>Returns the value to which this map previously associated the key,
654 * or <tt>null</tt> if the map contained no mapping for the key. A
655 * return value of <tt>null</tt> does not <i>necessarily</i> indicate
656 * that the map contained no mapping for the key; it's also possible
657 * that the map explicitly mapped the key to <tt>null</tt>.
658 *
659 * <p>The map will not contain a mapping for the specified key once the
660 * call returns.
661 *
662 * @param key key whose mapping is to be removed from the map
663 * @return the previous value associated with <tt>key</tt>, or
664 * <tt>null</tt> if there was no mapping for <tt>key</tt>
665 */
666 public V remove(Object key) {
667 Object k = maskNull(key);
668 int h = hash(k);
669 Entry<K,V>[] tab = getTable();
670 int i = indexFor(h, tab.length);
671 Entry<K,V> prev = tab[i];
672 Entry<K,V> e = prev;
673
674 while (e != null) {
675 Entry<K,V> next = e.next;
676 if (h == e.hash && eq(k, e.get())) {
677 modCount++;
678 size--;
679 if (prev == e)
680 tab[i] = next;
681 else
682 prev.next = next;
683 return e.value;
684 }
685 prev = e;
686 e = next;
687 }
688
689 return null;
690 }
691
692 /** Special version of remove needed by Entry set */
693 boolean removeMapping(Object o) {
694 if (!(o instanceof Map.Entry))
695 return false;
696 Entry<K,V>[] tab = getTable();
697 Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
698 Object k = maskNull(entry.getKey());
699 int h = hash(k);
700 int i = indexFor(h, tab.length);
701 Entry<K,V> prev = tab[i];
702 Entry<K,V> e = prev;
703
704 while (e != null) {
705 Entry<K,V> next = e.next;
706 if (h == e.hash && e.equals(entry)) {
707 modCount++;
708 size--;
709 if (prev == e)
710 tab[i] = next;
711 else
712 prev.next = next;
713 return true;
714 }
715 prev = e;
716 e = next;
717 }
718
719 return false;
761 }
762
763 /**
764 * Special-case code for containsValue with null argument
765 */
766 private boolean containsNullValue() {
767 Entry<K,V>[] tab = getTable();
768 for (int i = tab.length; i-- > 0;)
769 for (Entry<K,V> e = tab[i]; e != null; e = e.next)
770 if (e.value==null)
771 return true;
772 return false;
773 }
774
775 /**
776 * The entries in this hash table extend WeakReference, using its main ref
777 * field as the key.
778 */
779 private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> {
780 V value;
781 int hash;
782 Entry<K,V> next;
783
784 /**
785 * Creates new entry.
786 */
787 Entry(Object key, V value,
788 ReferenceQueue<Object> queue,
789 int hash, Entry<K,V> next) {
790 super(key, queue);
791 this.value = value;
792 this.hash = hash;
793 this.next = next;
794 }
795
796 @SuppressWarnings("unchecked")
797 public K getKey() {
798 return (K) WeakHashMap.unmaskNull(get());
799 }
800
801 public V getValue() {
|