Package org.apache.xbean.propertyeditor
Class ReferenceIdentityMap
- java.lang.Object
-
- org.apache.xbean.propertyeditor.ReferenceIdentityMap
-
- All Implemented Interfaces:
java.util.Map
public class ReferenceIdentityMap extends java.lang.Object implements java.util.MapStreamlined 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
ReferenceIdentityMapandAbstractReferenceMap.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classReferenceIdentityMap.ReferenceEntryEach entry in the Map is represented with a ReferenceEntry.
-
Field Summary
Fields Modifier and Type Field Description private ReferenceIdentityMap.ReferenceEntry[]dataMap entriesprivate static intDEFAULT_CAPACITYThe default capacity to use.private static floatDEFAULT_LOAD_FACTORThe default load factor to useprivate floatloadFactorLoad factor, normally 0.75private static intMAXIMUM_CAPACITYThe maximum capacity allowedprivate java.lang.ref.ReferenceQueuepurgeQueueReferenceQueue used to eliminate GC'ed entries.private intsizeThe size of the mapprivate intthresholdSize at which to rehash
-
Constructor Summary
Constructors Constructor Description ReferenceIdentityMap()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private intcalculateThreshold(int newCapacity, float factor)Calculates the new threshold of the map, where it will be resized.private voidcheckCapacity()Checks the capacity of the map and enlarges it if necessary.voidclear()Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.booleancontainsKey(java.lang.Object key)Checks whether the map contains the specified key.booleancontainsValue(java.lang.Object value)Checks whether the map contains the specified value.private ReferenceIdentityMap.ReferenceEntrycreateEntry(int index, int hashCode, java.lang.Object key, java.lang.Object value)Creates a new ReferenceEntry.private voidensureCapacity(int newCapacity)Changes the size of the data structure to the capacity proposed.java.util.SetentrySet()java.lang.Objectget(java.lang.Object key)Gets the value mapped to the key specified.private ReferenceIdentityMap.ReferenceEntrygetEntry(java.lang.Object key)Gets the entry mapped to the key specified.private inthash(java.lang.Object key)Gets the hash code for the key specified.private inthashIndex(int hashCode, int dataSize)Gets the index into the data storage for the hashCode specified.booleanisEmpty()Checks whether the map is currently empty.java.util.SetkeySet()private voidpurge()Purges stale mappings from this map.private voidpurge(java.lang.ref.Reference purgedEntry)Purges the specified reference.java.lang.Objectput(java.lang.Object key, java.lang.Object value)Puts a key-value entry into this map.voidputAll(java.util.Map t)java.lang.Objectremove(java.lang.Object key)Removes the specified mapping from this map.private voidremoveEntry(ReferenceIdentityMap.ReferenceEntry entry, int hashIndex, ReferenceIdentityMap.ReferenceEntry previous)Removes an entry from the chain stored in a particular index.intsize()Gets the size of the map.java.util.Collectionvalues()
-
-
-
Field Detail
-
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITY
The default capacity to use. Always use a power of 2!!!- See Also:
- Constant Field Values
-
DEFAULT_LOAD_FACTOR
private static final float DEFAULT_LOAD_FACTOR
The default load factor to use- See Also:
- Constant Field Values
-
MAXIMUM_CAPACITY
private static final int MAXIMUM_CAPACITY
The maximum capacity allowed- See Also:
- Constant Field Values
-
loadFactor
private float loadFactor
Load factor, normally 0.75
-
size
private transient int size
The size of the map
-
data
private transient ReferenceIdentityMap.ReferenceEntry[] data
Map entries
-
threshold
private transient int threshold
Size at which to rehash
-
purgeQueue
private java.lang.ref.ReferenceQueue purgeQueue
ReferenceQueue used to eliminate GC'ed entries.
-
-
Method Detail
-
size
public int size()
Gets the size of the map.- Specified by:
sizein interfacejava.util.Map- Returns:
- the size
-
isEmpty
public boolean isEmpty()
Checks whether the map is currently empty.- Specified by:
isEmptyin interfacejava.util.Map- Returns:
- true if the map is currently size zero
-
containsKey
public boolean containsKey(java.lang.Object key)
Checks whether the map contains the specified key.- Specified by:
containsKeyin interfacejava.util.Map- Parameters:
key- the key to search for- Returns:
- true if the map contains the key
-
containsValue
public boolean containsValue(java.lang.Object value)
Checks whether the map contains the specified value.- Specified by:
containsValuein interfacejava.util.Map- Parameters:
value- the value to search for- Returns:
- true if the map contains the value
-
get
public java.lang.Object get(java.lang.Object key)
Gets the value mapped to the key specified.- Specified by:
getin interfacejava.util.Map- Parameters:
key- the key- Returns:
- the mapped value, null if no match
-
put
public java.lang.Object put(java.lang.Object key, java.lang.Object value)Puts a key-value entry into this map. Neither the key nor the value may be null.- Specified by:
putin interfacejava.util.Map- Parameters:
key- the key to add, must not be nullvalue- the value to add, must not be null- Returns:
- the value previously mapped to this key, null if none
-
remove
public java.lang.Object remove(java.lang.Object key)
Removes the specified mapping from this map.- Specified by:
removein interfacejava.util.Map- Parameters:
key- the mapping to remove- Returns:
- the value mapped to the removed key, null if key not in map
-
clear
public void clear()
Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.- Specified by:
clearin interfacejava.util.Map
-
values
public java.util.Collection values()
- Specified by:
valuesin interfacejava.util.Map
-
putAll
public void putAll(java.util.Map t)
- Specified by:
putAllin interfacejava.util.Map
-
entrySet
public java.util.Set entrySet()
- Specified by:
entrySetin interfacejava.util.Map
-
keySet
public java.util.Set keySet()
- Specified by:
keySetin interfacejava.util.Map
-
getEntry
private ReferenceIdentityMap.ReferenceEntry getEntry(java.lang.Object key)
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, java.lang.Object key, java.lang.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
private int hash(java.lang.Object key)
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
private void purge(java.lang.ref.Reference purgedEntry)
Purges the specified reference.- Parameters:
purgedEntry- the reference to purge
-
-