Class DefaultThreadContextMap

  • All Implemented Interfaces:
    java.io.Serializable, ThreadContextMap, ReadOnlyStringMap

    public class DefaultThreadContextMap
    extends java.lang.Object
    implements ThreadContextMap, ReadOnlyStringMap
    The default implementation of ThreadContextMap

    An instance of UnmodifiableArrayBackedMap can be represented as a single Object[], which can safely be stored on the ThreadLocal with no fear of classloader-related memory leaks.

    Performance of the underlying UnmodifiableArrayBackedMap exceeds HashMap in all supported operations other than get(). Note that get() performance scales linearly with the current map size, and callers are advised to minimize this work.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String INHERITABLE_MAP
      Property name ("isThreadContextMapInheritable") for selecting InheritableThreadLocal (value "true") or plain ThreadLocal (value is not "true") in the implementation.
      private java.lang.ThreadLocal<java.lang.Object[]> localState  
      private static long serialVersionUID  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the context.
      boolean containsKey​(java.lang.String key)
      Determines if the key is in the context.
      boolean equals​(java.lang.Object obj)  
      <V> void forEach​(BiConsumer<java.lang.String,​? super V> action)
      Performs the given action for each key-value pair in this data structure until all entries have been processed or the action throws an exception.
      <V,​S>
      void
      forEach​(TriConsumer<java.lang.String,​? super V,​S> action, S state)
      Performs the given action for each key-value pair in this data structure until all entries have been processed or the action throws an exception.
      java.lang.String get​(java.lang.String key)
      Gets the context identified by the key parameter.
      java.util.Map<java.lang.String,​java.lang.String> getCopy()
      {@return a mutable copy of the current thread context map}
      java.util.Map<java.lang.String,​java.lang.String> getImmutableMapOrNull()
      Returns an immutable view on the context Map or null if the context map is empty.
      <V> V getValue​(java.lang.String key)
      Returns the value for the specified key, or null if the specified key does not exist in this collection.
      int hashCode()  
      boolean isEmpty()
      Returns true if the Map is empty.
      void put​(java.lang.String key, java.lang.String value)
      Puts a context value (the o parameter) as identified with the key parameter into the current thread's context map.
      void putAll​(java.util.Map<java.lang.String,​java.lang.String> m)  
      void remove​(java.lang.String key)
      Removes the context identified by the key parameter.
      void removeAll​(java.lang.Iterable<java.lang.String> keys)  
      int size()
      Returns the number of key-value pairs in this collection.
      java.util.Map<java.lang.String,​java.lang.String> toMap()
      Returns a non-null mutable Map<String, String> containing a snapshot of this data structure.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • INHERITABLE_MAP

        public static final java.lang.String INHERITABLE_MAP
        Property name ("isThreadContextMapInheritable") for selecting InheritableThreadLocal (value "true") or plain ThreadLocal (value is not "true") in the implementation.
        See Also:
        Constant Field Values
      • localState

        private java.lang.ThreadLocal<java.lang.Object[]> localState
    • Constructor Detail

      • DefaultThreadContextMap

        public DefaultThreadContextMap()
      • DefaultThreadContextMap

        @Deprecated
        public DefaultThreadContextMap​(boolean ignored)
        Deprecated.
        Since 2.24.0. Use NoOpThreadContextMap for a no-op implementation.
      • DefaultThreadContextMap

        DefaultThreadContextMap​(PropertiesUtil properties)
    • Method Detail

      • put

        public void put​(java.lang.String key,
                        java.lang.String value)
        Description copied from interface: ThreadContextMap
        Puts a context value (the o parameter) as identified with the key parameter into the current thread's context map.

        If the current thread does not have a context map it is created as a side effect.

        Specified by:
        put in interface ThreadContextMap
        Parameters:
        key - The key name.
        value - The key value.
      • putAll

        public void putAll​(java.util.Map<java.lang.String,​java.lang.String> m)
      • get

        public java.lang.String get​(java.lang.String key)
        Description copied from interface: ThreadContextMap
        Gets the context identified by the key parameter.

        This method has no side effects.

        Specified by:
        get in interface ThreadContextMap
        Parameters:
        key - The key to locate.
        Returns:
        The value associated with the key or null.
      • remove

        public void remove​(java.lang.String key)
        Description copied from interface: ThreadContextMap
        Removes the context identified by the key parameter.
        Specified by:
        remove in interface ThreadContextMap
        Parameters:
        key - The key to remove.
      • removeAll

        public void removeAll​(java.lang.Iterable<java.lang.String> keys)
        Since:
        2.8
      • toMap

        public java.util.Map<java.lang.String,​java.lang.String> toMap()
        Description copied from interface: ReadOnlyStringMap
        Returns a non-null mutable Map<String, String> containing a snapshot of this data structure.
        Specified by:
        toMap in interface ReadOnlyStringMap
        Returns:
        a mutable copy of this data structure in Map<String, String> form.
      • containsKey

        public boolean containsKey​(java.lang.String key)
        Description copied from interface: ThreadContextMap
        Determines if the key is in the context.
        Specified by:
        containsKey in interface ReadOnlyStringMap
        Specified by:
        containsKey in interface ThreadContextMap
        Parameters:
        key - The key to locate.
        Returns:
        True if the key is in the context, false otherwise.
      • forEach

        public <V> void forEach​(BiConsumer<java.lang.String,​? super V> action)
        Description copied from interface: ReadOnlyStringMap
        Performs the given action for each key-value pair in this data structure until all entries have been processed or the action throws an exception.

        Some implementations may not support structural modifications (adding new elements or removing elements) while iterating over the contents. In such implementations, attempts to add or remove elements from the BiConsumer's BiConsumer.accept(Object, Object) accept} method may cause a ConcurrentModificationException to be thrown.

        Specified by:
        forEach in interface ReadOnlyStringMap
        Type Parameters:
        V - type of the value.
        Parameters:
        action - The action to be performed for each key-value pair in this collection.
      • forEach

        public <V,​S> void forEach​(TriConsumer<java.lang.String,​? super V,​S> action,
                                        S state)
        Description copied from interface: ReadOnlyStringMap
        Performs the given action for each key-value pair in this data structure until all entries have been processed or the action throws an exception.

        The third parameter lets callers pass in a stateful object to be modified with the key-value pairs, so the TriConsumer implementation itself can be stateless and potentially reusable.

        Some implementations may not support structural modifications (adding new elements or removing elements) while iterating over the contents. In such implementations, attempts to add or remove elements from the TriConsumer's accept method may cause a ConcurrentModificationException to be thrown.

        Specified by:
        forEach in interface ReadOnlyStringMap
        Type Parameters:
        V - type of the value.
        S - type of the third parameter.
        Parameters:
        action - The action to be performed for each key-value pair in this collection.
        state - the object to be passed as the third parameter to each invocation on the specified triconsumer.
      • getValue

        public <V> V getValue​(java.lang.String key)
        Description copied from interface: ReadOnlyStringMap
        Returns the value for the specified key, or null if the specified key does not exist in this collection.
        Specified by:
        getValue in interface ReadOnlyStringMap
        Parameters:
        key - the key whose value to return.
        Returns:
        the value for the specified key or null.
      • getCopy

        public java.util.Map<java.lang.String,​java.lang.String> getCopy()
        {@return a mutable copy of the current thread context map}
        Specified by:
        getCopy in interface ThreadContextMap
        Returns:
        a mutable copy of the context.
      • getImmutableMapOrNull

        public java.util.Map<java.lang.String,​java.lang.String> getImmutableMapOrNull()
        Description copied from interface: ThreadContextMap
        Returns an immutable view on the context Map or null if the context map is empty.
        Specified by:
        getImmutableMapOrNull in interface ThreadContextMap
        Returns:
        an immutable context Map or null.
      • size

        public int size()
        Description copied from interface: ReadOnlyStringMap
        Returns the number of key-value pairs in this collection.
        Specified by:
        size in interface ReadOnlyStringMap
        Returns:
        the number of key-value pairs in this collection.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object