Top Description Fields Constructors Methods
org.apache.commons.collections4.map

public Class ReferenceIdentityMap<K, V>

extends AbstractReferenceMap<K, V>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.lang.ref.Reference

A Map implementation that allows mappings to be removed by the garbage collector and matches keys and values based on == not equals().

When you construct a ReferenceIdentityMap, you can specify what kind of references are used to store the map's keys and values. If non-hard references are used, then the garbage collector can remove mappings if a key or value becomes unreachable, or if the JVM's memory is running low. For information on how the different reference types behave, see Reference.

Different types of references can be specified for keys and values. The default constructor uses hard keys and soft values, providing a memory-sensitive cache.

This map is similar to ReferenceMap. It differs in that keys and values in this class are compared using ==.

This map will violate the detail of various Map and map view contracts. As a general rule, don't compare this map to other maps.

This Map implementation does not allow null elements. Attempting to add a null key or value to the map will raise a NullPointerException.

This implementation is not synchronized. You can use java.util.Collections#synchronizedMap to provide synchronized access to a ReferenceIdentityMap. Remember that synchronization will not stop the garbage collector removing entries.

All the available iterators can be reset back to the start by casting to ResettableIterator and calling reset().

Note that ReferenceIdentityMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using java.util.Collections#synchronizedMap. This class may throw exceptions when accessed by concurrent threads without synchronization.

Since
3.0 (previously in main package v2.1)
See Also
java.lang.ref.Reference

Field Summary

Modifier and TypeField and Description
private static final long
serialVersionUID

Serialization version

Constructor Summary

AccessConstructor and Description
public
ReferenceIdentityMap()

Constructs a new ReferenceIdentityMap that will use hard references to keys and soft references to values.

public
ReferenceIdentityMap(final AbstractReferenceMap.ReferenceStrength
the type of reference to use for keys; must be HARD, SOFT, WEAK
keyType
,
final AbstractReferenceMap.ReferenceStrength
the type of reference to use for values; must be HARD, SOFT, WEAK
valueType
)

Constructs a new ReferenceIdentityMap that will use the specified types of references.

public
ReferenceIdentityMap(final AbstractReferenceMap.ReferenceStrength
the type of reference to use for keys; must be HARD, SOFT, WEAK
keyType
,
final AbstractReferenceMap.ReferenceStrength
the type of reference to use for values; must be HARD, SOFT, WEAK
valueType
,
final boolean
should the value be automatically purged when the key is garbage collected
purgeValues
)

Constructs a new ReferenceIdentityMap that will use the specified types of references.

public
ReferenceIdentityMap(final AbstractReferenceMap.ReferenceStrength
the type of reference to use for keys; must be HARD, SOFT, WEAK
keyType
,
final AbstractReferenceMap.ReferenceStrength
the type of reference to use for values; must be HARD, SOFT, WEAK
valueType
,
final int
the initial capacity for the map
capacity
,
final float
the load factor for the map
loadFactor
)

Constructs a new ReferenceIdentityMap with the specified reference types, load factor and initial capacity.

public
ReferenceIdentityMap(final AbstractReferenceMap.ReferenceStrength
the type of reference to use for keys; must be HARD, SOFT, WEAK
keyType
,
final AbstractReferenceMap.ReferenceStrength
the type of reference to use for values; must be HARD, SOFT, WEAK
valueType
,
final int
the initial capacity for the map
capacity
,
final float
the load factor for the map
loadFactor
,
final boolean
should the value be automatically purged when the key is garbage collected
purgeValues
)

Constructs a new ReferenceIdentityMap with the specified reference types, load factor and initial capacity.

Method Summary

Modifier and TypeMethod and Description
protected int

Returns:

the hash code
hash
(final Object
the key to get a hash code for
key
)

Overrides org.apache.commons.collections4.map.AbstractHashedMap.hash.

Gets the hash code for the key specified.
protected int

Returns:

the hash code, as per the MapEntry specification
hashEntry
(final Object
the key to get a hash code for, may be null
key
,
final Object
the value to get a hash code for, may be null
value
)

Overrides org.apache.commons.collections4.map.AbstractReferenceMap.hashEntry.

Gets the hash code for a MapEntry.
protected boolean

Returns:

true if equal by identity
isEqualKey
(final Object
the first key to compare passed in from outside
key1
,
Object
the second key extracted from the entry via entry.key
key2
)

Overrides org.apache.commons.collections4.map.AbstractReferenceMap.isEqualKey.

Compares two keys for equals.
protected boolean

Returns:

true if equal by identity
isEqualValue
(final Object
the first value to compare passed in from outside
value1
,
final Object
the second value extracted from the entry via getValue()
value2
)

Overrides org.apache.commons.collections4.map.AbstractHashedMap.isEqualValue.

Compares two values for equals.
private void
readObject(final ObjectInputStream
the input stream
in
)

Read the map in using a custom routine.

private void
writeObject(final ObjectOutputStream
the output stream
out
)

Write the map out using a custom routine.

Inherited from org.apache.commons.collections4.map.AbstractReferenceMap:
clearcontainsKeycontainsValuecreateEntrycreateEntrySetIteratorcreateKeySetIteratorcreateValuesIteratordoReadObjectdoWriteObjectentrySetgetgetEntryinitisEmptyisKeyTypeisValueTypekeySetmapIteratorpurgepurgepurgeBeforeReadpurgeBeforeWriteputremovesizevalues

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

Constructor Detail

ReferenceIdentityMapback to summary
public ReferenceIdentityMap()

Constructs a new ReferenceIdentityMap that will use hard references to keys and soft references to values.

ReferenceIdentityMapback to summary
public ReferenceIdentityMap(final AbstractReferenceMap.ReferenceStrength keyType, final AbstractReferenceMap.ReferenceStrength valueType)

Constructs a new ReferenceIdentityMap that will use the specified types of references.

Parameters
keyType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for keys; must be HARD, SOFT, WEAK

valueType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for values; must be HARD, SOFT, WEAK

ReferenceIdentityMapback to summary
public ReferenceIdentityMap(final AbstractReferenceMap.ReferenceStrength keyType, final AbstractReferenceMap.ReferenceStrength valueType, final boolean purgeValues)

Constructs a new ReferenceIdentityMap that will use the specified types of references.

Parameters
keyType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for keys; must be HARD, SOFT, WEAK

valueType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for values; must be HARD, SOFT, WEAK

purgeValues:boolean

should the value be automatically purged when the key is garbage collected

ReferenceIdentityMapback to summary
public ReferenceIdentityMap(final AbstractReferenceMap.ReferenceStrength keyType, final AbstractReferenceMap.ReferenceStrength valueType, final int capacity, final float loadFactor)

Constructs a new ReferenceIdentityMap with the specified reference types, load factor and initial capacity.

Parameters
keyType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for keys; must be HARD, SOFT, WEAK

valueType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for values; must be HARD, SOFT, WEAK

capacity:int

the initial capacity for the map

loadFactor:float

the load factor for the map

ReferenceIdentityMapback to summary
public ReferenceIdentityMap(final AbstractReferenceMap.ReferenceStrength keyType, final AbstractReferenceMap.ReferenceStrength valueType, final int capacity, final float loadFactor, final boolean purgeValues)

Constructs a new ReferenceIdentityMap with the specified reference types, load factor and initial capacity.

Parameters
keyType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for keys; must be HARD, SOFT, WEAK

valueType:AbstractReferenceMap.ReferenceStrength

the type of reference to use for values; must be HARD, SOFT, WEAK

capacity:int

the initial capacity for the map

loadFactor:float

the load factor for the map

purgeValues:boolean

should the value be automatically purged when the key is garbage collected

Method Detail

hashback to summary
protected int hash(final Object key)

Overrides org.apache.commons.collections4.map.AbstractHashedMap.hash.

Gets the hash code for the key specified.

This implementation uses the identity hash code.

Parameters
key:Object

the key to get a hash code for

Returns:int

the hash code

Annotations
@Override
hashEntryback to summary
protected int hashEntry(final Object key, final Object value)

Overrides org.apache.commons.collections4.map.AbstractReferenceMap.hashEntry.

Gets the hash code for a MapEntry.

This implementation uses the identity hash code.

Parameters
key:Object

the key to get a hash code for, may be null

value:Object

the value to get a hash code for, may be null

Returns:int

the hash code, as per the MapEntry specification

Annotations
@Override
isEqualKeyback to summary
protected boolean isEqualKey(final Object key1, Object key2)

Overrides org.apache.commons.collections4.map.AbstractReferenceMap.isEqualKey.

Compares two keys for equals.

This implementation converts the key from the entry to a real reference before comparison and uses ==.

Parameters
key1:Object

the first key to compare passed in from outside

key2:Object

the second key extracted from the entry via entry.key

Returns:boolean

true if equal by identity

Annotations
@Override
isEqualValueback to summary
protected boolean isEqualValue(final Object value1, final Object value2)

Overrides org.apache.commons.collections4.map.AbstractHashedMap.isEqualValue.

Compares two values for equals.

This implementation uses ==.

Parameters
value1:Object

the first value to compare passed in from outside

value2:Object

the second value extracted from the entry via getValue()

Returns:boolean

true if equal by identity

Annotations
@Override
readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException

Read the map in using a custom routine.

Parameters
in:ObjectInputStream

the input stream

Exceptions
IOException:
if an error occurs while reading from the stream
ClassNotFoundException:
if an object read from the stream can not be loaded
writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Write the map out using a custom routine.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
if an error occurs while writing to the stream