Package org.apache.xbean.propertyeditor
Class ReferenceIdentityMap
java.lang.Object
org.apache.xbean.propertyeditor.ReferenceIdentityMap
- All Implemented Interfaces:
Map
Streamlined version of a WeakIdentityHashMap. Provides Identity semantics with
Weak References to keys. This allows proxies to be GC'ed when no longer referenced
by clients.
BasicProxymanager.destroyProxy()
need not be invoked when a
proxy is no longer needed. Note that this is not a full Map implementation.
The iteration and collection capabilities of Map have been discarded to keep the
implementation lightweight.
Much of this code was cribbed from the Commons Collection 3.1 implementation of
ReferenceIdentityMap
and AbstractReferenceMap
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Each entry in the Map is represented with a ReferenceEntry. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ReferenceIdentityMap.ReferenceEntry[]
Map entriesprivate static final int
The default capacity to use.private static final float
The default load factor to useprivate float
Load factor, normally 0.75private static final int
The maximum capacity allowedprivate ReferenceQueue
ReferenceQueue used to eliminate GC'ed entries.private int
The size of the mapprivate int
Size at which to rehash -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate int
calculateThreshold
(int newCapacity, float factor) Calculates the new threshold of the map, where it will be resized.private void
Checks the capacity of the map and enlarges it if necessary.void
clear()
Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.boolean
containsKey
(Object key) Checks whether the map contains the specified key.boolean
containsValue
(Object value) Checks whether the map contains the specified value.createEntry
(int index, int hashCode, Object key, Object value) Creates a new ReferenceEntry.private void
ensureCapacity
(int newCapacity) Changes the size of the data structure to the capacity proposed.entrySet()
Gets the value mapped to the key specified.Gets the entry mapped to the key specified.private int
Gets the hash code for the key specified.private int
hashIndex
(int hashCode, int dataSize) Gets the index into the data storage for the hashCode specified.boolean
isEmpty()
Checks whether the map is currently empty.keySet()
private void
purge()
Purges stale mappings from this map.private void
Purges the specified reference.Puts a key-value entry into this map.void
Removes the specified mapping from this map.private void
removeEntry
(ReferenceIdentityMap.ReferenceEntry entry, int hashIndex, ReferenceIdentityMap.ReferenceEntry previous) Removes an entry from the chain stored in a particular index.int
size()
Gets the size of the map.values()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITYThe default capacity to use. Always use a power of 2!!!- See Also:
-
DEFAULT_LOAD_FACTOR
private static final float DEFAULT_LOAD_FACTORThe default load factor to use- See Also:
-
MAXIMUM_CAPACITY
private static final int MAXIMUM_CAPACITYThe maximum capacity allowed- See Also:
-
loadFactor
private float loadFactorLoad factor, normally 0.75 -
size
private transient int sizeThe size of the map -
data
Map entries -
threshold
private transient int thresholdSize at which to rehash -
purgeQueue
ReferenceQueue used to eliminate GC'ed entries.
-
-
Constructor Details
-
ReferenceIdentityMap
public ReferenceIdentityMap()
-
-
Method Details
-
size
public int size()Gets the size of the map. -
isEmpty
public boolean isEmpty()Checks whether the map is currently empty. -
containsKey
Checks whether the map contains the specified key.- Specified by:
containsKey
in interfaceMap
- Parameters:
key
- the key to search for- Returns:
- true if the map contains the key
-
containsValue
Checks whether the map contains the specified value.- Specified by:
containsValue
in interfaceMap
- Parameters:
value
- the value to search for- Returns:
- true if the map contains the value
-
get
Gets the value mapped to the key specified. -
put
Puts a key-value entry into this map. Neither the key nor the value may be null. -
remove
Removes the specified mapping from this map. -
clear
public void clear()Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues. -
values
-
putAll
-
entrySet
-
keySet
-
getEntry
Gets the entry mapped to the key specified.- Parameters:
key
- the key- Returns:
- the entry, null if no match
-
createEntry
private ReferenceIdentityMap.ReferenceEntry createEntry(int index, int hashCode, Object key, Object value) Creates a new ReferenceEntry.- Parameters:
index
- the index into the data maphashCode
- the hash code for the new entrykey
- the key to storevalue
- the value to store- Returns:
- the newly created entry
-
removeEntry
private void removeEntry(ReferenceIdentityMap.ReferenceEntry entry, int hashIndex, ReferenceIdentityMap.ReferenceEntry previous) Removes an entry from the chain stored in a particular index.This implementation removes the entry from the data storage table. The size is not updated.
- Parameters:
entry
- the entry to removehashIndex
- the index into the data structureprevious
- the previous entry in the chain
-
checkCapacity
private void checkCapacity()Checks the capacity of the map and enlarges it if necessary.This implementation uses the threshold to check if the map needs enlarging
-
ensureCapacity
private void ensureCapacity(int newCapacity) Changes the size of the data structure to the capacity proposed.- Parameters:
newCapacity
- the new capacity of the array (a power of two, less or equal to max)
-
calculateThreshold
private int calculateThreshold(int newCapacity, float factor) Calculates the new threshold of the map, where it will be resized. This implementation uses the load factor.- Parameters:
newCapacity
- the new capacityfactor
- the load factor- Returns:
- the new resize threshold
-
hash
Gets the hash code for the key specified.This implementation uses the identity hash code.
- Parameters:
key
- the key to get a hash code for- Returns:
- the hash code
-
hashIndex
private int hashIndex(int hashCode, int dataSize) Gets the index into the data storage for the hashCode specified. This implementation uses the least significant bits of the hashCode.- Parameters:
hashCode
- the hash code to usedataSize
- the size of the data to pick a bucket from- Returns:
- the bucket index
-
purge
private void purge()Purges stale mappings from this map.Note that this method is not synchronized! Special care must be taken if, for instance, you want stale mappings to be removed on a periodic basis by some background thread.
-
purge
Purges the specified reference.- Parameters:
purgedEntry
- the reference to purge
-