Class LocalMap<K,V>
- java.lang.Object
-
- javolution37.javolution.util.LocalMap<K,V>
-
- All Implemented Interfaces:
- java.util.Map<K,V>
public final class LocalMap<K,V> extends java.lang.Object implements java.util.Map<K,V>This class represents a map which can be temporarily modified without impacting other threads (
locallyscoped changes).Operation on instances of this class are completely thread-safe. For example: public class XmlFormat { static LocalMap
CLASS_TO_FORMAT = new LocalMap (); public static void setFormat(Class forClass, XmlFormat that) { CLASS_TO_FORMAT.put(forClass, that); // No synchronization required. } public static XmlFormat getInstance(Class forClass) { return CLASS_TO_FORMAT.get(forClass); // No synchronization required. } } public void main(String[] args) { // Sets default (global settings). XmlFormat.setFormat(Foo.class, xFormat); XmlFormat.setFormat(Bar.class, yFormat); } ... // Another thread. LocalContext.enter(); try { // Use of local context to avoid impacting other threads. XmlFormat.setFormat(Foo.class, zFormat); XmlFormat.getInstance(Foo.class); // Returns zFormat XmlFormat.getInstance(Bar.class); // Returns yFormat (inherited) } finally { LocalContext.exit(); } getInstance(Foo.class); // Returns xFormat [/code] Note: Because key-value mappings are inherited, the semantic of
remove(java.lang.Object)andclear()is slightly modified (associatenullvalues instead of removing the entries).
-
-
Constructor Summary
Constructors Constructor and Description LocalMap()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidclear()Removes all mappings from this map (sets the local values tonull).booleancontainsKey(java.lang.Object key)Indicates if this map contains a mapping for the specified key.booleancontainsValue(java.lang.Object value)Indicates if this map associates one or more keys to the specified value.java.util.Set<java.util.Map.Entry<K,V>>entrySet()Returns aFastCollectionview of the mappings contained in this map.Vget(java.lang.Object key)Returns the value to which this map associates the specified key.booleanisEmpty()Indicates if this map contains no key-value mappings.java.util.Set<K>keySet()Returns aFastCollectionview of the keys contained in this map.Vput(K key, V value)Associates the specified value with the specified key in this map.voidputAll(java.util.Map<? extends K,? extends V> map)Copies all of the mappings from the specified map to this map.VputDefault(K key, V defaultValue)Sets the default value for the specified key (typically done at initialization).Vremove(java.lang.Object key)Removes the mapping for this key from this map if present (sets the local value tonull).LocalMap<K,V>setKeyComparator(FastComparator keyComparator)Sets the key comparator for this local map.LocalMap<K,V>setValueComparator(FastComparator valueComparator)Sets the value comparator for this local map.intsize()Returns the number of key-value mappings in this map.java.util.Collection<V>values()Returns aFastCollectionview of the values contained in this map.
-
-
-
Method Detail
-
setKeyComparator
public LocalMap<K,V> setKeyComparator(FastComparator keyComparator)
Sets the key comparator for this local map.- Parameters:
keyComparator- the key comparator.- Returns:
this
-
setValueComparator
public LocalMap<K,V> setValueComparator(FastComparator valueComparator)
Sets the value comparator for this local map.- Parameters:
valueComparator- the value comparator.- Returns:
this
-
putDefault
public V putDefault(K key, V defaultValue)
Sets the default value for the specified key (typically done at initialization).- Parameters:
key- the key with which the specified value is to be associated.defaultValue- the default value to be associated with the specified key.- Returns:
- the previous default value associated with specified key, or
nullif there was no mapping for key. Anullreturn can also indicate that the map previously associatednullwith the specified key. - Throws:
java.lang.NullPointerException- if the key isnull.
-
size
public int size()
Returns the number of key-value mappings in this map.
-
isEmpty
public boolean isEmpty()
Indicates if this map contains no key-value mappings.
-
containsKey
public boolean containsKey(java.lang.Object key)
Indicates if this map contains a mapping for the specified key.
-
containsValue
public boolean containsValue(java.lang.Object value)
Indicates if this map associates one or more keys to the specified value.
-
get
public V get(java.lang.Object key)
Returns the value to which this map associates the specified key.
-
put
public V put(K key, V value)
Associates the specified value with the specified key in this map.- Specified by:
putin interfacejava.util.Map<K,V>- Parameters:
key- the key with which the specified value is to be associated.value- the value to be associated with the specified key.- Returns:
- the previous value associated with specified key, or
nullif there was no mapping for key. Anullreturn can also indicate that the map previously associatednullwith the specified key. - Throws:
java.lang.NullPointerException- if the key isnull.
-
putAll
public void putAll(java.util.Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map.
-
remove
public V remove(java.lang.Object key)
Removes the mapping for this key from this map if present (sets the local value tonull).
-
clear
public void clear()
Removes all mappings from this map (sets the local values tonull).
-
keySet
public java.util.Set<K> keySet()
Returns aFastCollectionview of the keys contained in this map.- Specified by:
keySetin interfacejava.util.Map<K,V>- Returns:
- a set view of the keys contained in this map
(instance of
FastCollection).
-
values
public java.util.Collection<V> values()
Returns aFastCollectionview of the values contained in this map.- Specified by:
valuesin interfacejava.util.Map<K,V>- Returns:
- a collection view of the values contained in this map
(instance of
FastCollection).
-
entrySet
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Returns aFastCollectionview of the mappings contained in this map.- Specified by:
entrySetin interfacejava.util.Map<K,V>- Returns:
- a collection view of the mappings contained in this map
(instance of
FastCollection).
-
-
DMelt 3.0 © DataMelt by jWork.ORG