|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.neeve.lang.XLinkedHashMap<K,V>
public class XLinkedHashMap<K,V>
This class represents a hash map that avoid creation of garbage;
smooth capacity increase and thread-safe without external
synchronization when shared
.
XLinkedHashMap
has a predictable iteration order, which is the order in
which keys are inserted into the map (similar to
java.util.LinkedHashMap
collection class). If the map is
marked shared
then all operations are
thread-safe including iterations over the map's collections.
Unlike ConcurrentHashMap
, access
never
blocks; retrieval reflects the map state not older than the last time the
accessing threads have been synchronized (for multi-processors systems
synchronizing ensures that the CPU internal cache is not stale).
In most application it is not a problem because thread synchronization
is done at high level (e.g. scheduler) and threads run freely
(and quickly) until the next synchronization point. In some cases the
"happen before" guarantee is necessary (e.g. to ensure unicity) and
threads have to be synchronized explicitly. Whenever possible such
synchronization should be performed on the key object itself and
not the whole map. For example:
XLinkedHashMap
XLinkedHashMap.Entry
can quickly be iterated over (forward or backward)
without using iterators. For example:
XLinkedHashMap
Alternatively The map can be iterated over using reuseableEntryIterator()
,
reuseableKeyIterator()
, or reuseableValueIterator()
. Utilization of
these iterators does not introduce garbage because they are created once. However,
these iterators may not be used concurrently by multiple threads as they are
single instance by nature.
Custom map implementations may override the newEntry()
method
in order to return their own XLinkedHashMap.Entry
implementation (with
additional fields for example).
Shared
maps do not use internal synchronization, except in case of
concurrent modifications of the map structure (entries being added/deleted).
Reads and iterations are never synchronized and never blocking.
With regards to the memory model, shared maps are equivalent to shared
non-volatile variables (no "happen before" guarantee). They can be used
as very efficient lookup tables. For example:[code]
public class Unit {
static XLinkedHashMap
Implementation Note: To maintain time-determinism, rehash/resize is performed only when the map's size is small. For large maps (size > 512), the map is divided recursively into (64) smaller sub-maps. The cost of the dispatching (based upon hashcode value) has been measured to be at most 20% of the access time (and most often way less).
This class was based on collections code from http://javolution.org, but modified for use outside of realtime jvms.
Nested Class Summary | |
---|---|
static class |
XLinkedHashMap.Entry<K,V>
This class represents a XLinkedHashMap entry. |
Constructor Summary | |
---|---|
XLinkedHashMap()
Creates a map whose capacity increment smoothly without large resize operations. |
|
XLinkedHashMap(int capacity)
Creates a map of specified maximum size (a full resize may occur if the specififed capacity is exceeded). |
|
XLinkedHashMap(Map<? extends K,? extends V> map)
Creates a map containing the specified entries, in the order they are returned by the map iterator. |
Method Summary | ||
---|---|---|
void |
clear()
Removes all map's entries. |
|
boolean |
containsKey(Object key)
Indicates if this map contains a mapping for the specified key. |
|
boolean |
containsValue(Object value)
Indicates if this map associates one or more keys to the specified value. |
|
Set<Map.Entry<K,V>> |
entrySet()
Returns a XCollection view of the mappings contained in this
map. |
|
boolean |
equals(Object obj)
Compares the specified object with this map for equality. |
|
V |
get(Object key)
Returns the value to which this map associates the specified key. |
|
XLinkedHashMap.Entry<K,V> |
getEntry(Object key)
Returns the entry with the specified key. |
|
int |
hashCode()
Returns the hash code value for this map. |
|
XLinkedHashMap.Entry<K,V> |
head()
Returns the head entry of this map. |
|
boolean |
isEmpty()
Indicates if this map contains no key-value mappings. |
|
boolean |
isShared()
Indicates if this map supports concurrent operations without synchronization (default unshared). |
|
Set<K> |
keySet()
Returns a XCollection view of the keys contained in this
map. |
|
static
|
newInstance()
Creates a new XLinkedHashMap. |
|
void |
printStatistics(PrintStream out)
Prints the current statistics on this map. |
|
V |
put(K key,
V value)
Associates the specified value with the specified key in this map. |
|
void |
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map. |
|
XLinkedHashMap.Entry<K,V> |
putEntry(K key,
V value)
Associates the specified value with the specified key in this map and returns the corresponding entry. |
|
V |
putIfAbsent(K key,
V value)
Associates the specified value only if the specified key is not already associated. |
|
V |
remove(Object key)
Removes the entry for the specified key if present. |
|
XCollectionIterator<Map.Entry<K,V>> |
reuseableEntryIterator()
Returns a reusable value iterator. |
|
XIterator<K> |
reuseableKeyIterator()
Returns a reusable key iterator. |
|
XIterator<V> |
reuseableValueIterator()
Returns a reusable value iterator. |
|
XLinkedHashMap<K,V> |
shared()
Sets the shared status of this map (whether the map is thread-safe or not). |
|
int |
size()
Returns the number of key-value mappings in this XLinkedHashMap . |
|
XLinkedHashMap.Entry<K,V> |
tail()
Returns the tail entry of this map. |
|
String |
toString()
Returns a string representation of this map. |
|
Map<K,V> |
unmodifiable()
Returns the unmodifiable view associated to this map. |
|
XCollection<V> |
values()
Returns a XCollection view of the values contained in this
map. |
Methods inherited from class java.lang.Object |
---|
getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public XLinkedHashMap()
public XLinkedHashMap(int capacity)
capacity
- the maximum capacity.public XLinkedHashMap(Map<? extends K,? extends V> map)
map
- the map whose entries are to be placed into this map.Method Detail |
---|
public static <K,V> XLinkedHashMap<K,V> newInstance()
public final XLinkedHashMap.Entry<K,V> head()
head().getNext()
holds
the first map entry.public final XLinkedHashMap.Entry<K,V> tail()
tail().getPrevious()
holds the last map entry.public final int size()
XLinkedHashMap
.
Note: If concurrent updates are performed, application should not rely upon the size during iterations.
size
in interface Map<K,V>
public final boolean isEmpty()
isEmpty
in interface Map<K,V>
true
if this map contains no key-value mappings;
false
otherwise.public final boolean containsKey(Object key)
containsKey
in interface Map<K,V>
key
- the key whose presence in this map is to be tested.
true
if this map contains a mapping for the
specified key; false
otherwise.
NullPointerException
- if the key is null
.public final boolean containsValue(Object value)
containsValue
in interface Map<K,V>
value
- the value whose presence in this map is to be tested.
true
if this map maps one or more keys to the
specified value.
NullPointerException
- if the key is null
.public final V get(Object key)
shared
.
get
in interface Map<K,V>
key
- the key whose associated value is to be returned.
null
if there is no mapping for the key.
NullPointerException
- if key is null
.public final XLinkedHashMap.Entry<K,V> getEntry(Object key)
key
- the key whose associated entry is to be returned.
null
if none.public final V put(K key, V value)
shared
map, internal synchronization
is performed only when new entries are created.
put
in interface Map<K,V>
key
- the key with which the specified value is to be associated.value
- the value to be associated with the specified key.
null
if there was no mapping for key. A
null
return can also indicate that the map
previously associated null
with the specified key.
NullPointerException
- if the key is null
.public final XLinkedHashMap.Entry<K,V> putEntry(K key, V value)
key
- the key with which the specified value is to be associated.value
- the value to be associated with the specified key.
NullPointerException
- if the key is null
.public final void putAll(Map<? extends K,? extends V> map)
putAll
in interface Map<K,V>
map
- the mappings to be stored in this map.
NullPointerException
- the specified map is null
,
or the specified map contains null
keys.public final V putIfAbsent(K key, V value)
key
- the key with which the specified value is to be associated.value
- the value to be associated with the specified key.
null
if there was no mapping for key. A
null
return can also indicate that the map
previously associated null
with the specified key.
NullPointerException
- if the key is null
.public final V remove(Object key)
shared
;
otherwise the entry is candidate for garbage collection.
Note: Shared maps in ImmortalMemory (e.g. static) should not remove
their entries as it could cause a memory leak (ImmortalMemory
is never garbage collected), instead they should set their
entry values to null
.
remove
in interface Map<K,V>
key
- the key whose mapping is to be removed from the map.
null
if there was no mapping for key. A
null
return can also indicate that the map
previously associated null
with the specified key.
NullPointerException
- if the key is null
.public XLinkedHashMap<K,V> shared()
Sets the shared status of this map (whether the map is thread-safe or not). Shared maps are typically used for lookup table (e.g. static instances in ImmortalMemory). They support concurrent access (e.g. iterations) without synchronization, the maps updates themselves are synchronized internally.
Unlike ConcurrentHashMap
access to a shared map never
blocks. Retrieval reflects the map state not older than the last
time the accessing thread has been synchronized (for multi-processors
systems synchronizing ensures that the CPU internal cache is not
stale).
this
public boolean isShared()
true
if this map is thread-safe; false
otherwise.public final void clear()
shared
in which case the entries
are candidate for garbage collection.
Note: Shared maps in ImmortalMemory (e.g. static) should not remove
their entries as it could cause a memory leak (ImmortalMemory
is never garbage collected), instead they should set their
entry values to null
.
clear
in interface Map<K,V>
public boolean equals(Object obj)
true
if the given object is also a map and the two
maps represent the same mappings (regardless of collection iteration
order).
equals
in interface Map<K,V>
equals
in class Object
obj
- the object to be compared for equality with this map.
true
if the specified object is equal to this map;
false
otherwise.public int hashCode()
hashCode
in interface Map<K,V>
hashCode
in class Object
public String toString()
String.valueOf(Object)
.
toString
in class Object
public void printStatistics(PrintStream out)
out
- the stream to use for output (e.g. System.out
)public final XCollection<V> values()
XCollection
view of the values contained in this
map. The collection is backed by the map, so changes to the
map are reflected in the collection, and vice-versa. The collection
supports element removal, which removes the corresponding mapping from
this map, via the Iterator.remove
,
Collection.remove
, removeAll
,
retainAll
and clear
operations.
It does not support the add
or addAll
operations.
values
in interface Map<K,V>
XCollection
).public final Set<Map.Entry<K,V>> entrySet()
XCollection
view of the mappings contained in this
map. Each element in the returned collection is a
XLinkedHashMap.Entry
. The collection is backed by the map, so
changes to the map are reflected in the collection, and vice-versa. The
collection supports element removal, which removes the corresponding
mapping from this map, via the Iterator.remove
,
Collection.remove
,removeAll
,
retainAll
, and clear
operations. It does
not support the add
or addAll
operations.
entrySet
in interface Map<K,V>
XCollection
).public final Set<K> keySet()
XCollection
view of the keys contained in this
map. The set is backed by the map, so changes to the map are reflected
in the set, and vice-versa. The set supports element removal, which
removes the corresponding mapping from this map, via the
Iterator.remove
, Collection.remove
,removeAll,
retainAll
, and clear
operations. It does
not support the add
or addAll
operations.
- Specified by:
keySet
in interface Map<K,V>
- Returns:
- a set view of the keys contained in this map
(instance of
XCollection
).
public XIterator<K> reuseableKeyIterator()
XCollectionIterator.toFirst()
is called by this method to reset it
to the beginning.
public XIterator<V> reuseableValueIterator()
XCollectionIterator.toFirst()
is called by this method to reset it
to the beginning.
public XCollectionIterator<Map.Entry<K,V>> reuseableEntryIterator()
XCollectionIterator.toFirst()
is called by this method to reset it
to the beginning.
public final Map<K,V> unmodifiable()
unmodifiable().entrySet()
)
result in an UnsupportedOperationException
being thrown.
Unmodifiable XCollection
views of this map keys and values
are nonetheless obtainable (e.g. unmodifiable().keySet(),
unmodifiable().values()
).
- Returns:
- an unmodifiable view of this map.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |