Package com.pnfsoftware.jeb.util.collect
Class WeakIdentityHashMap<K,V>
java.lang.Object
com.pnfsoftware.jeb.util.collect.WeakIdentityHashMap<K,V>
- Type Parameters:
K- key typeV- value type
A pseudo map where keys are compared using identity comparison (like
IdentityHashMap) but
where the presence of an object as a key in the map does not prevent it being garbage collected
(like WeakHashMap). This class does not implement the Map interface because it is
difficult to ensure correct semantics for iterators over the entrySet(). This map does not
support null keys.
Because we do not implement Map, we do not copy the questionable interface where you can call get(k) or remove(k) for any type of k, which of course can only have an effect if k is of type K.
Implementation note: The approach is to wrap each key in a WeakReference and use the wrapped value as a key in an ordinary HashMap. The WeakReference has to be a subclass IdentityWeakReference (IWR) where two IWRs are equal if they refer to the same object. This enables us to find the entry again.
Forked from https://android.googlesource.com/platform/libcore/+/42cdd0bf7251074df8bf6dfa7f5d62a01c0a1ef6/ojluni/src/main/java/com/sun/jmx/mbeanserver/WeakIdentityHashMap.java
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a weak identity hash map.WeakIdentityHashMap(int initialCapacity) Create a weak identity hash map.WeakIdentityHashMap(int initialCapacity, float loadFactor) Create a weak identity hash map. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Remove all mappings.entrySet()Retrieve live entries.voidexpunge()Remove entries whose weak keys were collected.Retrieve a value.booleanisEmpty()keySet()Retrieve live keys.Add or replace a mapping.Add or replace a mapping without rejecting null keys.Remove a mapping.intsize()toString()values()Retrieve values.
-
Constructor Details
-
WeakIdentityHashMap
public WeakIdentityHashMap(int initialCapacity, float loadFactor) Create a weak identity hash map.- Parameters:
initialCapacity- initial capacityloadFactor- load factor
-
WeakIdentityHashMap
public WeakIdentityHashMap(int initialCapacity) Create a weak identity hash map.- Parameters:
initialCapacity- initial capacity
-
WeakIdentityHashMap
public WeakIdentityHashMap()Create a weak identity hash map.
-
-
Method Details
-
get
Retrieve a value.- Parameters:
key- key compared by identity- Returns:
- mapped value, or null
-
keySet
Retrieve live keys.- Returns:
- copy of the live key set
-
size
public int size()- Returns:
- number of live entries
-
isEmpty
public boolean isEmpty()- Returns:
- true if no live entry exists
-
values
Retrieve values.- Returns:
- collection of values
-
entrySet
Retrieve live entries.- Returns:
- copy of the live entry set
-
put
Add or replace a mapping.- Parameters:
key- non-null key compared by identityvalue- value- Returns:
- previous value, or null
-
putUnsafe
Add or replace a mapping without rejecting null keys.- Parameters:
key- key compared by identityvalue- value- Returns:
- previous value, or null
-
remove
Remove a mapping.- Parameters:
key- key compared by identity- Returns:
- removed value, or null
-
clear
public void clear()Remove all mappings. -
toString
-
expunge
public void expunge()Remove entries whose weak keys were collected.
-