com.google.common.collect
Class ReferenceMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by com.google.common.collect.ReferenceMap<K,V>
All Implemented Interfaces:
Serializable, ConcurrentMap<K,V>, Map<K,V>

public final class ReferenceMap<K,V>
extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable

A ConcurrentMap implementation that internally utilizes your choice of strong, soft or weak references for its keys and for its values. As soon as any key or value is reclaimed by the garbage collector, the corresponding entry automatically disappears from the map.

All nine possible combinations of reference types are supported, although using strong keys with strong values provides no benefit over using a Map or ConcurrentMap directly.

Note: because garbage collection happens concurrently to your program, it follows that this map is always subject to concurrent modifications, whether or not the caller exposes it to multiple application threads. The usual caveats about the reliability of methods such as size() and AbstractMap.equals(java.lang.Object) apply; for example, size() may be observed to remain unchanged for a short time after an entry was reclaimed.

This implementation does not permit null keys or values. To determine equality to a key or value, this implementation uses Object.equals(java.lang.Object) for strong references, and identity-based equality for soft and weak references.

Note: new ReferenceMap(WEAK, STRONG) is very nearly a drop-in replacement for WeakHashMap, but improves upon this by using only identity-based equality for keys. When possible, ReferenceMap should be preferred over the JDK collection, for its concurrency and greater flexibility.

Author:
Bob Lee, Kevin Bourrillion
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
ReferenceMap(ReferenceType keyReferenceType, ReferenceType valueReferenceType)
          Constructs an empty instance, using the given reference types for keys and values.
ReferenceMap(ReferenceType keyReferenceType, ReferenceType valueReferenceType, ConcurrentMap<Object,Object> backingMap)
          Constructs an empty instance, using the given backing map and the given reference types for keys and values.
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set<Map.Entry<K,V>> entrySet()
          
 V get(Object key)
           
 boolean isEmpty()
           
 V put(K key, V value)
           
 V putIfAbsent(K key, V value)
           
 V remove(Object key)
           
 boolean remove(Object key, Object value)
           
 V replace(K key, V value)
           
 boolean replace(K key, V oldValue, V newValue)
           
 int size()
           
 
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, keySet, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, keySet, putAll, values
 

Constructor Detail

ReferenceMap

public ReferenceMap(ReferenceType keyReferenceType,
                    ReferenceType valueReferenceType)
Constructs an empty instance, using the given reference types for keys and values.


ReferenceMap

public ReferenceMap(ReferenceType keyReferenceType,
                    ReferenceType valueReferenceType,
                    ConcurrentMap<Object,Object> backingMap)
Constructs an empty instance, using the given backing map and the given reference types for keys and values.

Method Detail

size

public int size()
Specified by:
size in interface Map<K,V>
Overrides:
size in class AbstractMap<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>
Overrides:
isEmpty in class AbstractMap<K,V>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in class AbstractMap<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>
Overrides:
containsValue in class AbstractMap<K,V>

get

public V get(Object key)
Specified by:
get in interface Map<K,V>
Overrides:
get in class AbstractMap<K,V>

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>
Overrides:
put in class AbstractMap<K,V>

putIfAbsent

public V putIfAbsent(K key,
                     V value)
Specified by:
putIfAbsent in interface ConcurrentMap<K,V>

replace

public V replace(K key,
                 V value)
Specified by:
replace in interface ConcurrentMap<K,V>

replace

public boolean replace(K key,
                       V oldValue,
                       V newValue)
Specified by:
replace in interface ConcurrentMap<K,V>

remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>
Overrides:
remove in class AbstractMap<K,V>

remove

public boolean remove(Object key,
                      Object value)
Specified by:
remove in interface ConcurrentMap<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>
Overrides:
clear in class AbstractMap<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()

Note: Regardless of the choice of key and value reference types, an entry in the entry set always has strong references to both key and value. You should avoid any lingering strong references to Entry objects.

Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in class AbstractMap<K,V>