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 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 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 static booleaninheritableMapprivate java.lang.ThreadLocal<java.util.Map<java.lang.String,java.lang.String>>localMapprivate static longserialVersionUIDprivate booleanuseMap
-
Constructor Summary
Constructors Constructor Description DefaultThreadContextMap()DefaultThreadContextMap(boolean useMap)
-
Method Summary
All Methods Static 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.(package private) static java.lang.ThreadLocal<java.util.Map<java.lang.String,java.lang.String>>createThreadLocalMap(boolean isMapEnabled)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()Gets a non-nullmutable copy of current thread's 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()(package private) static voidinit()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
-
useMap
private final boolean useMap
-
localMap
private final java.lang.ThreadLocal<java.util.Map<java.lang.String,java.lang.String>> localMap
-
inheritableMap
private static boolean inheritableMap
-
-
Method Detail
-
createThreadLocalMap
static java.lang.ThreadLocal<java.util.Map<java.lang.String,java.lang.String>> createThreadLocalMap(boolean isMapEnabled)
-
init
static void init()
-
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)
-
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()
Description copied from interface:ThreadContextMapGets a non-nullmutable copy of current thread's 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
-
-