353 |
* |
* |
354 |
* @author Jochen Hoenicke |
* @author Jochen Hoenicke |
355 |
*/ |
*/ |
356 |
private static class WeakBucket extends WeakReference |
private static class WeakBucket<K, V> extends WeakReference<K> |
357 |
{ |
{ |
358 |
/** |
/** |
359 |
* The value of this entry. The key is stored in the weak |
* The value of this entry. The key is stored in the weak |
360 |
* reference that we extend. |
* reference that we extend. |
361 |
*/ |
*/ |
362 |
Object value; |
V value; |
363 |
|
|
364 |
/** |
/** |
365 |
* The next bucket describing another entry that uses the same |
* The next bucket describing another entry that uses the same |
366 |
* slot. |
* slot. |
367 |
*/ |
*/ |
368 |
WeakBucket next; |
WeakBucket<K, V> next; |
369 |
|
|
370 |
/** |
/** |
371 |
* The slot of this entry. This should be |
* The slot of this entry. This should be |
388 |
* @param slot the slot. This must match the slot where this bucket |
* @param slot the slot. This must match the slot where this bucket |
389 |
* will be enqueued. |
* will be enqueued. |
390 |
*/ |
*/ |
391 |
public WeakBucket(Object key, ReferenceQueue queue, Object value, |
public WeakBucket(K key, ReferenceQueue queue, V value, |
392 |
int slot) |
int slot) |
393 |
{ |
{ |
394 |
super(key, queue); |
super(key, queue); |
401 |
* current bucket. It also keeps a strong reference to the |
* current bucket. It also keeps a strong reference to the |
402 |
* key; bad things may happen otherwise. |
* key; bad things may happen otherwise. |
403 |
*/ |
*/ |
404 |
class WeakEntry implements Map.Entry |
class WeakEntry implements Map.Entry<K, V> |
405 |
{ |
{ |
406 |
/** |
/** |
407 |
* The strong ref to the key. |
* The strong ref to the key. |
408 |
*/ |
*/ |
409 |
Object key; |
K key; |
410 |
|
|
411 |
/** |
/** |
412 |
* Creates a new entry for the key. |
* Creates a new entry for the key. |
413 |
* @param key the key |
* @param key the key |
414 |
*/ |
*/ |
415 |
public WeakEntry(Object key) |
public WeakEntry(K key) |
416 |
{ |
{ |
417 |
this.key = key; |
this.key = key; |
418 |
} |
} |
430 |
* Returns the key. |
* Returns the key. |
431 |
* @return the key |
* @return the key |
432 |
*/ |
*/ |
433 |
public Object getKey() |
public K getKey() |
434 |
{ |
{ |
435 |
return key == NULL_KEY ? null : key; |
return key == NULL_KEY ? null : key; |
436 |
} |
} |
439 |
* Returns the value. |
* Returns the value. |
440 |
* @return the value |
* @return the value |
441 |
*/ |
*/ |
442 |
public Object getValue() |
public V getValue() |
443 |
{ |
{ |
444 |
return value; |
return value; |
445 |
} |
} |
450 |
* @param newVal the new value |
* @param newVal the new value |
451 |
* @return the old value |
* @return the old value |
452 |
*/ |
*/ |
453 |
public Object setValue(Object newVal) |
public V setValue(V newVal) |
454 |
{ |
{ |
455 |
Object oldVal = value; |
V oldVal = value; |
456 |
value = newVal; |
value = newVal; |
457 |
return oldVal; |
return oldVal; |
458 |
} |
} |
495 |
*/ |
*/ |
496 |
WeakEntry getEntry() |
WeakEntry getEntry() |
497 |
{ |
{ |
498 |
final Object key = this.get(); |
final K key = this.get(); |
499 |
if (key == null) |
if (key == null) |
500 |
return null; |
return null; |
501 |
return new WeakEntry(key); |
return new WeakEntry(key); |
762 |
public V get(Object key) |
public V get(Object key) |
763 |
{ |
{ |
764 |
cleanQueue(); |
cleanQueue(); |
765 |
WeakBucket.WeakEntry entry = internalGet(key); |
WeakBucket<K, V>.WeakEntry entry = internalGet(key); |
766 |
return entry == null ? null : entry.getValue(); |
return entry == null ? null : entry.getValue(); |
767 |
} |
} |
768 |
|
|
774 |
* null if the key wasn't in this map, or if the mapped value |
* null if the key wasn't in this map, or if the mapped value |
775 |
* was explicitly set to null. |
* was explicitly set to null. |
776 |
*/ |
*/ |
777 |
public Object put(K key, V value) |
public V put(K key, V value) |
778 |
{ |
{ |
779 |
cleanQueue(); |
cleanQueue(); |
780 |
WeakBucket.WeakEntry entry = internalGet(key); |
WeakBucket<K, V>.WeakEntry entry = internalGet(key); |
781 |
if (entry != null) |
if (entry != null) |
782 |
return entry.setValue(value); |
return entry.setValue(value); |
783 |
|
|
799 |
public V remove(Object key) |
public V remove(Object key) |
800 |
{ |
{ |
801 |
cleanQueue(); |
cleanQueue(); |
802 |
WeakBucket.WeakEntry entry = internalGet(key); |
WeakBucket<K, V>.WeakEntry entry = internalGet(key); |
803 |
if (entry == null) |
if (entry == null) |
804 |
return null; |
return null; |
805 |
|
|