Class AbstractHashedMap<K,V>
- java.lang.Object
- 
- java.util.AbstractMap<K,V>
- 
- org.apache.openjpa.lib.util.collections.AbstractHashedMap<K,V>
 
 
- 
- Type Parameters:
- K- the type of the keys in this map
- V- the type of the values in this map
 - All Implemented Interfaces:
- java.util.Map<K,V>,- IterableMap<K,V>
 - Direct Known Subclasses:
- AbstractLinkedMap,- AbstractReferenceMap
 
 public class AbstractHashedMap<K,V> extends java.util.AbstractMap<K,V> implements IterableMap<K,V> An abstract implementation of a hash-based map which provides numerous points for subclasses to override.This class implements all the features necessary for a subclass hash-based map. Key-value entries are stored in instances of the HashEntryclass, 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. NOTE: From Commons Collections 3.1 this class extends AbstractMap. This is to provide backwards compatibility for ReferenceMap between v3.0 and v3.1. This extends clause will be removed in v5.0. - Since:
- 3.0
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description protected static classAbstractHashedMap.EntrySet<K,V>EntrySet implementation.protected static classAbstractHashedMap.EntrySetIterator<K,V>EntrySet iterator.protected static classAbstractHashedMap.HashEntry<K,V>HashEntry used to store the data.protected static classAbstractHashedMap.HashIterator<K,V>Base Iteratorprotected static classAbstractHashedMap.HashMapIterator<K,V>MapIterator implementation.protected static classAbstractHashedMap.KeySet<K>KeySet implementation.protected static classAbstractHashedMap.KeySetIterator<K>KeySet iterator.protected static classAbstractHashedMap.Values<V>Values implementation.protected static classAbstractHashedMap.ValuesIterator<V>Values iterator.
 - 
Field SummaryFields Modifier and Type Field Description protected static intDEFAULT_CAPACITYThe default capacity to useprotected static floatDEFAULT_LOAD_FACTORThe default load factor to useprotected static intDEFAULT_THRESHOLDThe default threshold to useprotected static java.lang.StringGETKEY_INVALIDprotected static java.lang.StringGETVALUE_INVALIDprotected static intMAXIMUM_CAPACITYThe maximum capacity allowedprotected static java.lang.StringNO_NEXT_ENTRYprotected static java.lang.StringNO_PREVIOUS_ENTRYprotected static java.lang.ObjectNULLAn object for masking nullprotected static java.lang.StringREMOVE_INVALIDprotected static java.lang.StringSETVALUE_INVALID
 - 
Constructor SummaryConstructors Modifier Constructor Description protectedAbstractHashedMap()Constructor only used in deserialization, do not use otherwise.protectedAbstractHashedMap(int initialCapacity)Constructs a new, empty map with the specified initial capacity and default load factor.protectedAbstractHashedMap(int initialCapacity, float loadFactor)Constructs a new, empty map with the specified initial capacity and load factor.protectedAbstractHashedMap(int initialCapacity, float loadFactor, int threshold)Constructor which performs no validation on the passed in parameters.protectedAbstractHashedMap(java.util.Map<? extends K,? extends V> map)Constructor copying elements from another map.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddEntry(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex)Adds an entry into this map.protected voidaddMapping(int hashIndex, int hashCode, K key, V value)Adds a new key-value mapping into this map.protected intcalculateNewCapacity(int proposedCapacity)Calculates the new capacity of the map.protected intcalculateThreshold(int newCapacity, float factor)Calculates the new threshold of the map, where it will be resized.protected 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.protected AbstractHashedMap<K,V>clone()Clones the map without cloning the keys or values.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.protected java.lang.ObjectconvertKey(java.lang.Object key)Converts input keys to another object for storage in the map.protected AbstractHashedMap.HashEntry<K,V>createEntry(AbstractHashedMap.HashEntry<K,V> next, int hashCode, K key, V value)Creates an entry to store the key-value data.protected java.util.Iterator<java.util.Map.Entry<K,V>>createEntrySetIterator()Creates an entry set iterator.protected java.util.Iterator<K>createKeySetIterator()Creates a key set iterator.protected java.util.Iterator<V>createValuesIterator()Creates a values iterator.protected voiddestroyEntry(AbstractHashedMap.HashEntry<K,V> entry)Kills an entry ready for the garbage collector.protected voiddoReadObject(java.io.ObjectInputStream in)Reads the map data from the stream.protected voiddoWriteObject(java.io.ObjectOutputStream out)Writes the map data to the stream.protected voidensureCapacity(int newCapacity)Changes the size of the data structure to the capacity proposed.protected intentryHashCode(AbstractHashedMap.HashEntry<K,V> entry)Gets thehashCodefield from aHashEntry.protected KentryKey(AbstractHashedMap.HashEntry<K,V> entry)Gets thekeyfield from aHashEntry.protected AbstractHashedMap.HashEntry<K,V>entryNext(AbstractHashedMap.HashEntry<K,V> entry)Gets thenextfield from aHashEntry.java.util.Set<java.util.Map.Entry<K,V>>entrySet()Gets the entrySet view of the map.protected VentryValue(AbstractHashedMap.HashEntry<K,V> entry)Gets thevaluefield from aHashEntry.booleanequals(java.lang.Object obj)Compares this map with another.Vget(java.lang.Object key)Gets the value mapped to the key specified.protected AbstractHashedMap.HashEntry<K,V>getEntry(java.lang.Object key)Gets the entry mapped to the key specified.protected inthash(java.lang.Object key)Gets the hash code for the key specified.inthashCode()Gets the standard Map hashCode.protected inthashIndex(int hashCode, int dataSize)Gets the index into the data storage for the hashCode specified.protected voidinit()Initialise subclasses during construction, cloning or deserialization.booleanisEmpty()Checks whether the map is currently empty.protected booleanisEqualKey(java.lang.Object key1, java.lang.Object key2)Compares two keys, in internal converted form, to see if they are equal.protected booleanisEqualValue(java.lang.Object value1, java.lang.Object value2)Compares two values, in external form, to see if they are equal.java.util.Set<K>keySet()Gets the keySet view of the map.MapIterator<K,V>mapIterator()Gets an iterator over the map.Vput(K key, V value)Puts a key-value mapping into this map.voidputAll(java.util.Map<? extends K,? extends V> map)Puts all the values from the specified map into this map.Vremove(java.lang.Object key)Removes the specified mapping from this map.protected voidremoveEntry(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex, AbstractHashedMap.HashEntry<K,V> previous)Removes an entry from the chain stored in a particular index.protected voidremoveMapping(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex, AbstractHashedMap.HashEntry<K,V> previous)Removes a mapping from the map.protected voidreuseEntry(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex, int hashCode, K key, V value)Reuses an existing key-value mapping, storing completely new data.intsize()Gets the size of the map.java.lang.StringtoString()Gets the map as a String.protected voidupdateEntry(AbstractHashedMap.HashEntry<K,V> entry, V newValue)Updates an existing key-value mapping to change the value.java.util.Collection<V>values()Gets the values view of the map.
 
- 
- 
- 
Field Detail- 
NO_NEXT_ENTRYprotected static final java.lang.String NO_NEXT_ENTRY - See Also:
- Constant Field Values
 
 - 
NO_PREVIOUS_ENTRYprotected static final java.lang.String NO_PREVIOUS_ENTRY - See Also:
- Constant Field Values
 
 - 
REMOVE_INVALIDprotected static final java.lang.String REMOVE_INVALID - See Also:
- Constant Field Values
 
 - 
GETKEY_INVALIDprotected static final java.lang.String GETKEY_INVALID - See Also:
- Constant Field Values
 
 - 
GETVALUE_INVALIDprotected static final java.lang.String GETVALUE_INVALID - See Also:
- Constant Field Values
 
 - 
SETVALUE_INVALIDprotected static final java.lang.String SETVALUE_INVALID - See Also:
- Constant Field Values
 
 - 
DEFAULT_CAPACITYprotected static final int DEFAULT_CAPACITY The default capacity to use- See Also:
- Constant Field Values
 
 - 
DEFAULT_THRESHOLDprotected static final int DEFAULT_THRESHOLD The default threshold to use- See Also:
- Constant Field Values
 
 - 
DEFAULT_LOAD_FACTORprotected static final float DEFAULT_LOAD_FACTOR The default load factor to use- See Also:
- Constant Field Values
 
 - 
MAXIMUM_CAPACITYprotected static final int MAXIMUM_CAPACITY The maximum capacity allowed- See Also:
- Constant Field Values
 
 - 
NULLprotected static final java.lang.Object NULL An object for masking null
 
- 
 - 
Constructor Detail- 
AbstractHashedMapprotected AbstractHashedMap() Constructor only used in deserialization, do not use otherwise.
 - 
AbstractHashedMapprotected AbstractHashedMap(int initialCapacity, float loadFactor, int threshold)Constructor which performs no validation on the passed in parameters.- Parameters:
- initialCapacity- the initial capacity, must be a power of two
- loadFactor- the load factor, must be > 0.0f and generally < 1.0f
- threshold- the threshold, must be sensible
 
 - 
AbstractHashedMapprotected AbstractHashedMap(int initialCapacity) Constructs a new, empty map with the specified initial capacity and default load factor.- Parameters:
- initialCapacity- the initial capacity
- Throws:
- java.lang.IllegalArgumentException- if the initial capacity is negative
 
 - 
AbstractHashedMapprotected AbstractHashedMap(int initialCapacity, float loadFactor)Constructs a new, empty map with the specified initial capacity and load factor.- Parameters:
- initialCapacity- the initial capacity
- loadFactor- the load factor
- Throws:
- java.lang.IllegalArgumentException- if the initial capacity is negative
- java.lang.IllegalArgumentException- if the load factor is less than or equal to zero
 
 
- 
 - 
Method Detail- 
initprotected void init() Initialise subclasses during construction, cloning or deserialization.
 - 
getpublic V get(java.lang.Object key) Gets the value mapped to the key specified.
 - 
sizepublic int size() Gets the size of the map.
 - 
isEmptypublic boolean isEmpty() Checks whether the map is currently empty.
 - 
containsKeypublic boolean containsKey(java.lang.Object key) Checks whether the map contains the specified key.
 - 
containsValuepublic boolean containsValue(java.lang.Object value) Checks whether the map contains the specified value.
 - 
putpublic V put(K key, V value) Puts a key-value mapping into this map.- Specified by:
- putin interface- IterableMap<K,V>
- Specified by:
- putin interface- java.util.Map<K,V>
- Overrides:
- putin class- java.util.AbstractMap<K,V>
- Parameters:
- key- the key to add
- value- the value to add
- Returns:
- the value previously mapped to this key, null if none
- See Also:
- Map.put(Object, Object)
 
 - 
putAllpublic void putAll(java.util.Map<? extends K,? extends V> map) Puts all the values from the specified map into this map.This implementation iterates around the specified map and uses put(Object, Object).
 - 
removepublic V remove(java.lang.Object key) Removes the specified mapping from this map.
 - 
clearpublic void clear() Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.
 - 
convertKeyprotected java.lang.Object convertKey(java.lang.Object key) Converts input keys to another object for storage in the map. This implementation masks nulls. Subclasses can override this to perform alternate key conversions.The reverse conversion can be changed, if required, by overriding the getKey() method in the hash entry. - Parameters:
- key- the key convert
- Returns:
- the converted key
 
 - 
hashprotected int hash(java.lang.Object key) Gets the hash code for the key specified. This implementation uses the additional hashing routine from JDK1.4. Subclasses can override this to return alternate hash codes.- Parameters:
- key- the key to get a hash code for
- Returns:
- the hash code
 
 - 
isEqualKeyprotected boolean isEqualKey(java.lang.Object key1, java.lang.Object key2)Compares two keys, in internal converted form, to see if they are equal. This implementation uses the equals method and assumes neither key is null. Subclasses can override this to match differently.- Parameters:
- key1- the first key to compare passed in from outside
- key2- the second key extracted from the entry via- entry.key
- Returns:
- true if equal
 
 - 
isEqualValueprotected boolean isEqualValue(java.lang.Object value1, java.lang.Object value2)Compares two values, in external form, to see if they are equal. This implementation uses the equals method and assumes neither value is null. Subclasses can override this to match differently.- Parameters:
- value1- the first value to compare passed in from outside
- value2- the second value extracted from the entry via- getValue()
- Returns:
- true if equal
 
 - 
hashIndexprotected 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. Subclasses can override this to return alternate bucketing.- Parameters:
- hashCode- the hash code to use
- dataSize- the size of the data to pick a bucket from
- Returns:
- the bucket index
 
 - 
getEntryprotected AbstractHashedMap.HashEntry<K,V> getEntry(java.lang.Object key) Gets the entry mapped to the key specified.This method exists for subclasses that may need to perform a multi-step process accessing the entry. The public methods in this class don't use this method to gain a small performance boost. - Parameters:
- key- the key
- Returns:
- the entry, null if no match
 
 - 
updateEntryprotected void updateEntry(AbstractHashedMap.HashEntry<K,V> entry, V newValue) Updates an existing key-value mapping to change the value.This implementation calls setValue()on the entry. Subclasses could override to handle changes to the map.- Parameters:
- entry- the entry to update
- newValue- the new value to store
 
 - 
reuseEntryprotected void reuseEntry(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex, int hashCode, K key, V value) Reuses an existing key-value mapping, storing completely new data.This implementation sets all the data fields on the entry. Subclasses could populate additional entry fields. - Parameters:
- entry- the entry to update, not null
- hashIndex- the index in the data array
- hashCode- the hash code of the key to add
- key- the key to add
- value- the value to add
 
 - 
addMappingprotected void addMapping(int hashIndex, int hashCode, K key, V value)Adds a new key-value mapping into this map.This implementation calls createEntry(),addEntry()andcheckCapacity(). It also handles changes tomodCountandsize. Subclasses could override to fully control adds to the map.- Parameters:
- hashIndex- the index into the data array to store at
- hashCode- the hash code of the key to add
- key- the key to add
- value- the value to add
 
 - 
createEntryprotected AbstractHashedMap.HashEntry<K,V> createEntry(AbstractHashedMap.HashEntry<K,V> next, int hashCode, K key, V value) Creates an entry to store the key-value data.This implementation creates a new HashEntry instance. Subclasses can override this to return a different storage class, or implement caching. - Parameters:
- next- the next entry in sequence
- hashCode- the hash code to use
- key- the key to store
- value- the value to store
- Returns:
- the newly created entry
 
 - 
addEntryprotected void addEntry(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex) Adds an entry into this map.This implementation adds the entry to the data storage table. Subclasses could override to handle changes to the map. - Parameters:
- entry- the entry to add
- hashIndex- the index into the data array to store at
 
 - 
removeMappingprotected void removeMapping(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex, AbstractHashedMap.HashEntry<K,V> previous) Removes a mapping from the map.This implementation calls removeEntry()anddestroyEntry(). It also handles changes tomodCountandsize. Subclasses could override to fully control removals from the map.- Parameters:
- entry- the entry to remove
- hashIndex- the index into the data structure
- previous- the previous entry in the chain
 
 - 
removeEntryprotected void removeEntry(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex, AbstractHashedMap.HashEntry<K,V> 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. Subclasses could override to handle changes to the map. - Parameters:
- entry- the entry to remove
- hashIndex- the index into the data structure
- previous- the previous entry in the chain
 
 - 
destroyEntryprotected void destroyEntry(AbstractHashedMap.HashEntry<K,V> entry) Kills an entry ready for the garbage collector.This implementation prepares the HashEntry for garbage collection. Subclasses can override this to implement caching (override clear as well). - Parameters:
- entry- the entry to destroy
 
 - 
checkCapacityprotected 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 
 - 
ensureCapacityprotected 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)
 
 - 
calculateNewCapacityprotected int calculateNewCapacity(int proposedCapacity) Calculates the new capacity of the map. This implementation normalizes the capacity to a power of two.- Parameters:
- proposedCapacity- the proposed capacity
- Returns:
- the normalized new capacity
 
 - 
calculateThresholdprotected 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 capacity
- factor- the load factor
- Returns:
- the new resize threshold
 
 - 
entryNextprotected AbstractHashedMap.HashEntry<K,V> entryNext(AbstractHashedMap.HashEntry<K,V> entry) Gets thenextfield from aHashEntry. Used in subclasses that have no visibility of the field.- Parameters:
- entry- the entry to query, must not be null
- Returns:
- the nextfield of the entry
- Throws:
- java.lang.NullPointerException- if the entry is null
- Since:
- 3.1
 
 - 
entryHashCodeprotected int entryHashCode(AbstractHashedMap.HashEntry<K,V> entry) Gets thehashCodefield from aHashEntry. Used in subclasses that have no visibility of the field.- Parameters:
- entry- the entry to query, must not be null
- Returns:
- the hashCodefield of the entry
- Throws:
- java.lang.NullPointerException- if the entry is null
- Since:
- 3.1
 
 - 
entryKeyprotected K entryKey(AbstractHashedMap.HashEntry<K,V> entry) Gets thekeyfield from aHashEntry. Used in subclasses that have no visibility of the field.- Parameters:
- entry- the entry to query, must not be null
- Returns:
- the keyfield of the entry
- Throws:
- java.lang.NullPointerException- if the entry is null
- Since:
- 3.1
 
 - 
entryValueprotected V entryValue(AbstractHashedMap.HashEntry<K,V> entry) Gets thevaluefield from aHashEntry. Used in subclasses that have no visibility of the field.- Parameters:
- entry- the entry to query, must not be null
- Returns:
- the valuefield of the entry
- Throws:
- java.lang.NullPointerException- if the entry is null
- Since:
- 3.1
 
 - 
mapIteratorpublic MapIterator<K,V> mapIterator() Gets an iterator over the map. Changes made to the iterator affect this map.A MapIterator returns the keys in the map. It also provides convenient methods to get the key and value, and set the value. It avoids the need to create an entrySet/keySet/values object. It also avoids creating the Map.Entry object. - Specified by:
- mapIteratorin interface- IterableMap<K,V>
- Returns:
- the map iterator
 
 - 
entrySetpublic java.util.Set<java.util.Map.Entry<K,V>> entrySet() Gets the entrySet view of the map. Changes made to the view affect this map. To simply iterate through the entries, usemapIterator().
 - 
createEntrySetIteratorprotected java.util.Iterator<java.util.Map.Entry<K,V>> createEntrySetIterator() Creates an entry set iterator. Subclasses can override this to return iterators with different properties.- Returns:
- the entrySet iterator
 
 - 
keySetpublic java.util.Set<K> keySet() Gets the keySet view of the map. Changes made to the view affect this map. To simply iterate through the keys, usemapIterator().
 - 
createKeySetIteratorprotected java.util.Iterator<K> createKeySetIterator() Creates a key set iterator. Subclasses can override this to return iterators with different properties.- Returns:
- the keySet iterator
 
 - 
valuespublic java.util.Collection<V> values() Gets the values view of the map. Changes made to the view affect this map. To simply iterate through the values, usemapIterator().
 - 
createValuesIteratorprotected java.util.Iterator<V> createValuesIterator() Creates a values iterator. Subclasses can override this to return iterators with different properties.- Returns:
- the values iterator
 
 - 
doWriteObjectprotected void doWriteObject(java.io.ObjectOutputStream out) throws java.io.IOExceptionWrites the map data to the stream. This method must be overridden if a subclass must be setup beforeput()is used.Serialization is not one of the JDK's nicest topics. Normal serialization will initialise the superclass before the subclass. Sometimes however, this isn't what you want, as in this case the put()method on read can be affected by subclass state.The solution adopted here is to serialize the state data of this class in this protected method. This method must be called by the writeObject()of the first serializable subclass.Subclasses may override if they have a specific field that must be present on read before this implementation will work. Generally, the read determines what must be serialized here, if anything. - Parameters:
- out- the output stream
- Throws:
- java.io.IOException- if an error occurs while writing tothe stream
 
 - 
doReadObjectprotected void doReadObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundExceptionReads the map data from the stream. This method must be overridden if a subclass must be setup beforeput()is used.Serialization is not one of the JDK's nicest topics. Normal serialization will initialise the superclass before the subclass. Sometimes however, this isn't what you want, as in this case the put()method on read can be affected by subclass state.The solution adopted here is to deserialize the state data of this class in this protected method. This method must be called by the readObject()of the first serializable subclass.Subclasses may override if the subclass has a specific field that must be present before put()orcalculateThreshold()will work correctly.- Parameters:
- in- the input stream
- Throws:
- java.io.IOException- if an error occurs while reading from the stream
- java.lang.ClassNotFoundException- if an object read from the stream can not be loaded
 
 - 
cloneprotected AbstractHashedMap<K,V> clone() Clones the map without cloning the keys or values.To implement clone(), a subclass must implement theCloneableinterface and make this method public.
 - 
equalspublic boolean equals(java.lang.Object obj) Compares this map with another.
 - 
hashCodepublic int hashCode() Gets the standard Map hashCode.
 
- 
 
-