Class DefaultThreadContextMap
- java.lang.Object
-
- org.apache.logging.log4j.spi.DefaultThreadContextMap
-
- All Implemented Interfaces:
java.io.Serializable,ThreadContextMap,ReadOnlyStringMap
public class DefaultThreadContextMap extends java.lang.Object implements ThreadContextMap, ReadOnlyStringMap
The default implementation ofThreadContextMapAn instance of UnmodifiableArrayBackedMap can be represented as a single
Object[], which can safely be stored on theThreadLocalwith no fear of classloader-related memory leaks.Performance of the underlying
UnmodifiableArrayBackedMapexceedsHashMapin all supported operations other thanget(). Note thatget()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.StringINHERITABLE_MAPProperty name ("isThreadContextMapInheritable") for selectingInheritableThreadLocal(value "true") or plainThreadLocal(value is not "true") in the implementation.private java.lang.ThreadLocal<java.lang.Object[]>localStateprivate static longserialVersionUID
-
Constructor Summary
Constructors Constructor Description DefaultThreadContextMap()DefaultThreadContextMap(boolean ignored)Deprecated.Since 2.24.0.DefaultThreadContextMap(PropertiesUtil properties)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears the context.booleancontainsKey(java.lang.String key)Determines if the key is in the context.booleanequals(java.lang.Object obj)<V> voidforEach(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>
voidforEach(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.Stringget(java.lang.String key)Gets the context identified by thekeyparameter.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 ornullif the context map is empty.<V> VgetValue(java.lang.String key)Returns the value for the specified key, ornullif the specified key does not exist in this collection.inthashCode()booleanisEmpty()Returns true if the Map is empty.voidput(java.lang.String key, java.lang.String value)Puts a context value (theoparameter) as identified with thekeyparameter into the current thread's context map.voidputAll(java.util.Map<java.lang.String,java.lang.String> m)voidremove(java.lang.String key)Removes the context identified by thekeyparameter.voidremoveAll(java.lang.Iterable<java.lang.String> keys)intsize()Returns the number of key-value pairs in this collection.java.util.Map<java.lang.String,java.lang.String>toMap()Returns a non-nullmutableMap<String, String>containing a snapshot of this data structure.java.lang.StringtoString()
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
INHERITABLE_MAP
public static final java.lang.String INHERITABLE_MAP
Property name ("isThreadContextMapInheritable") for selectingInheritableThreadLocal(value "true") or plainThreadLocal(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. UseNoOpThreadContextMapfor 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:ThreadContextMapPuts a context value (theoparameter) as identified with thekeyparameter 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:
putin interfaceThreadContextMap- 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:ThreadContextMapGets the context identified by thekeyparameter.This method has no side effects.
- Specified by:
getin interfaceThreadContextMap- 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:ThreadContextMapRemoves the context identified by thekeyparameter.- Specified by:
removein interfaceThreadContextMap- Parameters:
key- The key to remove.
-
removeAll
public void removeAll(java.lang.Iterable<java.lang.String> keys)
- Since:
- 2.8
-
clear
public void clear()
Description copied from interface:ThreadContextMapClears the context.- Specified by:
clearin interfaceThreadContextMap
-
toMap
public java.util.Map<java.lang.String,java.lang.String> toMap()
Description copied from interface:ReadOnlyStringMapReturns a non-nullmutableMap<String, String>containing a snapshot of this data structure.- Specified by:
toMapin interfaceReadOnlyStringMap- 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:ThreadContextMapDetermines if the key is in the context.- Specified by:
containsKeyin interfaceReadOnlyStringMap- Specified by:
containsKeyin interfaceThreadContextMap- 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:ReadOnlyStringMapPerforms 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'sBiConsumer.accept(Object, Object)accept} method may cause aConcurrentModificationExceptionto be thrown.- Specified by:
forEachin interfaceReadOnlyStringMap- 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:ReadOnlyStringMapPerforms 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'sacceptmethod may cause aConcurrentModificationExceptionto be thrown.- Specified by:
forEachin interfaceReadOnlyStringMap- 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:ReadOnlyStringMapReturns the value for the specified key, ornullif the specified key does not exist in this collection.- Specified by:
getValuein interfaceReadOnlyStringMap- 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:
getCopyin interfaceThreadContextMap- Returns:
- a mutable copy of the context.
-
getImmutableMapOrNull
public java.util.Map<java.lang.String,java.lang.String> getImmutableMapOrNull()
Description copied from interface:ThreadContextMapReturns an immutable view on the context Map ornullif the context map is empty.- Specified by:
getImmutableMapOrNullin interfaceThreadContextMap- Returns:
- an immutable context Map or
null.
-
isEmpty
public boolean isEmpty()
Description copied from interface:ThreadContextMapReturns true if the Map is empty.- Specified by:
isEmptyin interfaceReadOnlyStringMap- Specified by:
isEmptyin interfaceThreadContextMap- Returns:
- true if the Map is empty, false otherwise.
-
size
public int size()
Description copied from interface:ReadOnlyStringMapReturns the number of key-value pairs in this collection.- Specified by:
sizein interfaceReadOnlyStringMap- Returns:
- the number of key-value pairs in this collection.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
-