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

public abstract Class AbstractReferenceMap<K, V>

extends AbstractHashedMap<K, V>
Class Inheritance
Known Direct Subclasses
org.apache.commons.collections4.map.ReferenceIdentityMap, org.apache.commons.collections4.map.ReferenceMap
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, java.lang.ref.Reference, .ReferenceQueue, .SoftReference, .WeakReference, java.util.ArrayList, .Collection, .ConcurrentModificationException, .Iterator, .List, .Map, .NoSuchElementException, .Set, org.apache.commons.collections4.MapIterator, org.apache.commons.collections4.keyvalue.DefaultMapEntry

An abstract implementation of a hash-based map that allows the entries to be removed by the garbage collector.

This class implements all the features necessary for a subclass reference hash-based map. Key-value entries are stored in instances of the ReferenceEntry class which can be overridden and replaced. The iterators can similarly be replaced, without the need to replace the KeySet, EntrySet and Values view classes.

Overridable methods are provided to change the default hashing behaviour, and to change how entries are added to and removed from the map. Hopefully, all you need for unusual subclasses is here.

When you construct an AbstractReferenceMap, 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 keys can be configured to be weak but the values hard, in which case this class will behave like a WeakHashMap. However, you can also specify hard keys and weak values, or any other combination. The default constructor uses hard keys and soft values, providing a memory-sensitive cache.

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

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

This implementation is not synchronized. You can use java.util.Collections#synchronizedMap to provide synchronized access to a ReferenceMap.

Since
3.1 (extracted from ReferenceMap in 3.0)
See Also
java.lang.ref.Reference

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
protected static class
AbstractReferenceMap.ReferenceEntry<K, V>

A MapEntry implementation for the map.

pack-priv static class
AbstractReferenceMap.ReferenceEntrySet<K, V>

EntrySet implementation.

pack-priv static class
pack-priv static class
AbstractReferenceMap.ReferenceKeySet<K>

KeySet implementation.

pack-priv static class
pack-priv static class
AbstractReferenceMap.ReferenceMapIterator<K, V>

The MapIterator implementation.

public static enum
pack-priv static class
AbstractReferenceMap.ReferenceValues<V>

Values implementation.

pack-priv static class
pack-priv static class
AbstractReferenceMap.SoftRef<T>

A soft reference holder.

pack-priv static class
AbstractReferenceMap.WeakRef<T>

A weak reference holder.

Field Summary

Modifier and TypeField and Description
private AbstractReferenceMap.ReferenceStrength
keyType

The reference type for keys.

private boolean
purgeValues

Should the value be automatically purged when the associated key has been collected?

private transient ReferenceQueue<Object>
queue

ReferenceQueue used to eliminate stale mappings.

private AbstractReferenceMap.ReferenceStrength
valueType

The reference type for values.

Inherited from org.apache.commons.collections4.map.AbstractHashedMap:
dataDEFAULT_CAPACITYDEFAULT_LOAD_FACTORDEFAULT_THRESHOLDentrySetGETKEY_INVALIDGETVALUE_INVALIDkeySetloadFactorMAXIMUM_CAPACITYmodCountNO_NEXT_ENTRYNO_PREVIOUS_ENTRYNULLREMOVE_INVALIDSETVALUE_INVALIDsizethresholdvalues

Constructor Summary

AccessConstructor and Description
protected
AbstractReferenceMap()

Constructor used during deserialization.

protected
AbstractReferenceMap(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 ReferenceStrength#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 empty map with the specified reference types, load factor and initial capacity.

Method Summary

Modifier and TypeMethod and Description
public void
public boolean

Returns:

true if the map contains the key
containsKey
(final Object
the key to search for
key
)

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

Implements java.util.Map.containsKey, org.apache.commons.collections4.Get.containsKey.

Checks whether the map contains the specified key.
public boolean

Returns:

true if the map contains the value
containsValue
(final Object
the value to search for
value
)

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

Implements java.util.Map.containsValue, org.apache.commons.collections4.Get.containsValue.

Checks whether the map contains the specified value.
protected AbstractReferenceMap.ReferenceEntry<K, V>

Returns:

the newly created entry
createEntry
(final AbstractHashedMap.HashEntry<K, V>
the next entry in sequence
next
,
final int
the hash code to use
hashCode
,
final K
the key to store
key
,
final V
the value to store
value
)

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

Creates a ReferenceEntry instead of a HashEntry.
protected Iterator<Map.Entry<K, V>>

Returns:

the entrySet iterator
createEntrySetIterator
()

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

Creates an entry set iterator.
protected Iterator<K>

Returns:

the keySet iterator
createKeySetIterator
()

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

Creates an key set iterator.
protected Iterator<V>

Returns:

the values iterator
createValuesIterator
()

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

Creates an values iterator.
protected void
doReadObject(final ObjectInputStream
the input stream
in
)

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

Replaces the superclass method to read the state of this class.
protected void
doWriteObject(final ObjectOutputStream
the output stream
out
)

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

Replaces the superclass method to store the state of this class.
public Set<Map.Entry<K, V>>

Returns:

a set view of this map's entries
entrySet
()

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

Implements java.util.Map.entrySet, org.apache.commons.collections4.Get.entrySet.

Returns a set view of this map's entries.
public V

Returns:

the mapped value, null if no match
get
(final Object
the key
key
)

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

Implements java.util.Map.get, org.apache.commons.collections4.Get.get.

Gets the value mapped to the key specified.
protected AbstractHashedMap.HashEntry<K, V>

Returns:

the entry, null if no match
getEntry
(final Object
the key
key
)

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

Gets the entry mapped to 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
)

Gets the hash code for a MapEntry.

protected void
init()

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

Initialise this subclass during construction, cloning or deserialization.
public boolean

Returns:

true if the map is currently size zero
isEmpty
()

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

Implements java.util.Map.isEmpty, org.apache.commons.collections4.Get.isEmpty.

Checks whether the map is currently empty.
protected boolean

Returns:

true if equal
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.AbstractHashedMap.isEqualKey.

Compares two keys, in internal converted form, to see if they are equal.
protected boolean

Returns:

true if keyType has the specified type
isKeyType
(final AbstractReferenceMap.ReferenceStrength
the type to check against.
type
)

Provided protected read-only access to the key type.

protected boolean

Returns:

true if valueType has the specified type
isValueType
(final AbstractReferenceMap.ReferenceStrength
the type to check against.
type
)

Provided protected read-only access to the value type.

public Set<K>

Returns:

a set view of this map's keys
keySet
()

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

Implements java.util.Map.keySet, org.apache.commons.collections4.Get.keySet.

Returns a set view of this map's keys.
public MapIterator<K, V>

Returns:

a map iterator
mapIterator
()

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

Implements org.apache.commons.collections4.IterableGet.mapIterator.

Gets a MapIterator over the reference map.
protected void
purge()

Purges stale mappings from this map.

protected void
purge(final Reference<?>
the reference to purge
ref
)

Purges the specified reference.

protected void
purgeBeforeRead()

Purges stale mappings from this map before read operations.

protected void
purgeBeforeWrite()

Purges stale mappings from this map before write operations.

public V

Returns:

the value previously mapped to this key, null if none
put
(final K
the key to add, must not be null
key
,
final V
the value to add, must not be null
value
)

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

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

Puts a key-value mapping into this map.
public V

Returns:

the value mapped to the removed key, null if key not in map
remove
(final Object
the mapping to remove
key
)

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

Implements java.util.Map.remove, org.apache.commons.collections4.Get.remove.

Removes the specified mapping from this map.
public int
public Collection<V>

Returns:

a set view of this map's values
values
()

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

Implements java.util.Map.values, org.apache.commons.collections4.Get.values.

Returns a collection view of this map's values.
Inherited from org.apache.commons.collections4.map.AbstractHashedMap:
addEntryaddMappingcalculateNewCapacitycalculateThresholdcheckCapacitycloneconvertKeydestroyEntryensureCapacityentryHashCodeentryKeyentryNextentryValueequalshashhashCodehashIndexisEqualValueputAllremoveEntryremoveMappingreuseEntrytoStringupdateEntry