Top Description Inners Fields Constructors Methods
com.sun.star.lib.util

public final Class WeakMap<K, V>

extends Object
implements Map
Class Inheritance
All Implemented Interfaces
java.util.Map
Imports
java.lang.ref.ReferenceQueue, .WeakReference, java.util.Collection, .HashMap, .Iterator, .Map, .Set

A hash map that holds values of type WeakReference.

Like HashMap, this implementation provides all of the optional map operations, and permits the null 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 the WeakMap, they have to use external synchronization.

Unlike other map implementations, WeakMap is asymmetric in that put expects the given value to be a plain object that is then wrapped in a WeakReference, while the occurrences of values in all other methods (containsValue, entrySet, equals, get, hashCode, remove, values, and also the return value of put) expect already wrapped instances of WeakReference. That is, after weakMap.put("key", o), weakMap.get("key").equals(o) does not work as naïvely expected; neither does weakMap1.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 WeakMap may optionally support the DisposeNotifier interface. For those that do, the associated WeakReference wrappers are automatically cleared as soon as the values are disposed.

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final HashMap<K, WeakReference<V>>
map

Declare the map as WeakReference instead of Entry because it makes the return type signatures of values() and keySet() cleaner.

private final ReferenceQueue<V>

Constructor Summary

AccessConstructor and Description
public
WeakMap()

Constructs an empty WeakMap.

public
WeakMap(Map<K, V>
the map whose mappings are to be placed in this map
m
)

Constructs a new WeakMap with the same mappings as the specified Map.

Method Summary

Modifier and TypeMethod and Description
private void
cleanUp()

cleanUp() must only be called from within modifying methods.

public void
clear()

Implements java.util.Map.clear.

Removes all mappings from this map.

public boolean

Returns:

true if this map contains a mapping for the specified key
containsKey
(Object
the key whose presence in this map is to be tested
key
)

Implements java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key.

public boolean

Returns:

true if this map maps one or more keys to the specified value
containsValue
(Object
the value whose presence in this map is to be tested
value
)

Implements java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value.

public Set<E>

Returns:

a collection view of the mappings contained in this map
entrySet
()

Implements java.util.Map.entrySet.

Returns a collection view of the mappings contained in this map.

public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Compares the specified object with this map for equality.

public WeakReference<V>

Returns:

the value to which this map maps the specified key, or null if the map contains no mapping for this key
get
(Object
the key whose associated value is to be returned
key
)

Implements java.util.Map.get.

Returns the value to which the specified key is mapped in this map, or null if the map contains no mapping for this key.

public static <T> T

Returns:

the referent of the specified WeakReference, or null if ref is null
getValue
(Object
must be either an instance of WeakReference or null
ref
)

Returns the referent of a WeakReference, silently handling a null argument.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Returns the hash code value for this map.

public boolean

Returns:

true if this map contains no key–value mappings
isEmpty
()

Implements java.util.Map.isEmpty.

Returns true if this map contains no key–value mappings.

public Set<K>

Returns:

a set view of the keys contained in this map
keySet
()

Implements java.util.Map.keySet.

Returns a view of the keys contained in this map.

public Object

Returns:

previous value associated with the specified key, or null if there was no mapping for the key
put
(Object
the key with which the specified value is to be associated
key
,
Object
the value to be associated with the specified key. This must be a plain object, which is then wrapped in a WeakReference.
value
)

Implements java.util.Map.put.

Associates the specified value with the specified key in this map.

public void
putAll(Map<K, V>
mappings to be stored in this map. The values of those mappings must be plain objects, which are then wrapped in instances of WeakReference.
m
)

Implements java.util.Map.putAll.

Copies all of the mappings from the specified map to this map.

public Object

Returns:

previous value associated with the specified key, or null if there was no mapping for the key
remove
(Object
the key whose mapping is to be removed from the map
key
)

Implements java.util.Map.remove.

Removes the mapping for this key from this map if present.

public int

Returns:

the number of key–value mappings in this map
size
()

Implements java.util.Map.size.

Returns the number of key–value mappings in this map.

public Collection<WeakReference<V>>

Returns:

a collection view of the values contained in this map
values
()

Implements java.util.Map.values.

Returns a collection view of the values contained in this map.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

mapback to summary
private final HashMap<K, WeakReference<V>> map

Declare the map as WeakReference instead of Entry because it makes the return type signatures of values() and keySet() cleaner.

queueback to summary
private final ReferenceQueue<V> queue

Constructor Detail

WeakMapback to summary
public WeakMap()

Constructs an empty WeakMap.

WeakMapback to summary
public WeakMap(Map<K, V> m)

Constructs a new WeakMap with the same mappings as the specified Map.

Parameters
m:Map<K, V>

the map whose mappings are to be placed in this map

Method Detail

cleanUpback to summary
private void cleanUp()

cleanUp() must only be called from within modifying methods. Otherwise, the implementations of entrySet, keySet and values would break (Specifically, iterating over the collections returned by those methods), as non-modifying methods might modify the underlying map.

Annotations
@SuppressWarnings:unchecked
clearback to summary
public void clear()

Implements java.util.Map.clear.

Removes all mappings from this map.

This is a modifying operation.

containsKeyback to summary
public boolean containsKey(Object key)

Implements java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key.

This is a non-modifying operation.

Parameters
key:Object

the key whose presence in this map is to be tested

Returns:boolean

true if this map contains a mapping for the specified key

containsValueback to summary
public boolean containsValue(Object value)

Implements java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value.

This is a non-modifying operation.

Parameters
value:Object

the value whose presence in this map is to be tested

Returns:boolean

true if this map maps one or more keys to the specified value

entrySetback to summary
public Set<E> entrySet()

Implements java.util.Map.entrySet.

Returns a collection view of the mappings contained in this map.

This is a non-modifying operation.

Returns:Set<E>

a collection view of the mappings contained in this map

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Doc from java.util.Map.equals.

Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.

Annotations
@Override
getback to summary
public WeakReference<V> get(Object key)

Implements java.util.Map.get.

Returns the value to which the specified key is mapped in this map, or null if the map contains no mapping for this key.

This is a non-modifying operation.

Parameters
key:Object

the key whose associated value is to be returned

Returns:WeakReference<V>

the value to which this map maps the specified key, or null if the map contains no mapping for this key

getValueback to summary
public static <T> T getValue(Object ref)

Returns the referent of a WeakReference, silently handling a null argument.

This static method is useful to wrap around the return values of methods like get.

Parameters
ref:Object

must be either an instance of WeakReference or null

Returns:T

the referent of the specified WeakReference, or null if ref is null

Annotations
@SuppressWarnings:unchecked
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Doc from java.util.Map.hashCode.

Returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry in the map's entrySet() view. This ensures that m1.equals(m2) implies that m1.hashCode()==m2.hashCode() for any two maps m1 and m2, as required by the general contract of Object#hashCode.

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Implements java.util.Map.isEmpty.

Returns true if this map contains no key–value mappings.

This is a non-modifying operation.

Returns:boolean

true if this map contains no key–value mappings

keySetback to summary
public Set<K> keySet()

Implements java.util.Map.keySet.

Returns a view of the keys contained in this map.

This is a non-modifying operation.

Returns:Set<K>

a set view of the keys contained in this map

putback to summary
public Object put(Object key, Object value)

Implements java.util.Map.put.

Associates the specified value with the specified key in this map.

This is a modifying operation.

Parameters
key:Object

the key with which the specified value is to be associated

value:Object

the value to be associated with the specified key. This must be a plain object, which is then wrapped in a WeakReference.

Returns:Object

previous value associated with the specified key, or null if there was no mapping for the key

Annotations
@SuppressWarnings:unchecked
putAllback to summary
public void putAll(Map<K, V> m)

Implements java.util.Map.putAll.

Copies all of the mappings from the specified map to this map.

This is a modifying operation.

Parameters
m:Map<K, V>

mappings to be stored in this map. The values of those mappings must be plain objects, which are then wrapped in instances of WeakReference.

Annotations
@SuppressWarnings:unchecked
removeback to summary
public Object remove(Object key)

Implements java.util.Map.remove.

Removes the mapping for this key from this map if present.

This is a modifying operation.

Parameters
key:Object

the key whose mapping is to be removed from the map

Returns:Object

previous value associated with the specified key, or null if there was no mapping for the key

sizeback to summary
public int size()

Implements java.util.Map.size.

Returns the number of key–value mappings in this map.

This is a non-modifying operation.

Returns:int

the number of key–value mappings in this map

valuesback to summary
public Collection<WeakReference<V>> values()

Implements java.util.Map.values.

Returns a collection view of the values contained in this map.

This is a non-modifying operation.

Returns:Collection<WeakReference<V>>

a collection view of the values contained in this map

com.sun.star.lib.util back to summary

private final Class WeakMap.Entry<K, V>

extends WeakReference<V>
implements DisposeListener
Class Inheritance
All Implemented Interfaces
com.sun.star.lib.util.DisposeListener

Field Summary

Modifier and TypeField and Description
private final K

Constructor Summary

AccessConstructor and Description
private
Entry(K key, V value, ReferenceQueue<V> queue)

Method Summary

Modifier and TypeMethod and Description
public void
notifyDispose(DisposeNotifier source)

Implements com.sun.star.lib.util.DisposeListener.notifyDispose.

Callback fired by a DisposeNotifier once it is disposed.

Field Detail

keyback to summary
private final K key

Constructor Detail

Entryback to summary
private Entry(K key, V value, ReferenceQueue<V> queue)

Method Detail

notifyDisposeback to summary
public void notifyDispose(DisposeNotifier source)

Implements com.sun.star.lib.util.DisposeListener.notifyDispose.

Doc from com.sun.star.lib.util.DisposeListener.notifyDispose.

Callback fired by a DisposeNotifier once it is disposed.

See Also
DisposeListener#notifyDispose(DisposeNotifier)