J avolution v5.2 (J2SE 1.5+)

javolution.util
Class LocalMap<K,V>

java.lang.Object
  extended by 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 (locally scoped 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) and clear() is slightly modified (associate null values instead of removing the entries).

Version:
3.7, January 27, 2005
Author:
Jean-Marie Dautelle

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
LocalMap()
          Default constructor.
 
Method Summary
 void clear()
          Removes all mappings from this map (sets the local values to null).
 boolean containsKey(java.lang.Object key)
          Indicates if this map contains a mapping for the specified key.
 boolean containsValue(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 a FastCollection view of the mappings contained in this map.
 V get(java.lang.Object key)
          Returns the value to which this map associates the specified key.
 boolean isEmpty()
          Indicates if this map contains no key-value mappings.
 java.util.Set<K> keySet()
          Returns a FastCollection view of the keys contained in this map.
 V put(K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll(java.util.Map<? extends K,? extends V> map)
          Copies all of the mappings from the specified map to this map.
 V putDefault(K key, V defaultValue)
          Sets the default value for the specified key (typically done at initialization).
 V remove(java.lang.Object key)
          Removes the mapping for this key from this map if present (sets the local value to null).
 LocalMap<K,V> setKeyComparator(FastComparator<? super K> keyComparator)
          Sets the key comparator for this local map.
 LocalMap<K,V> setValueComparator(FastComparator<? super V> valueComparator)
          Sets the value comparator for this local map.
 int size()
          Returns the number of key-value mappings in this map.
 java.util.Collection<V> values()
          Returns a FastCollection view of the values contained in this map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

LocalMap

public LocalMap()
Default constructor.

Method Detail

setKeyComparator

public LocalMap<K,V> setKeyComparator(FastComparator<? super K> keyComparator)
Sets the key comparator for this local map.

Parameters:
keyComparator - the key comparator.
Returns:
this

setValueComparator

public LocalMap<K,V> setValueComparator(FastComparator<? super V> 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 null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key.
Throws:
java.lang.NullPointerException - if the key is null.

size

public int size()
Returns the number of key-value mappings in this map.

Specified by:
size in interface java.util.Map<K,V>
Returns:
this map's size.

isEmpty

public boolean isEmpty()
Indicates if this map contains no key-value mappings.

Specified by:
isEmpty in interface java.util.Map<K,V>
Returns:
true if this map contains no key-value mappings; false otherwise.

containsKey

public boolean containsKey(java.lang.Object key)
Indicates if this map contains a mapping for the specified key.

Specified by:
containsKey in interface java.util.Map<K,V>
Parameters:
key - the key whose presence in this map is to be tested.
Returns:
true if this map contains a mapping for the specified key; false otherwise.
Throws:
java.lang.NullPointerException - if the key is null.

containsValue

public boolean containsValue(java.lang.Object value)
Indicates if this map associates one or more keys to the specified value.

Specified by:
containsValue in interface java.util.Map<K,V>
Parameters:
value - the value whose presence in this map is to be tested.
Returns:
true if this map maps one or more keys to the specified value.
Throws:
java.lang.NullPointerException - if the key is null.

get

public V get(java.lang.Object key)
Returns the value to which this map associates the specified key.

Specified by:
get in interface java.util.Map<K,V>
Parameters:
key - the key whose associated value is to be returned.
Returns:
the value to which this map maps the specified key, or null if there is no mapping for the key.
Throws:
java.lang.NullPointerException - if key is null.

put

public V put(K key,
             V value)
Associates the specified value with the specified key in this map.

Specified by:
put in interface java.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 null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key.
Throws:
java.lang.NullPointerException - if the key is null.

putAll

public void putAll(java.util.Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map.

Specified by:
putAll in interface java.util.Map<K,V>
Parameters:
map - the mappings to be stored in this map.
Throws:
java.lang.NullPointerException - the specified map is null, or the specified map contains null keys.

remove

public V remove(java.lang.Object key)
Removes the mapping for this key from this map if present (sets the local value to null).

Specified by:
remove in interface java.util.Map<K,V>
Parameters:
key - the key whose value is set to null
Returns:
put(key, null)
Throws:
java.lang.NullPointerException - if the key is null.

clear

public void clear()
Removes all mappings from this map (sets the local values to null).

Specified by:
clear in interface java.util.Map<K,V>

keySet

public java.util.Set<K> keySet()
Returns a FastCollection view of the keys contained in this map.

Specified by:
keySet in interface java.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 a FastCollection view of the values contained in this map.

Specified by:
values in interface java.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 a FastCollection view of the mappings contained in this map.

Specified by:
entrySet in interface java.util.Map<K,V>
Returns:
a collection view of the mappings contained in this map (instance of FastCollection).

J avolution v5.2 (J2SE 1.5+)

Copyright © 2005 - 2007 Javolution.