org.apache.logging.log4j.spi

Class DefaultThreadContextMap

  • All Implemented Interfaces:
    Serializable, ThreadContextMap, ReadOnlyStringMap


    public class DefaultThreadContextMap
    extends Object
    implements ThreadContextMap, ReadOnlyStringMap
    The actual ThreadContext Map. A new ThreadContext Map is created each time it is updated and the Map stored is always immutable. This means the Map can be passed to other threads without concern that it will be updated. Since it is expected that the Map will be passed to many more log events than the number of keys it contains the performance should be much better than if the Map was copied for each event.
    See Also:
    Serialized Form
    • Field Detail

      • INHERITABLE_MAP

        public static final 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
    • Constructor Detail

      • DefaultThreadContextMap

        public DefaultThreadContextMap()
      • DefaultThreadContextMap

        public DefaultThreadContextMap(boolean useMap)
    • Method Detail

      • put

        public void put(String key,
                        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.
      • get

        public String get(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(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.
      • toMap

        public Map<String,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.
      • forEach

        public <V> void forEach(BiConsumer<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<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(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.
      • 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.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object

Copyright © 1999-2023 The Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.