public class AbstractLinkedMap extends AbstractHashedMap implements OrderedMap
This class implements all the features necessary for a subclass linked
hash-based map. Key-value entries are stored in instances of the
LinkEntry
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.
This implementation maintains order by original insertion, but subclasses
may work differently. The OrderedMap
interface 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
to OrderedIterator
if required.
All the available iterators can be reset back to the start by casting to
ResettableIterator
and calling reset()
.
The implementation is also designed to be subclassed, with lots of useful methods exposed.
Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractLinkedMap.EntrySetIterator
EntrySet iterator.
|
protected static class |
AbstractLinkedMap.KeySetIterator
KeySet iterator.
|
protected static class |
AbstractLinkedMap.LinkEntry
LinkEntry that stores the data.
|
protected static class |
AbstractLinkedMap.LinkIterator
Base Iterator that iterates in link order.
|
protected static class |
AbstractLinkedMap.LinkMapIterator
MapIterator implementation.
|
protected static class |
AbstractLinkedMap.ValuesIterator
Values iterator.
|
AbstractHashedMap.EntrySet, AbstractHashedMap.HashEntry, AbstractHashedMap.HashIterator, AbstractHashedMap.HashMapIterator, AbstractHashedMap.KeySet, AbstractHashedMap.Values
AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry
Modifier and Type | Field and Description |
---|---|
protected AbstractLinkedMap.LinkEntry |
header
Header in the linked list
|
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
Modifier | Constructor and Description |
---|---|
protected |
AbstractLinkedMap()
Constructor only used in deserialization, do not use otherwise.
|
protected |
AbstractLinkedMap(int initialCapacity)
Constructs a new, empty map with the specified initial capacity.
|
protected |
AbstractLinkedMap(int initialCapacity,
float loadFactor)
Constructs a new, empty map with the specified initial capacity and
load factor.
|
protected |
AbstractLinkedMap(int initialCapacity,
float loadFactor,
int threshold)
Constructor which performs no validation on the passed in parameters.
|
protected |
AbstractLinkedMap(Map map)
Constructor copying elements from another map.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addEntry(AbstractHashedMap.HashEntry entry,
int hashIndex)
Adds an entry into this map, maintaining insertion order.
|
void |
clear()
Clears the map, resetting the size to zero and nullifying references
to avoid garbage collection issues.
|
boolean |
containsValue(Object value)
Checks whether the map contains the specified value.
|
protected AbstractHashedMap.HashEntry |
createEntry(AbstractHashedMap.HashEntry next,
int hashCode,
Object key,
Object value)
Creates an entry to store the data.
|
protected Iterator |
createEntrySetIterator()
Creates an entry set iterator.
|
protected Iterator |
createKeySetIterator()
Creates a key set iterator.
|
protected Iterator |
createValuesIterator()
Creates a values iterator.
|
protected AbstractLinkedMap.LinkEntry |
entryAfter(AbstractLinkedMap.LinkEntry entry)
Gets the
after field from a LinkEntry . |
protected AbstractLinkedMap.LinkEntry |
entryBefore(AbstractLinkedMap.LinkEntry entry)
Gets the
before field from a LinkEntry . |
Object |
firstKey()
Gets the first key in the map, which is the most recently inserted.
|
protected AbstractLinkedMap.LinkEntry |
getEntry(int index)
Gets the key at the specified index.
|
protected void |
init()
Initialise this subclass during construction.
|
Object |
lastKey()
Gets the last key in the map, which is the first inserted.
|
MapIterator |
mapIterator()
Gets an iterator over the map.
|
Object |
nextKey(Object key)
Gets the next key in sequence.
|
OrderedMapIterator |
orderedMapIterator()
Gets a bidirectional iterator over the map.
|
Object |
previousKey(Object key)
Gets the previous key in sequence.
|
protected void |
removeEntry(AbstractHashedMap.HashEntry entry,
int hashIndex,
AbstractHashedMap.HashEntry previous)
Removes an entry from the map and the linked list.
|
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
protected transient AbstractLinkedMap.LinkEntry header
protected AbstractLinkedMap()
protected AbstractLinkedMap(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 AbstractLinkedMap(int initialCapacity)
initialCapacity
- the initial capacityIllegalArgumentException
- if the initial capacity is less than oneprotected AbstractLinkedMap(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacityloadFactor
- the load factorIllegalArgumentException
- if the initial capacity is less than oneIllegalArgumentException
- if the load factor is less than zeroprotected AbstractLinkedMap(Map map)
map
- the map to copyNullPointerException
- if the map is nullprotected void init()
NOTE: As from v3.2 this method calls
AbstractHashedMap.createEntry(HashEntry, int, Object, Object)
to create
the map entry object.
init
in class AbstractHashedMap
public boolean containsValue(Object value)
containsValue
in interface Map
containsValue
in class AbstractHashedMap
value
- the value to search forpublic void clear()
clear
in interface Map
clear
in class AbstractHashedMap
public Object firstKey()
firstKey
in interface OrderedMap
public Object lastKey()
lastKey
in interface OrderedMap
public Object nextKey(Object key)
nextKey
in interface OrderedMap
key
- the key to get afterpublic Object previousKey(Object key)
previousKey
in interface OrderedMap
key
- the key to get beforeprotected AbstractLinkedMap.LinkEntry getEntry(int index)
index
- the index to retrieveIndexOutOfBoundsException
- if the index is invalidprotected void addEntry(AbstractHashedMap.HashEntry entry, int hashIndex)
This implementation adds the entry to the data storage table and to the end of the linked list.
addEntry
in class AbstractHashedMap
entry
- the entry to addhashIndex
- the index into the data array to store atprotected AbstractHashedMap.HashEntry createEntry(AbstractHashedMap.HashEntry next, int hashCode, Object key, Object value)
This implementation creates a new LinkEntry instance.
createEntry
in class AbstractHashedMap
next
- the next entry in sequencehashCode
- the hash code to usekey
- the key to storevalue
- the value to storeprotected void removeEntry(AbstractHashedMap.HashEntry entry, int hashIndex, AbstractHashedMap.HashEntry previous)
This implementation removes the entry from the linked list chain, then calls the superclass implementation.
removeEntry
in class AbstractHashedMap
entry
- the entry to removehashIndex
- the index into the data structureprevious
- the previous entry in the chainprotected AbstractLinkedMap.LinkEntry entryBefore(AbstractLinkedMap.LinkEntry entry)
before
field from a LinkEntry
.
Used in subclasses that have no visibility of the field.entry
- the entry to query, must not be nullbefore
field of the entryNullPointerException
- if the entry is nullprotected AbstractLinkedMap.LinkEntry entryAfter(AbstractLinkedMap.LinkEntry entry)
after
field from a LinkEntry
.
Used in subclasses that have no visibility of the field.entry
- the entry to query, must not be nullafter
field of the entryNullPointerException
- if the entry is nullpublic MapIterator mapIterator()
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.
mapIterator
in interface IterableMap
mapIterator
in class AbstractHashedMap
public OrderedMapIterator orderedMapIterator()
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.
orderedMapIterator
in interface OrderedMap
protected Iterator createEntrySetIterator()
createEntrySetIterator
in class AbstractHashedMap
protected Iterator createKeySetIterator()
createKeySetIterator
in class AbstractHashedMap
protected Iterator createValuesIterator()
createValuesIterator
in class AbstractHashedMap
Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.