|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.AbstractMap<K,V> org.apache.commons.collections.map.AbstractHashedMap<K,V>
public class AbstractHashedMap<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 theHashEntry
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.
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 v4.0.
Nested Class Summary | |
---|---|
protected static class |
AbstractHashedMap.EntrySet<K,V>
EntrySet implementation. |
protected static class |
AbstractHashedMap.EntrySetIterator<K,V>
EntrySet iterator. |
protected static class |
AbstractHashedMap.HashEntry<K,V>
HashEntry used to store the data. |
protected static class |
AbstractHashedMap.HashIterator<K,V>
Base Iterator |
protected static class |
AbstractHashedMap.HashMapIterator<K,V>
MapIterator implementation. |
protected static class |
AbstractHashedMap.KeySet<K,V>
KeySet implementation. |
protected static class |
AbstractHashedMap.KeySetIterator<K,V>
KeySet iterator. |
protected static class |
AbstractHashedMap.Values<K,V>
Values implementation. |
protected static class |
AbstractHashedMap.ValuesIterator<K,V>
Values iterator. |
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Field Summary | |
---|---|
protected AbstractHashedMap.HashEntry<K,V>[] |
data
Map entries |
protected static int |
DEFAULT_CAPACITY
The default capacity to use |
protected static float |
DEFAULT_LOAD_FACTOR
The default load factor to use |
protected static int |
DEFAULT_THRESHOLD
The default threshold to use |
protected AbstractHashedMap.EntrySet<K,V> |
entrySet
Entry set |
protected static String |
GETKEY_INVALID
|
protected static String |
GETVALUE_INVALID
|
protected AbstractHashedMap.KeySet<K,V> |
keySet
Key set |
protected float |
loadFactor
Load factor, normally 0.75 |
protected static int |
MAXIMUM_CAPACITY
The maximum capacity allowed |
protected int |
modCount
Modification count for iterators |
protected static String |
NO_NEXT_ENTRY
|
protected static String |
NO_PREVIOUS_ENTRY
|
protected static Object |
NULL
An object for masking null |
protected static String |
REMOVE_INVALID
|
protected static String |
SETVALUE_INVALID
|
protected int |
size
The size of the map |
protected int |
threshold
Size at which to rehash |
protected AbstractHashedMap.Values<K,V> |
values
Values |
Constructor Summary | |
---|---|
protected |
AbstractHashedMap()
Constructor only used in deserialization, do not use otherwise. |
protected |
AbstractHashedMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity and default load factor. |
protected |
AbstractHashedMap(int initialCapacity,
float loadFactor)
Constructs a new, empty map with the specified initial capacity and load factor. |
protected |
AbstractHashedMap(int initialCapacity,
float loadFactor,
int threshold)
Constructor which performs no validation on the passed in parameters. |
protected |
AbstractHashedMap(Map<? extends K,? extends V> map)
Constructor copying elements from another map. |
Method Summary | |
---|---|
protected void |
addEntry(AbstractHashedMap.HashEntry<K,V> entry,
int hashIndex)
Adds an entry into this map. |
protected void |
addMapping(int hashIndex,
int hashCode,
K key,
V value)
Adds a new key-value mapping into this map. |
protected int |
calculateNewCapacity(int proposedCapacity)
Calculates the new capacity of the map. |
protected int |
calculateThreshold(int newCapacity,
float factor)
Calculates the new threshold of the map, where it will be resized. |
protected 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. |
protected Object |
clone()
Clones the map without cloning the keys or values. |
boolean |
containsKey(Object key)
Checks whether the map contains the specified key. |
boolean |
containsValue(Object value)
Checks whether the map contains the specified value. |
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 Iterator<Map.Entry<K,V>> |
createEntrySetIterator()
Creates an entry set iterator. |
protected Iterator<K> |
createKeySetIterator()
Creates a key set iterator. |
protected Iterator<V> |
createValuesIterator()
Creates a values iterator. |
protected void |
destroyEntry(AbstractHashedMap.HashEntry<K,V> entry)
Kills an entry ready for the garbage collector. |
protected void |
doReadObject(ObjectInputStream in)
Reads the map data from the stream. |
protected void |
doWriteObject(ObjectOutputStream out)
Writes the map data to the stream. |
protected void |
ensureCapacity(int newCapacity)
Changes the size of the data structure to the capacity proposed. |
protected int |
entryHashCode(AbstractHashedMap.HashEntry<K,V> entry)
Gets the hashCode field from a HashEntry . |
protected K |
entryKey(AbstractHashedMap.HashEntry<K,V> entry)
Gets the key field from a HashEntry . |
protected AbstractHashedMap.HashEntry<K,V> |
entryNext(AbstractHashedMap.HashEntry<K,V> entry)
Gets the next field from a HashEntry . |
Set<Map.Entry<K,V>> |
entrySet()
Gets the entrySet view of the map. |
protected V |
entryValue(AbstractHashedMap.HashEntry<K,V> entry)
Gets the value field from a HashEntry . |
boolean |
equals(Object obj)
Compares this map with another. |
V |
get(Object key)
Gets the value mapped to the key specified. |
protected AbstractHashedMap.HashEntry<K,V> |
getEntry(Object key)
Gets the entry mapped to the key specified. |
protected int |
hash(Object key)
Gets the hash code for the key specified. |
int |
hashCode()
Gets the standard Map hashCode. |
protected int |
hashIndex(int hashCode,
int dataSize)
Gets the index into the data storage for the hashCode specified. |
protected void |
init()
Initialise subclasses during construction, cloning or deserialization. |
boolean |
isEmpty()
Checks whether the map is currently empty. |
protected boolean |
isEqualKey(Object key1,
Object key2)
Compares two keys, in internal converted form, to see if they are equal. |
protected boolean |
isEqualValue(Object value1,
Object value2)
Compares two values, in external form, to see if they are equal. |
Set<K> |
keySet()
Gets the keySet view of the map. |
MapIterator<K,V> |
mapIterator()
Gets an iterator over the map. |
V |
put(K key,
V value)
Puts a key-value mapping into this map. |
void |
putAll(Map<? extends K,? extends V> map)
Puts all the values from the specified map into this map. |
V |
remove(Object key)
Removes the specified mapping from this map. |
protected 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. |
protected void |
removeMapping(AbstractHashedMap.HashEntry<K,V> entry,
int hashIndex,
AbstractHashedMap.HashEntry<K,V> previous)
Removes a mapping from the map. |
protected 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. |
int |
size()
Gets the size of the map. |
String |
toString()
Gets the map as a String. |
protected void |
updateEntry(AbstractHashedMap.HashEntry<K,V> entry,
V newValue)
Updates an existing key-value mapping to change the value. |
Collection<V> |
values()
Gets the values view of the map. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final String NO_NEXT_ENTRY
protected static final String NO_PREVIOUS_ENTRY
protected static final String REMOVE_INVALID
protected static final String GETKEY_INVALID
protected static final String GETVALUE_INVALID
protected static final String SETVALUE_INVALID
protected static final int DEFAULT_CAPACITY
protected static final int DEFAULT_THRESHOLD
protected static final float DEFAULT_LOAD_FACTOR
protected static final int MAXIMUM_CAPACITY
protected static final Object NULL
protected transient float loadFactor
protected transient int size
protected transient AbstractHashedMap.HashEntry<K,V>[] data
protected transient int threshold
protected transient int modCount
protected transient AbstractHashedMap.EntrySet<K,V> entrySet
protected transient AbstractHashedMap.KeySet<K,V> keySet
protected transient AbstractHashedMap.Values<K,V> values
Constructor Detail |
---|
protected AbstractHashedMap()
protected AbstractHashedMap(int initialCapacity, float loadFactor, int threshold)
initialCapacity
- the initial capacity, must be a power of twoloadFactor
- the load factor, must be > 0.0f and generally < 1.0fthreshold
- the threshold, must be sensibleprotected AbstractHashedMap(int initialCapacity)
initialCapacity
- the initial capacity
IllegalArgumentException
- if the initial capacity is less than oneprotected AbstractHashedMap(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacityloadFactor
- the load factor
IllegalArgumentException
- if the initial capacity is less than one
IllegalArgumentException
- if the load factor is less than or equal to zeroprotected AbstractHashedMap(Map<? extends K,? extends V> map)
map
- the map to copy
NullPointerException
- if the map is nullMethod Detail |
---|
protected void init()
public V get(Object key)
get
in interface Map<K,V>
get
in class AbstractMap<K,V>
key
- the key
public int size()
size
in interface Map<K,V>
size
in class AbstractMap<K,V>
public boolean isEmpty()
isEmpty
in interface Map<K,V>
isEmpty
in class AbstractMap<K,V>
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
key
- the key to search for
public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
value
- the value to search for
public V put(K key, V value)
put
in interface Map<K,V>
put
in class AbstractMap<K,V>
key
- the key to addvalue
- the value to add
public void putAll(Map<? extends K,? extends V> map)
put(Object, Object)
.
putAll
in interface Map<K,V>
putAll
in class AbstractMap<K,V>
map
- the map to add
NullPointerException
- if the map is nullpublic V remove(Object key)
remove
in interface Map<K,V>
remove
in class AbstractMap<K,V>
key
- the mapping to remove
public void clear()
clear
in interface Map<K,V>
clear
in class AbstractMap<K,V>
protected int hash(Object key)
key
- the key to get a hash code for
protected boolean isEqualKey(Object key1, Object key2)
key1
- the first key to compare passed in from outsidekey2
- the second key extracted from the entry via entry.key
protected boolean isEqualValue(Object value1, Object value2)
value1
- the first value to compare passed in from outsidevalue2
- the second value extracted from the entry via getValue()
protected int hashIndex(int hashCode, int dataSize)
hashCode
- the hash code to usedataSize
- the size of the data to pick a bucket from
protected AbstractHashedMap.HashEntry<K,V> getEntry(Object key)
key
- the key
protected void updateEntry(AbstractHashedMap.HashEntry<K,V> entry, V newValue)
setValue()
on the entry.
Subclasses could override to handle changes to the map.
entry
- the entry to updatenewValue
- the new value to storeprotected void reuseEntry(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex, int hashCode, K key, V value)
entry
- the entry to update, not nullhashIndex
- the index in the data arrayhashCode
- the hash code of the key to addkey
- the key to addvalue
- the value to addprotected void addMapping(int hashIndex, int hashCode, K key, V value)
createEntry()
, addEntry()
and checkCapacity()
.
It also handles changes to modCount
and size
.
Subclasses could override to fully control adds to the map.
hashIndex
- the index into the data array to store athashCode
- the hash code of the key to addkey
- the key to addvalue
- the value to addprotected AbstractHashedMap.HashEntry<K,V> createEntry(AbstractHashedMap.HashEntry<K,V> next, int hashCode, K key, V value)
next
- the next entry in sequencehashCode
- the hash code to usekey
- the key to storevalue
- the value to store
protected void addEntry(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex)
entry
- the entry to addhashIndex
- the index into the data array to store atprotected void removeMapping(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex, AbstractHashedMap.HashEntry<K,V> previous)
removeEntry()
and destroyEntry()
.
It also handles changes to modCount
and size
.
Subclasses could override to fully control removals from the map.
entry
- the entry to removehashIndex
- the index into the data structureprevious
- the previous entry in the chainprotected void removeEntry(AbstractHashedMap.HashEntry<K,V> entry, int hashIndex, AbstractHashedMap.HashEntry<K,V> previous)
entry
- the entry to removehashIndex
- the index into the data structureprevious
- the previous entry in the chainprotected void destroyEntry(AbstractHashedMap.HashEntry<K,V> entry)
entry
- the entry to destroyprotected void checkCapacity()
protected void ensureCapacity(int newCapacity)
newCapacity
- the new capacity of the array (a power of two, less or equal to max)protected int calculateNewCapacity(int proposedCapacity)
proposedCapacity
- the proposed capacity
protected int calculateThreshold(int newCapacity, float factor)
newCapacity
- the new capacityfactor
- the load factor
protected AbstractHashedMap.HashEntry<K,V> entryNext(AbstractHashedMap.HashEntry<K,V> entry)
next
field from a HashEntry
.
Used in subclasses that have no visibility of the field.
entry
- the entry to query, must not be null
next
field of the entry
NullPointerException
- if the entry is nullprotected int entryHashCode(AbstractHashedMap.HashEntry<K,V> entry)
hashCode
field from a HashEntry
.
Used in subclasses that have no visibility of the field.
entry
- the entry to query, must not be null
hashCode
field of the entry
NullPointerException
- if the entry is nullprotected K entryKey(AbstractHashedMap.HashEntry<K,V> entry)
key
field from a HashEntry
.
Used in subclasses that have no visibility of the field.
entry
- the entry to query, must not be null
key
field of the entry
NullPointerException
- if the entry is nullprotected V entryValue(AbstractHashedMap.HashEntry<K,V> entry)
value
field from a HashEntry
.
Used in subclasses that have no visibility of the field.
entry
- the entry to query, must not be null
value
field of the entry
NullPointerException
- if the entry is nullpublic MapIterator<K,V> mapIterator()
mapIterator
in interface IterableMap<K,V>
public Set<Map.Entry<K,V>> entrySet()
mapIterator()
.
entrySet
in interface Map<K,V>
entrySet
in class AbstractMap<K,V>
protected Iterator<Map.Entry<K,V>> createEntrySetIterator()
public Set<K> keySet()
mapIterator()
.
keySet
in interface Map<K,V>
keySet
in class AbstractMap<K,V>
protected Iterator<K> createKeySetIterator()
public Collection<V> values()
mapIterator()
.
values
in interface Map<K,V>
values
in class AbstractMap<K,V>
protected Iterator<V> createValuesIterator()
protected void doWriteObject(ObjectOutputStream out) throws IOException
put()
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.
out
- the output stream
IOException
protected void doReadObject(ObjectInputStream in) throws IOException, ClassNotFoundException
put()
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()
or calculateThreshold()
will work correctly.
in
- the input stream
IOException
ClassNotFoundException
protected Object clone()
clone()
, a subclass must implement the
Cloneable
interface and make this method public.
clone
in class AbstractMap<K,V>
public boolean equals(Object obj)
equals
in interface Map<K,V>
equals
in class AbstractMap<K,V>
obj
- the object to compare to
public int hashCode()
hashCode
in interface Map<K,V>
hashCode
in class AbstractMap<K,V>
public String toString()
toString
in class AbstractMap<K,V>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |