public class BasicEMap<K,V> extends java.lang.Object implements EMap<K,V>, java.lang.Cloneable, java.io.Serializable
Modifier and Type | Class and Description |
---|---|
protected class |
BasicEMap.BasicEMapIterator<U>
An iterator over the map entry data.
|
protected class |
BasicEMap.BasicEMapKeyIterator
An iterator over the map key data.
|
protected class |
BasicEMap.BasicEMapValueIterator
An iterator over the map value data.
|
protected class |
BasicEMap.DelegatingMap |
static interface |
BasicEMap.Entry<K,V>
An extended implementation interface for caching hash values
and for updating an entry that may be manufactured as a uninitialized instance by a factory.
|
protected class |
BasicEMap.EntryImpl
A simple and obvious entry implementation.
|
protected static class |
BasicEMap.View<K,V>
An implementation class to hold the views.
|
EMap.InternalMapView<K,V>
Modifier and Type | Field and Description |
---|---|
protected EList<BasicEMap.Entry<K,V>> |
delegateEList
The underlying list of entries.
|
protected BasicEList<BasicEMap.Entry<K,V>>[] |
entryData
The array of entry lists into which the hash codes are indexed.
|
protected int |
modCount
The modification indicator used to ensure iterator integrity.
|
protected int |
size
The size of the map.
|
protected BasicEMap.View<K,V> |
view
The various alternative views of the map.
|
Constructor and Description |
---|
BasicEMap()
Creates an empty instance.
|
BasicEMap(int initialCapacity)
Creates an empty instance with the given capacity.
|
BasicEMap(java.util.Map<? extends K,? extends V> map)
Creates an instance that is a copy of the map.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
java.util.Map.Entry<K,V> object)
Delegates to
delegateEList . |
boolean |
add(java.util.Map.Entry<K,V> object)
Delegates to
delegateEList . |
boolean |
addAll(java.util.Collection<? extends java.util.Map.Entry<K,V>> collection)
Delegates to
delegateEList . |
boolean |
addAll(int index,
java.util.Collection<? extends java.util.Map.Entry<K,V>> collection)
Delegates to
delegateEList . |
void |
clear()
Delegates to
delegateEList . |
java.lang.Object |
clone()
Returns a shallow copy of this map.
|
boolean |
contains(java.lang.Object object)
Delegates to
delegateEList . |
boolean |
containsAll(java.util.Collection<?> collection)
Delegates to
delegateEList . |
boolean |
containsKey(java.lang.Object key)
Returns whether the key is associated with a value.
|
boolean |
containsValue(java.lang.Object value)
Returns whether the value is associated with a key.
|
protected void |
didAdd(BasicEMap.Entry<K,V> entry)
Called to indicate that the entry has been added.
|
protected void |
didClear(BasicEList<BasicEMap.Entry<K,V>>[] oldEntryData)
Called to indicate that the map has been cleared.
|
protected void |
didModify(BasicEMap.Entry<K,V> entry,
V oldValue)
Called to indicate that the entry has an updated value.
|
protected void |
didRemove(BasicEMap.Entry<K,V> entry)
Called to indicate that the entry has been removed.
|
protected void |
doClear()
Clears the map.
|
protected void |
doMove(BasicEMap.Entry<K,V> entry)
Increments the modification count.
|
protected void |
doPut(BasicEMap.Entry<K,V> entry)
Adds the new entry to the map.
|
protected void |
doRemove(BasicEMap.Entry<K,V> entry)
Removes the entry from the map.
|
protected void |
ensureEntryDataExists()
Ensures that the entry data is created
and is populated with contents of the delegate list.
|
protected BasicEMap.Entry<K,V> |
entryForKey(int index,
int hash,
java.lang.Object key)
Called to return the entry given the index, the hash, and the key.
|
protected int |
entryIndexForKey(int index,
int hash,
java.lang.Object key)
Called to return the entry list index given the index, the hash, and the key.
|
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
Returns a set view of the entries.
|
boolean |
equals(java.lang.Object object) |
BasicEMap.Entry<K,V> |
get(int index)
Delegates to
delegateEList . |
V |
get(java.lang.Object key)
Returns the value associated with the key.
|
protected boolean |
grow(int minimumCapacity)
Grows the capacity of the map
to ensure that no additional growth is needed until the size exceeds the specified minimum capacity.
|
int |
hashCode() |
protected int |
hashOf(java.lang.Object key)
Called to return the hash code of the key.
|
protected int |
indexOf(int hash)
Called to return the entry data index corresponding to the hash code.
|
int |
indexOf(java.lang.Object object)
Delegates to
delegateEList . |
int |
indexOfKey(java.lang.Object key)
Returns the index in the list of the entry with the given key,
or
-1 , if there is no such entry. |
protected void |
initializeDelegateEList()
Initializes the
delegateEList . |
boolean |
isEmpty()
Returns whether the map has zero size.
|
java.util.Iterator<java.util.Map.Entry<K,V>> |
iterator()
Delegates to
delegateEList . |
java.util.Set<K> |
keySet()
Returns a set view of the keys of the entries.
|
int |
lastIndexOf(java.lang.Object object)
Delegates to
delegateEList . |
java.util.ListIterator<java.util.Map.Entry<K,V>> |
listIterator()
Delegates to
delegateEList . |
java.util.ListIterator<java.util.Map.Entry<K,V>> |
listIterator(int index)
Delegates to
delegateEList . |
java.util.Map<K,V> |
map()
Returns a map view.
|
java.util.Map.Entry<K,V> |
move(int targetIndex,
int sourceIndex)
Delegates to
delegateEList . |
void |
move(int index,
java.util.Map.Entry<K,V> object)
Delegates to
delegateEList . |
protected BasicEMap.Entry<K,V> |
newEntry(int hash,
K key,
V value)
Returns a new entry.
|
protected BasicEList<BasicEMap.Entry<K,V>>[] |
newEntryData(int capacity)
Returns new allocated entry data storage.
|
protected BasicEList<BasicEMap.Entry<K,V>> |
newList()
Returns a new allocated list of entries.
|
V |
put(K key,
V value)
Associates the key with the value
and returns the value previously associated with the key, or
null . |
void |
putAll(EMap<? extends K,? extends V> map)
Puts each
Map.Entry of the given map into this one. |
void |
putAll(java.util.Map<? extends K,? extends V> map)
Puts each
Map.Entry of the given map into this one. |
protected V |
putEntry(BasicEMap.Entry<K,V> entry,
V value)
Sets the value of the entry, and returns the former value.
|
java.util.Map.Entry<K,V> |
remove(int index)
Delegates to
delegateEList . |
boolean |
remove(java.lang.Object object)
Delegates to
delegateEList . |
boolean |
removeAll(java.util.Collection<?> collection)
Delegates to
delegateEList . |
protected V |
removeEntry(int index,
int entryIndex)
Removes the fully indexed entry from the map and returns it's value.
|
V |
removeKey(java.lang.Object key)
Disassociates the key from its value,
and returns the value formerly associated with the key.
|
protected V |
resolve(K key,
V value)
Resolves the value associated with the key and returns the result.
|
boolean |
retainAll(java.util.Collection<?> collection)
Delegates to
delegateEList . |
java.util.Map.Entry<K,V> |
set(int index,
java.util.Map.Entry<K,V> object)
Delegates to
delegateEList . |
int |
size()
Returns the number of entries in the map.
|
java.util.List<java.util.Map.Entry<K,V>> |
subList(int start,
int end)
Delegates to
delegateEList . |
java.lang.Object[] |
toArray()
Delegates to
delegateEList . |
<T> T[] |
toArray(T[] array)
Delegates to
delegateEList . |
java.lang.String |
toString()
Delegates to
delegateEList . |
protected boolean |
useEqualsForKey()
Returns whether
equals rather than == should be used to compare keys. |
protected boolean |
useEqualsForValue()
Returns whether
equals rather than == should be used to compare values. |
protected void |
validateKey(K key)
Validates a new key.
|
protected void |
validateValue(V value)
Validates a new key.
|
java.util.Collection<V> |
values()
Returns a collection view the values of the entries.
|
protected transient EList<BasicEMap.Entry<K,V>> delegateEList
protected transient int size
protected transient BasicEList<BasicEMap.Entry<K,V>>[] entryData
protected transient int modCount
protected transient BasicEMap.View<K,V> view
public BasicEMap()
public BasicEMap(int initialCapacity)
initialCapacity
- the initial capacity of the map before it must grow.java.lang.IllegalArgumentException
- if the initialCapacity
is negative.protected void initializeDelegateEList()
delegateEList
.
This implementation illustrates the precise pattern that is used to
delegate a list implementation's callback methods to the map implementation.protected BasicEList<BasicEMap.Entry<K,V>>[] newEntryData(int capacity)
capacity
- the capacity of storage needed.protected void ensureEntryDataExists()
protected BasicEList<BasicEMap.Entry<K,V>> newList()
newEntry
.newEntry(int, Object, Object)
protected BasicEMap.Entry<K,V> newEntry(int hash, K key, V value)
validated
and the value is validated
.
Clients may override this to create typed storage.
The type must be kept in synch with newEntry
.hash
- the cached hash code of the key.key
- the key.value
- the value.newList()
protected V putEntry(BasicEMap.Entry<K,V> entry, V value)
validated
.entry
- the entry.value
- the value.null
.protected boolean useEqualsForKey()
equals
rather than ==
should be used to compare keys.
The default is to return true
but clients can optimize performance by returning false
.
The performance difference is highly significant.equals
rather than ==
should be used to compare keys.protected boolean useEqualsForValue()
equals
rather than ==
should be used to compare values.
The default is to return true
but clients can optimize performance by returning false
.
The performance difference is highly significant.equals
rather than ==
should be used to compare values.protected V resolve(K key, V value)
value
;
clients can use this to transform objects as they are fetched.key
- the key of an entry.value
- the value of an entry.protected void validateKey(K key)
key
- the new key.java.lang.IllegalArgumentException
- if a constraint prevents the object from being added.protected void validateValue(V value)
value
- the new value.java.lang.IllegalArgumentException
- if a constraint prevents the object from being added.protected void didAdd(BasicEMap.Entry<K,V> entry)
entry
- the added entry.protected void didModify(BasicEMap.Entry<K,V> entry, V oldValue)
entry
- the new entry.protected void didRemove(BasicEMap.Entry<K,V> entry)
entry
- the removed entry.protected void didClear(BasicEList<BasicEMap.Entry<K,V>>[] oldEntryData)
didRemove
for each entry;
clients can use this to monitor clearing of the map.oldEntryData
- the removed entries.public int size()
public boolean isEmpty()
public int indexOfKey(java.lang.Object key)
EMap
-1
, if there is no such entry.indexOfKey
in interface EMap<K,V>
key
- a key.public boolean containsKey(java.lang.Object key)
EMap
containsKey
in interface EMap<K,V>
key
- a key associated with a value.public boolean containsValue(java.lang.Object value)
EMap
containsValue
in interface EMap<K,V>
value
- a value associated with a key.public V get(java.lang.Object key)
EMap
null
.public V put(K key, V value)
EMap
null
.
The key, the value, or both may be null
.
Either the existing entry is updated,
or a new entry is added to the end of the list.protected void doPut(BasicEMap.Entry<K,V> entry)
entry
- the new entry.public V removeKey(java.lang.Object key)
EMap
protected void doRemove(BasicEMap.Entry<K,V> entry)
entry
- an entry in the map.protected V removeEntry(int index, int entryIndex)
index
- the index in the entry dataentryIndex
- the index in the list of entries.public void putAll(java.util.Map<? extends K,? extends V> map)
EMap
Map.Entry
of the given map into this one.putAll
in interface EMap<K,V>
map
- the map of entries.EMap.put(K, V)
public void putAll(EMap<? extends K,? extends V> map)
EMap
Map.Entry
of the given map into this one.putAll
in interface EMap<K,V>
map
- the map of entries.EMap.put(K, V)
protected void doClear()
protected void doMove(BasicEMap.Entry<K,V> entry)
public java.lang.Object clone()
clone
in class java.lang.Object
public java.util.Set<K> keySet()
EMap
public java.util.Collection<V> values()
EMap
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
EMap
protected int hashOf(java.lang.Object key)
key
- the key.protected int indexOf(int hash)
hash
- the hash code.protected BasicEMap.Entry<K,V> entryForKey(int index, int hash, java.lang.Object key)
index
- the entry data index of the key.hash
- the hash code of the key.key
- the key.protected int entryIndexForKey(int index, int hash, java.lang.Object key)
index
- the entry data index of the key.hash
- the hash code of the key.key
- the key.protected boolean grow(int minimumCapacity)
public boolean contains(java.lang.Object object)
delegateEList
.public boolean containsAll(java.util.Collection<?> collection)
delegateEList
.public int indexOf(java.lang.Object object)
delegateEList
.public int lastIndexOf(java.lang.Object object)
delegateEList
.public java.lang.Object[] toArray()
delegateEList
.public <T> T[] toArray(T[] array)
delegateEList
.public BasicEMap.Entry<K,V> get(int index)
delegateEList
.public java.util.Map.Entry<K,V> set(int index, java.util.Map.Entry<K,V> object)
delegateEList
.public boolean add(java.util.Map.Entry<K,V> object)
delegateEList
.public void add(int index, java.util.Map.Entry<K,V> object)
delegateEList
.public boolean addAll(java.util.Collection<? extends java.util.Map.Entry<K,V>> collection)
delegateEList
.public boolean addAll(int index, java.util.Collection<? extends java.util.Map.Entry<K,V>> collection)
delegateEList
.public boolean remove(java.lang.Object object)
delegateEList
.public boolean removeAll(java.util.Collection<?> collection)
delegateEList
.public java.util.Map.Entry<K,V> remove(int index)
delegateEList
.public boolean retainAll(java.util.Collection<?> collection)
delegateEList
.public void clear()
delegateEList
.public void move(int index, java.util.Map.Entry<K,V> object)
delegateEList
.public java.util.Map.Entry<K,V> move(int targetIndex, int sourceIndex)
delegateEList
.public java.util.Iterator<java.util.Map.Entry<K,V>> iterator()
delegateEList
.public java.util.ListIterator<java.util.Map.Entry<K,V>> listIterator()
delegateEList
.public java.util.ListIterator<java.util.Map.Entry<K,V>> listIterator(int index)
delegateEList
.public java.util.List<java.util.Map.Entry<K,V>> subList(int start, int end)
delegateEList
.public int hashCode()
public boolean equals(java.lang.Object object)
public java.lang.String toString()
delegateEList
.toString
in class java.lang.Object