Class WeakMap<K,V>
- java.lang.Object
-
- com.sun.star.lib.util.WeakMap<K,V>
-
- All Implemented Interfaces:
java.util.Map
public final class WeakMap<K,V> extends java.lang.Object implements java.util.Map
A hash map that holds values of typeWeakReference
.Like
HashMap
, this implementation provides all of the optional map operations, and permits thenull
key.Also like
HashMap
, this implementation is not synchronized. If multiple threads share an instance, and at least one of them executes any modifying operations on theWeakMap
, they have to use external synchronization.Unlike other map implementations,
WeakMap
is asymmetric in thatput
expects the given value to be a plain object that is then wrapped in aWeakReference
, while the occurrences of values in all other methods (containsValue
,entrySet
,equals
,get
,hashCode
,remove
,values
, and also the return value ofput
) expect already wrapped instances ofWeakReference
. That is, afterweakMap.put("key", o)
,weakMap.get("key").equals(o)
does not work as naïvely expected; neither doesweakMap1.putAll(weakMap2)
.At an arbitrary time after the
WeakReference
value of an entry has been cleared by the garbage collector, the entry is automatically removed from the map.Values placed into a
Note that this class does not actually implement the Map interface properly, the type of the return value of the entrySet and values methods is wrong, but the "implements Map" is retained for backward compatibility.WeakMap
may optionally support theDisposeNotifier
interface. For those that do, the associatedWeakReference
wrappers are automatically cleared as soon as the values are disposed.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all mappings from this map.boolean
containsKey(java.lang.Object key)
Returnstrue
if this map contains a mapping for the specified key.boolean
containsValue(java.lang.Object value)
Returnstrue
if this map maps one or more keys to the specified value.java.util.Set
entrySet()
Returns a collection view of the mappings contained in this map.boolean
equals(java.lang.Object o)
java.lang.ref.WeakReference<V>
get(java.lang.Object key)
Returns the value to which the specified key is mapped in this map, ornull
if the map contains no mapping for this key.static <T> T
getValue(java.lang.Object ref)
Returns the referent of aWeakReference
, silently handling anull
argument.int
hashCode()
boolean
isEmpty()
Returnstrue
if this map contains no key–value mappings.java.util.Set<K>
keySet()
Returns a view of the keys contained in this map.java.lang.Object
put(java.lang.Object key, java.lang.Object value)
Associates the specified value with the specified key in this map.void
putAll(java.util.Map m)
Copies all of the mappings from the specified map to this map.java.lang.Object
remove(java.lang.Object key)
Removes the mapping for this key from this map if present.int
size()
Returns the number of key–value mappings in this map.java.util.Collection<java.lang.ref.WeakReference<V>>
values()
Returns a collection view of the values contained in this map.
-
-
-
Method Detail
-
size
public int size()
Returns the number of key–value mappings in this map.This is a non-modifying operation.
-
isEmpty
public boolean isEmpty()
Returnstrue
if this map contains no key–value mappings.This is a non-modifying operation.
-
containsKey
public boolean containsKey(java.lang.Object key)
Returnstrue
if this map contains a mapping for the specified key.This is a non-modifying operation.
-
containsValue
public boolean containsValue(java.lang.Object value)
Returnstrue
if this map maps one or more keys to the specified value.This is a non-modifying operation.
-
get
public java.lang.ref.WeakReference<V> get(java.lang.Object key)
Returns the value to which the specified key is mapped in this map, ornull
if the map contains no mapping for this key.This is a non-modifying operation.
-
put
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
Associates the specified value with the specified key in this map.This is a modifying operation.
- Specified by:
put
in interfacejava.util.Map<K,V>
- Parameters:
key
- the key with which the specified value is to be associatedvalue
- the value to be associated with the specified key. This must be a plain object, which is then wrapped in aWeakReference
.- Returns:
- previous value associated with the specified key, or
null
if there was no mapping for the key
-
remove
public java.lang.Object remove(java.lang.Object key)
Removes the mapping for this key from this map if present.This is a modifying operation.
-
putAll
public void putAll(java.util.Map m)
Copies all of the mappings from the specified map to this map.This is a modifying operation.
-
clear
public void clear()
Removes all mappings from this map.This is a modifying operation.
-
keySet
public java.util.Set<K> keySet()
Returns a view of the keys contained in this map.This is a non-modifying operation.
-
values
public java.util.Collection<java.lang.ref.WeakReference<V>> values()
Returns a collection view of the values contained in this map.This is a non-modifying operation.
-
entrySet
public java.util.Set entrySet()
Returns a collection view of the mappings contained in this map.This is a non-modifying operation.
-
equals
public boolean equals(java.lang.Object o)
-
hashCode
public int hashCode()
-
getValue
public static <T> T getValue(java.lang.Object ref)
Returns the referent of aWeakReference
, silently handling anull
argument.This static method is useful to wrap around the return values of methods like
get
.- Parameters:
ref
- must be either an instance ofWeakReference
ornull
- Returns:
- the referent of the specified
WeakReference
, ornull
ifref
isnull
-
-