| java.lang.Object javolution.util.FastMap
All known Subclasses: j2me.util.HashMap, j2me.util.LinkedHashMap,
FastMap | public class FastMap implements Map,Reusable,XMLSerializable,Realtime(Code) | | This class represents a hash map with real-time behavior;
smooth capacity increase and thread-safe without external
synchronization when
FastMap.isShared shared .
FastMap 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
FastMap.setShared(boolean) shared then all operations are
thread-safe including iterations over the map's collections.
Unlike ConcurrentHashMap ,
FastMap.get(Object) 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).
FastMap may use custom key comparators; the default comparator is
either
FastComparator.DIRECT DIRECT or
FastComparator.REHASH REHASH based upon the current Javolution
Configuration. Users may explicitly set the key comparator to
FastComparator.DIRECT DIRECT for optimum performance
when the hash codes are well distributed for all run-time platforms
(e.g. calculated hash codes).
Custom key comparators are extremely useful for value retrieval when
map's keys and argument keys are not of the same class (such as
String and
javolution.text.Text Text
(
FastComparator.LEXICAL LEXICAL )), to substitute more efficient
hash code calculations (
FastComparator.STRING STRING )
or for identity maps (
FastComparator.IDENTITY IDENTITY ):[code]
FastMap identityMap = new FastMap().setKeyComparator(FastComparator.IDENTITY);
[/code]
FastMap.Entry can quickly be iterated over (forward or backward)
without using iterators. For example:[code]
FastMap map = ...;
for (FastMap.Entry e = map.head(), end = map.tail(); (e = e.getNext()) != end;) {
String key = e.getKey(); // No typecast necessary.
Thread value = e.getValue(); // No typecast necessary.
}[/code]
Custom map implementations may override the
FastMap.newEntry method
in order to return their own
Entry implementation (with
additional fields for example).
FastMap are
Reusable reusable ; they maintain an
internal pool of Map.Entry objects. When an entry is removed
from a map, it is automatically restored to its pool (unless the map
is shared in which case the removed entry is candidate for garbage
collection as it cannot be safely recycled).
Shared maps do not use internal synchronization, except in case of
concurrent modifications of the map structure (entry 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). There are
typically used as lookup tables. For example:[code]
public class Unit {
static FastMap labels = new FastMap().setShared(true);
...
public String toString() {
String label = labels.get(this); // No synchronization.
return label != null ? label : makeLabel();
}
}[/code]
Implementation Note: To maintain time-determinism, rehash/resize
is performed only when the map's size is small (see chart). For large
maps (size > 512), the collection 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).
author: Jean-Marie Dautelle version: 5.2, September 11, 2007 |
Inner Class :public static class Entry implements Map.Entry,Record | |
Constructor Summary | |
public | FastMap() Creates a map whose capacity increment smoothly without large resize
operations. | public | FastMap(String id) Creates a persistent map associated to the specified unique identifier
(convenience method). | public | FastMap(int capacity) Creates a map of specified maximum size (a full resize may occur if the
specififed capacity is exceeded). | public | FastMap(Map map) Creates a map containing the specified entries, in the order they
are returned by the map iterator. |
Method Summary | |
final public void | clear() Removes all map's entries. | final public boolean | containsKey(Object key) Indicates if this map contains a mapping for the specified key.
Parameters: key - the key whose presence in this map is to be tested. | final public boolean | containsValue(Object value) Indicates if this map associates one or more keys to the specified value.
Parameters: value - the value whose presence in this map is to be tested. | final public Set | entrySet() Returns a
FastCollection view of the mappings contained in this
map. | public boolean | equals(Object obj) Compares the specified object with this map for equality.
Returns true if the given object is also a map and the two
maps represent the same mappings (regardless of collection iteration
order).
Parameters: obj - the object to be compared for equality with this map. | final public Object | get(Object key) Returns the value to which this map associates the specified key.
This method is always thread-safe regardless whether or not the map
is marked
FastMap.isShared() shared .
Parameters: key - the key whose associated value is to be returned. | final public Entry | getEntry(Object key) Returns the entry with the specified key.
This method is always thread-safe without synchronization.
Parameters: key - the key whose associated entry is to be returned. | public FastComparator | getKeyComparator() Returns the key comparator for this fast map. | public FastComparator | getValueComparator() Returns the value comparator for this fast map. | public int | hashCode() Returns the hash code value for this map. | final public Entry | head() Returns the head entry of this map. | final public boolean | isEmpty() Indicates if this map contains no key-value mappings. | public boolean | isShared() Indicates if this map supports concurrent operations without
synchronization (default unshared). | final public Set | keySet() Returns a
FastCollection view of the keys contained in this
map. | protected Entry | newEntry() Returns a new entry for this map; this method can be overriden by
custom map implementations. | public static FastMap | newInstance() Returns a potentially
FastMap.recycle recycled map instance. | public void | printStatistics(PrintStream out) Prints the current statistics on this map.
This method may help identify poorly defined hash functions.
The average distance should be less than 20% (most of the entries
are in their slots or close). | final public Object | put(Object key, Object value) Associates the specified value with the specified key in this map.
If this map previously contained a mapping for this key, the old value
is replaced. | final public void | putAll(Map map) Copies all of the mappings from the specified map to this map. | final public Object | putIfAbsent(Object key, Object value) Associates the specified value only if the specified key is not already
associated. | public static void | recycle(FastMap instance) Recycles the specified map instance. | final public Object | remove(Object key) Removes the entry for the specified key if present. | public void | reset() | public FastMap | setKeyComparator(FastComparator keyComparator) Sets the key comparator for this fast map.
Parameters: keyComparator - the key comparator. | public FastMap | setShared(boolean isShared) Sets the shared status of this map (whether the map is thread-safe
or not). | public FastMap | setValueComparator(FastComparator valueComparator) Sets the value comparator for this map.
Parameters: valueComparator - the value comparator. | final public int | size() Returns the number of key-value mappings in this
FastMap . | final public Entry | tail() Returns the tail entry of this map. | final public String | toString() Returns the String representation of this
FastMap . | public Text | toText() Returns the textual representation of this map. | final public Map | unmodifiable() Returns the unmodifiable view associated to this map.
Attempts to modify the returned map or to directly access its
(modifiable) map entries (e.g. | final public Collection | values() Returns a
FastCollection view of the values contained in this
map. |
ONE_VOLATILE | static volatile int ONE_VOLATILE(Code) | | |
FastMap | public FastMap()(Code) | | Creates a map whose capacity increment smoothly without large resize
operations.
|
FastMap | public FastMap(String id)(Code) | | Creates a persistent map associated to the specified unique identifier
(convenience method).
Parameters: id - the unique identifier for this map. throws: IllegalArgumentException - if the identifier is not unique. See Also: javolution.context.PersistentContext.Reference |
FastMap | public FastMap(int capacity)(Code) | | Creates a map of specified maximum size (a full resize may occur if the
specififed capacity is exceeded).
Parameters: capacity - the maximum capacity. |
FastMap | public FastMap(Map map)(Code) | | Creates a map containing the specified entries, in the order they
are returned by the map iterator.
Parameters: map - the map whose entries are to be placed into this map. |
clear | final public void clear()(Code) | | Removes all map's entries. The entries are removed and recycled;
unless this map is
FastMap.isShared 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 .
|
containsKey | final public boolean containsKey(Object key)(Code) | | Indicates if this map contains a mapping for the specified key.
Parameters: key - the key whose presence in this map is to be tested. true if this map contains a mapping for thespecified key; false otherwise. throws: NullPointerException - if the key is null . |
containsValue | final public boolean containsValue(Object value)(Code) | | Indicates if this map associates one or more keys to the specified value.
Parameters: value - the value whose presence in this map is to be tested. true if this map maps one or more keys to thespecified value. throws: NullPointerException - if the key is null . |
entrySet | final public Set entrySet()(Code) | | Returns a
FastCollection view of the mappings contained in this
map. Each element in the returned collection is a
FastMap.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.
a collection view of the mappings contained in this map(instance of FastCollection). |
equals | public boolean equals(Object obj)(Code) | | Compares the specified object with this map for equality.
Returns true if the given object is also a map and the two
maps represent the same mappings (regardless of collection iteration
order).
Parameters: obj - the object to be compared for equality with this map. true if the specified object is equal to this map;false otherwise. |
get | final public Object get(Object key)(Code) | | Returns the value to which this map associates the specified key.
This method is always thread-safe regardless whether or not the map
is marked
FastMap.isShared() shared .
Parameters: key - the key whose associated value is to be returned. the value to which this map maps the specified key, ornull if there is no mapping for the key. throws: NullPointerException - if key is null . |
getEntry | final public Entry getEntry(Object key)(Code) | | Returns the entry with the specified key.
This method is always thread-safe without synchronization.
Parameters: key - the key whose associated entry is to be returned. the entry for the specified key or null if none. |
getKeyComparator | public FastComparator getKeyComparator()(Code) | | Returns the key comparator for this fast map.
the key comparator. |
getValueComparator | public FastComparator getValueComparator()(Code) | | Returns the value comparator for this fast map.
the value comparator. |
hashCode | public int hashCode()(Code) | | Returns the hash code value for this map.
the hash code value for this map. |
head | final public Entry head()(Code) | | Returns the head entry of this map.
the entry such as head().getNext() holds the first map entry. |
isEmpty | final public boolean isEmpty()(Code) | | Indicates if this map contains no key-value mappings.
true if this map contains no key-value mappings;false otherwise. |
isShared | public boolean isShared()(Code) | | Indicates if this map supports concurrent operations without
synchronization (default unshared).
true if this map is thread-safe; false otherwise. |
keySet | final public Set keySet()(Code) | | Returns a
FastCollection 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.
a set view of the keys contained in this map(instance of FastCollection). |
newEntry | protected Entry newEntry()(Code) | | Returns a new entry for this map; this method can be overriden by
custom map implementations.
a new entry. |
newInstance | public static FastMap newInstance()(Code) | | Returns a potentially
FastMap.recycle recycled map instance.
a new, preallocated or recycled map instance. |
printStatistics | public void printStatistics(PrintStream out)(Code) | | Prints the current statistics on this map.
This method may help identify poorly defined hash functions.
The average distance should be less than 20% (most of the entries
are in their slots or close).
Parameters: out - the stream to use for output (e.g. System.out ) |
put | final public Object put(Object key, Object value)(Code) | | Associates the specified value with the specified key in this map.
If this map previously contained a mapping for this key, the old value
is replaced. For
FastMap.isShared() shared map, internal synchronization
is performed only when new entries are created.
Parameters: key - the key with which the specified value is to be associated. Parameters: value - the value to be associated with the specified key. the previous value associated with specified key, ornull if there was no mapping for key. Anull return can also indicate that the mappreviously associated null with the specified key. throws: NullPointerException - if the key is null . |
putAll | final public void putAll(Map map)(Code) | | Copies all of the mappings from the specified map to this map.
Parameters: map - the mappings to be stored in this map. throws: NullPointerException - the specified map is null ,or the specified map contains null keys. |
putIfAbsent | final public Object putIfAbsent(Object key, Object value)(Code) | | Associates the specified value only if the specified key is not already
associated. This is equivalent to:[code]
if (!map.containsKey(key))
return map.put(key, value);
else
return map.get(key);[/code]
except that for shared maps the action is performed atomically.
For shared maps, this method guarantees that if two threads try to
put the same key concurrently only one of them will succeed.
Parameters: key - the key with which the specified value is to be associated. Parameters: value - the value to be associated with the specified key. the previous value associated with specified key, ornull if there was no mapping for key. Anull return can also indicate that the mappreviously associated null with the specified key. throws: NullPointerException - if the key is null . |
recycle | public static void recycle(FastMap instance)(Code) | | Recycles the specified map instance.
Parameters: instance - the map instance to recycle. |
remove | final public Object remove(Object key)(Code) | | Removes the entry for the specified key if present. The entry
is recycled if the map is not marked as
FastMap.isShared 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 .
Parameters: key - the key whose mapping is to be removed from the map. previous value associated with specified key, ornull if there was no mapping for key. Anull return can also indicate that the mappreviously associated null with the specified key. throws: NullPointerException - if the key is null . |
reset | public void reset()(Code) | | |
setKeyComparator | public FastMap setKeyComparator(FastComparator keyComparator)(Code) | | Sets the key comparator for this fast map.
Parameters: keyComparator - the key comparator. this |
setShared | public FastMap setShared(boolean isShared)(Code) | | 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).
Parameters: isShared - true if this map is shared and thread-safe;false otherwise. this |
setValueComparator | public FastMap setValueComparator(FastComparator valueComparator)(Code) | | Sets the value comparator for this map.
Parameters: valueComparator - the value comparator. this |
size | final public int size()(Code) | | Returns the number of key-value mappings in this
FastMap .
Note: If concurrent updates are performed, application should not
rely upon the size during iterations.
this map's size. |
tail | final public Entry tail()(Code) | | Returns the tail entry of this map.
the entry such as tail().getPrevious() holds the last map entry. |
toString | final public String toString()(Code) | | Returns the String representation of this
FastMap .
toText().toString() |
toText | public Text toText()(Code) | | Returns the textual representation of this map.
the textual representation of the entry set. |
unmodifiable | final public Map unmodifiable()(Code) | | Returns the unmodifiable view associated to this map.
Attempts to modify the returned map or to directly access its
(modifiable) map entries (e.g. unmodifiable().entrySet() )
result in an
UnsupportedOperationException being thrown.
Unmodifiable
FastCollection views of this map keys and values
are nonetheless obtainable (e.g. unmodifiable().keySet(),
unmodifiable().values() ).
an unmodifiable view of this map. |
values | final public Collection values()(Code) | | Returns a
FastCollection 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.
a collection view of the values contained in this map (instance of FastCollection). |
|
|