public class ReferenceIdentityMap
extends java.lang.Object
implements java.util.Map
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
.
Modifier and Type | Class and Description |
---|---|
private static class |
ReferenceIdentityMap.ReferenceEntry
Each entry in the Map is represented with a ReferenceEntry.
|
Modifier and Type | Field and Description |
---|---|
private ReferenceIdentityMap.ReferenceEntry[] |
data
Map entries
|
private static int |
DEFAULT_CAPACITY
The default capacity to use.
|
private static float |
DEFAULT_LOAD_FACTOR
The default load factor to use
|
private float |
loadFactor
Load factor, normally 0.75
|
private static int |
MAXIMUM_CAPACITY
The maximum capacity allowed
|
private java.lang.ref.ReferenceQueue |
purgeQueue
ReferenceQueue used to eliminate GC'ed entries.
|
private int |
size
The size of the map
|
private int |
threshold
Size at which to rehash
|
Constructor and Description |
---|
ReferenceIdentityMap() |
Modifier and Type | Method and Description |
---|---|
private int |
calculateThreshold(int newCapacity,
float factor)
Calculates the new threshold of the map, where it will be resized.
|
private void |
checkCapacity()
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(java.lang.Object key)
Checks whether the map contains the specified key.
|
boolean |
containsValue(java.lang.Object value)
Checks whether the map contains the specified value.
|
private ReferenceIdentityMap.ReferenceEntry |
createEntry(int index,
int hashCode,
java.lang.Object key,
java.lang.Object value)
Creates a new ReferenceEntry.
|
private void |
ensureCapacity(int newCapacity)
Changes the size of the data structure to the capacity proposed.
|
java.util.Set |
entrySet() |
java.lang.Object |
get(java.lang.Object key)
Gets the value mapped to the key specified.
|
private ReferenceIdentityMap.ReferenceEntry |
getEntry(java.lang.Object key)
Gets the entry mapped to the key specified.
|
private int |
hash(java.lang.Object key)
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.
|
java.util.Set |
keySet() |
private void |
purge()
Purges stale mappings from this map.
|
private void |
purge(java.lang.ref.Reference purgedEntry)
Purges the specified reference.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Puts a key-value entry into this map.
|
void |
putAll(java.util.Map t) |
java.lang.Object |
remove(java.lang.Object key)
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.
|
java.util.Collection |
values() |
private static final int DEFAULT_CAPACITY
private static final float DEFAULT_LOAD_FACTOR
private static final int MAXIMUM_CAPACITY
private float loadFactor
private transient int size
private transient ReferenceIdentityMap.ReferenceEntry[] data
private transient int threshold
private java.lang.ref.ReferenceQueue purgeQueue
public int size()
size
in interface java.util.Map
public boolean isEmpty()
isEmpty
in interface java.util.Map
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map
key
- the key to search forpublic boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map
value
- the value to search forpublic java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
key
- the keypublic java.lang.Object put(java.lang.Object key, java.lang.Object value)
put
in interface java.util.Map
key
- the key to add, must not be nullvalue
- the value to add, must not be nullpublic java.lang.Object remove(java.lang.Object key)
remove
in interface java.util.Map
key
- the mapping to removepublic void clear()
clear
in interface java.util.Map
public java.util.Collection values()
values
in interface java.util.Map
public void putAll(java.util.Map t)
putAll
in interface java.util.Map
public java.util.Set entrySet()
entrySet
in interface java.util.Map
public java.util.Set keySet()
keySet
in interface java.util.Map
private ReferenceIdentityMap.ReferenceEntry getEntry(java.lang.Object key)
key
- the keyprivate ReferenceIdentityMap.ReferenceEntry createEntry(int index, int hashCode, java.lang.Object key, java.lang.Object value)
index
- the index into the data maphashCode
- the hash code for the new entrykey
- the key to storevalue
- the value to storeprivate void removeEntry(ReferenceIdentityMap.ReferenceEntry entry, int hashIndex, ReferenceIdentityMap.ReferenceEntry previous)
This implementation removes the entry from the data storage table. The size is not updated.
entry
- the entry to removehashIndex
- the index into the data structureprevious
- the previous entry in the chainprivate void checkCapacity()
This implementation uses the threshold to check if the map needs enlarging
private void ensureCapacity(int newCapacity)
newCapacity
- the new capacity of the array (a power of two, less or equal to max)private int calculateThreshold(int newCapacity, float factor)
newCapacity
- the new capacityfactor
- the load factorprivate int hash(java.lang.Object key)
This implementation uses the identity hash code.
key
- the key to get a hash code forprivate int hashIndex(int hashCode, int dataSize)
hashCode
- the hash code to usedataSize
- the size of the data to pick a bucket fromprivate void purge()
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.
private void purge(java.lang.ref.Reference purgedEntry)
purgedEntry
- the reference to purge