Documentation of 'javolution37.javolution.util.LocalMap' Java class
LocalMap
javolution37.javolution.util

Class 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).

    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K,V>
    • Constructor Summary

      Constructors 
      Constructor and Description
      LocalMap()
      Default constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      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 keyComparator)
      Sets the key comparator for this local map.
      LocalMap<K,V> setValueComparator(FastComparator 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

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • LocalMap

        public LocalMap()
        Default constructor.
    • 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 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).

DMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.