Class AbstractLinkedMap
- java.lang.Object
-
- java.util.AbstractMap
-
- org.apache.commons.collections.map.AbstractHashedMap
-
- org.apache.commons.collections.map.AbstractLinkedMap
-
- All Implemented Interfaces:
java.util.Map,IterableMap,OrderedMap
public class AbstractLinkedMap extends AbstractHashedMap implements OrderedMap
An abstract implementation of a hash-based map that links entries to create an ordered map and which provides numerous points for subclasses to override.This class implements all the features necessary for a subclass linked hash-based map. Key-value entries are stored in instances of the
LinkEntryclass 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.
This implementation maintains order by original insertion, but subclasses may work differently. The
OrderedMapinterface is implemented to provide access to bidirectional iteration and extra convenience methods.The
orderedMapIterator()method provides direct access to a bidirectional iterator. The iterators from the other views can also be cast toOrderedIteratorif required.All the available iterators can be reset back to the start by casting to
ResettableIteratorand callingreset().The implementation is also designed to be subclassed, with lots of useful methods exposed.
- Since:
- Commons Collections 3.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- java util LinkedHashMap, Stephen Colebourne
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractLinkedMap.EntrySetIteratorEntrySet iterator.protected static classAbstractLinkedMap.KeySetIteratorKeySet iterator.protected static classAbstractLinkedMap.LinkEntryLinkEntry that stores the data.protected static classAbstractLinkedMap.LinkIteratorBase Iterator that iterates in link order.protected static classAbstractLinkedMap.LinkMapIteratorMapIterator implementation.protected static classAbstractLinkedMap.ValuesIteratorValues iterator.-
Nested classes/interfaces inherited from class org.apache.commons.collections.map.AbstractHashedMap
AbstractHashedMap.EntrySet, AbstractHashedMap.HashEntry, AbstractHashedMap.HashIterator, AbstractHashedMap.HashMapIterator, AbstractHashedMap.KeySet, AbstractHashedMap.Values
-
-
Field Summary
Fields Modifier and Type Field Description protected AbstractLinkedMap.LinkEntryheaderHeader in the linked list-
Fields inherited from class org.apache.commons.collections.map.AbstractHashedMap
data, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD, entrySet, GETKEY_INVALID, GETVALUE_INVALID, keySet, loadFactor, MAXIMUM_CAPACITY, modCount, NO_NEXT_ENTRY, NO_PREVIOUS_ENTRY, NULL, REMOVE_INVALID, SETVALUE_INVALID, size, threshold, values
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractLinkedMap()Constructor only used in deserialization, do not use otherwise.protectedAbstractLinkedMap(int initialCapacity)Constructs a new, empty map with the specified initial capacity.protectedAbstractLinkedMap(int initialCapacity, float loadFactor)Constructs a new, empty map with the specified initial capacity and load factor.protectedAbstractLinkedMap(int initialCapacity, float loadFactor, int threshold)Constructor which performs no validation on the passed in parameters.protectedAbstractLinkedMap(java.util.Map map)Constructor copying elements from another map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddEntry(AbstractHashedMap.HashEntry entry, int hashIndex)Adds an entry into this map, maintaining insertion order.voidclear()Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.booleancontainsValue(java.lang.Object value)Checks whether the map contains the specified value.protected AbstractHashedMap.HashEntrycreateEntry(AbstractHashedMap.HashEntry next, int hashCode, java.lang.Object key, java.lang.Object value)Creates an entry to store the data.protected java.util.IteratorcreateEntrySetIterator()Creates an entry set iterator.protected java.util.IteratorcreateKeySetIterator()Creates a key set iterator.protected java.util.IteratorcreateValuesIterator()Creates a values iterator.protected AbstractLinkedMap.LinkEntryentryAfter(AbstractLinkedMap.LinkEntry entry)Gets theafterfield from aLinkEntry.protected AbstractLinkedMap.LinkEntryentryBefore(AbstractLinkedMap.LinkEntry entry)Gets thebeforefield from aLinkEntry.java.lang.ObjectfirstKey()Gets the first key in the map, which is the most recently inserted.protected AbstractLinkedMap.LinkEntrygetEntry(int index)Gets the key at the specified index.protected voidinit()Initialise this subclass during construction.java.lang.ObjectlastKey()Gets the last key in the map, which is the first inserted.MapIteratormapIterator()Gets an iterator over the map.java.lang.ObjectnextKey(java.lang.Object key)Gets the next key in sequence.OrderedMapIteratororderedMapIterator()Gets a bidirectional iterator over the map.java.lang.ObjectpreviousKey(java.lang.Object key)Gets the previous key in sequence.protected voidremoveEntry(AbstractHashedMap.HashEntry entry, int hashIndex, AbstractHashedMap.HashEntry previous)Removes an entry from the map and the linked list.-
Methods inherited from class org.apache.commons.collections.map.AbstractHashedMap
addMapping, calculateNewCapacity, calculateThreshold, checkCapacity, clone, containsKey, convertKey, destroyEntry, doReadObject, doWriteObject, ensureCapacity, entryHashCode, entryKey, entryNext, entrySet, entryValue, equals, get, getEntry, hash, hashCode, hashIndex, isEmpty, isEqualKey, isEqualValue, keySet, put, putAll, remove, removeMapping, reuseEntry, size, toString, updateEntry, values
-
-
-
-
Field Detail
-
header
protected transient AbstractLinkedMap.LinkEntry header
Header in the linked list
-
-
Constructor Detail
-
AbstractLinkedMap
protected AbstractLinkedMap()
Constructor only used in deserialization, do not use otherwise.
-
AbstractLinkedMap
protected AbstractLinkedMap(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 twoloadFactor- the load factor, must be > 0.0f and generally < 1.0fthreshold- the threshold, must be sensible
-
AbstractLinkedMap
protected AbstractLinkedMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity- Throws:
java.lang.IllegalArgumentException- if the initial capacity is less than one
-
AbstractLinkedMap
protected AbstractLinkedMap(int initialCapacity, float loadFactor)Constructs a new, empty map with the specified initial capacity and load factor.- Parameters:
initialCapacity- the initial capacityloadFactor- the load factor- Throws:
java.lang.IllegalArgumentException- if the initial capacity is less than onejava.lang.IllegalArgumentException- if the load factor is less than zero
-
AbstractLinkedMap
protected AbstractLinkedMap(java.util.Map map)
Constructor copying elements from another map.- Parameters:
map- the map to copy- Throws:
java.lang.NullPointerException- if the map is null
-
-
Method Detail
-
init
protected void init()
Initialise this subclass during construction.NOTE: As from v3.2 this method calls
createEntry(HashEntry, int, Object, Object)to create the map entry object.- Overrides:
initin classAbstractHashedMap
-
containsValue
public boolean containsValue(java.lang.Object value)
Checks whether the map contains the specified value.- Specified by:
containsValuein interfacejava.util.Map- Overrides:
containsValuein classAbstractHashedMap- Parameters:
value- the value to search for- Returns:
- true if the map contains the value
-
clear
public void clear()
Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.- Specified by:
clearin interfacejava.util.Map- Overrides:
clearin classAbstractHashedMap
-
firstKey
public java.lang.Object firstKey()
Gets the first key in the map, which is the most recently inserted.- Specified by:
firstKeyin interfaceOrderedMap- Returns:
- the most recently inserted key
-
lastKey
public java.lang.Object lastKey()
Gets the last key in the map, which is the first inserted.- Specified by:
lastKeyin interfaceOrderedMap- Returns:
- the eldest key
-
nextKey
public java.lang.Object nextKey(java.lang.Object key)
Gets the next key in sequence.- Specified by:
nextKeyin interfaceOrderedMap- Parameters:
key- the key to get after- Returns:
- the next key
-
previousKey
public java.lang.Object previousKey(java.lang.Object key)
Gets the previous key in sequence.- Specified by:
previousKeyin interfaceOrderedMap- Parameters:
key- the key to get before- Returns:
- the previous key
-
getEntry
protected AbstractLinkedMap.LinkEntry getEntry(int index)
Gets the key at the specified index.- Parameters:
index- the index to retrieve- Returns:
- the key at the specified index
- Throws:
java.lang.IndexOutOfBoundsException- if the index is invalid
-
addEntry
protected void addEntry(AbstractHashedMap.HashEntry entry, int hashIndex)
Adds an entry into this map, maintaining insertion order.This implementation adds the entry to the data storage table and to the end of the linked list.
- Overrides:
addEntryin classAbstractHashedMap- Parameters:
entry- the entry to addhashIndex- the index into the data array to store at
-
createEntry
protected AbstractHashedMap.HashEntry createEntry(AbstractHashedMap.HashEntry next, int hashCode, java.lang.Object key, java.lang.Object value)
Creates an entry to store the data.This implementation creates a new LinkEntry instance.
- Overrides:
createEntryin classAbstractHashedMap- Parameters:
next- the next entry in sequencehashCode- the hash code to usekey- the key to storevalue- the value to store- Returns:
- the newly created entry
-
removeEntry
protected void removeEntry(AbstractHashedMap.HashEntry entry, int hashIndex, AbstractHashedMap.HashEntry previous)
Removes an entry from the map and the linked list.This implementation removes the entry from the linked list chain, then calls the superclass implementation.
- Overrides:
removeEntryin classAbstractHashedMap- Parameters:
entry- the entry to removehashIndex- the index into the data structureprevious- the previous entry in the chain
-
entryBefore
protected AbstractLinkedMap.LinkEntry entryBefore(AbstractLinkedMap.LinkEntry entry)
Gets thebeforefield from aLinkEntry. Used in subclasses that have no visibility of the field.- Parameters:
entry- the entry to query, must not be null- Returns:
- the
beforefield of the entry - Throws:
java.lang.NullPointerException- if the entry is null- Since:
- Commons Collections 3.1
-
entryAfter
protected AbstractLinkedMap.LinkEntry entryAfter(AbstractLinkedMap.LinkEntry entry)
Gets theafterfield from aLinkEntry. Used in subclasses that have no visibility of the field.- Parameters:
entry- the entry to query, must not be null- Returns:
- the
afterfield of the entry - Throws:
java.lang.NullPointerException- if the entry is null- Since:
- Commons Collections 3.1
-
mapIterator
public MapIterator 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.
- Specified by:
mapIteratorin interfaceIterableMap- Overrides:
mapIteratorin classAbstractHashedMap- Returns:
- the map iterator
-
orderedMapIterator
public OrderedMapIterator orderedMapIterator()
Gets a bidirectional 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.
- Specified by:
orderedMapIteratorin interfaceOrderedMap- Returns:
- the map iterator
-
createEntrySetIterator
protected java.util.Iterator createEntrySetIterator()
Creates an entry set iterator. Subclasses can override this to return iterators with different properties.- Overrides:
createEntrySetIteratorin classAbstractHashedMap- Returns:
- the entrySet iterator
-
createKeySetIterator
protected java.util.Iterator createKeySetIterator()
Creates a key set iterator. Subclasses can override this to return iterators with different properties.- Overrides:
createKeySetIteratorin classAbstractHashedMap- Returns:
- the keySet iterator
-
createValuesIterator
protected java.util.Iterator createValuesIterator()
Creates a values iterator. Subclasses can override this to return iterators with different properties.- Overrides:
createValuesIteratorin classAbstractHashedMap- Returns:
- the values iterator
-
-