|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractMap<K,V>
com.google.common.collect.ReferenceMap<K,V>
public final class ReferenceMap<K,V>
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. This implementation does not permit
null keys or values.
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 Map.equals(java.lang.Object)
apply; for example, size()
may be
observed to remain unchanged for a short time after an entry was reclaimed.
To determine equality to a key, this implementation uses
Object.equals(java.lang.Object)
for strong references, and identity-based equality for
soft and weak references. In other words, for a map with weak or soft key
references, get(java.lang.Object)
returns null
when passed an object that
equals a map key, but isn't the same instance. This behavior is similar to
the way IdentityHashMap
handles key lookups. However, to determine
value equality, as occurs when containsValue(java.lang.Object)
is called, the
ReferenceMap
always uses equals
, regardless of the value
reference type.
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.
Though this class implements Serializable
, serializing reference
maps with weak or soft references leads to unpredictable results.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.util.AbstractMap |
---|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V> |
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()
Removes all of the mappings from this map. |
boolean |
containsKey(Object key)
Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue(Object value)
Returns true if this map maps one or more keys to the specified
value. |
Set<Map.Entry<K,V>> |
entrySet()
|
V |
get(Object key)
Returns the value to which the specified key is mapped, or null if
this map contains no mapping for the key. |
boolean |
isEmpty()
Returns true if this map contains no key-value mappings. |
V |
put(K key,
V value)
Associates the specified value with the specified key in this map. |
V |
putIfAbsent(K key,
V value)
|
V |
remove(Object key)
Removes the mapping for a key from this map if it is present. |
boolean |
remove(Object key,
Object value)
|
V |
replace(K key,
V value)
|
boolean |
replace(K key,
V oldValue,
V newValue)
|
int |
size()
Returns the number of key-value mappings in this map. |
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 |
---|
public ReferenceMap(ReferenceType keyReferenceType, ReferenceType valueReferenceType)
public ReferenceMap(ReferenceType keyReferenceType, ReferenceType valueReferenceType, ConcurrentMap<Object,Object> backingMap)
Method Detail |
---|
public int size()
size
in interface Map<K,V>
size
in class AbstractMap<K,V>
public boolean isEmpty()
true
if this map contains no key-value mappings.
isEmpty
in interface Map<K,V>
isEmpty
in class AbstractMap<K,V>
public boolean containsKey(Object key)
true
if this map contains a mapping for the specified key.
containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
public boolean containsValue(Object value)
true
if this map maps one or more keys to the specified
value.
containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
public V get(Object key)
null
if
this map contains no mapping for the key.
get
in interface Map<K,V>
get
in class AbstractMap<K,V>
public V put(K key, V value)
put
in interface Map<K,V>
put
in class AbstractMap<K,V>
public V putIfAbsent(K key, V value)
putIfAbsent
in interface ConcurrentMap<K,V>
public V replace(K key, V value)
replace
in interface ConcurrentMap<K,V>
public boolean replace(K key, V oldValue, V newValue)
replace
in interface ConcurrentMap<K,V>
public V remove(Object key)
remove
in interface Map<K,V>
remove
in class AbstractMap<K,V>
public boolean remove(Object key, Object value)
remove
in interface ConcurrentMap<K,V>
public void clear()
clear
in interface Map<K,V>
clear
in class AbstractMap<K,V>
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.
entrySet
in interface Map<K,V>
entrySet
in class AbstractMap<K,V>
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |