- java.lang.Object
-
- org.apache.logging.log4j.spi.DefaultThreadContextMap
-
- All Implemented Interfaces:
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.
-
-
Constructor Summary
Constructors Constructor Description DefaultThreadContextMap()
DefaultThreadContextMap(boolean useMap)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears the context.boolean
containsKey(String key)
Determines if the key is in the context.boolean
equals(Object obj)
<V> void
forEach(BiConsumer<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<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.String
get(String key)
Gets the context identified by thekey
parameter.Map<String,String>
getCopy()
Gets a non-null
mutable copy of current thread's context Map.Map<String,String>
getImmutableMapOrNull()
Returns an immutable view on the context Map ornull
if the context map is empty.<V> V
getValue(String key)
Returns the Object value for the specified key, ornull
if the specified key does not exist in this collection.int
hashCode()
boolean
isEmpty()
Returns true if the Map is empty.void
put(String key, String value)
Puts a context value (theo
parameter) as identified with thekey
parameter into the current thread's context map.void
putAll(Map<String,String> m)
Puts all given context map entries into the current thread's context map.void
remove(String key)
Removes the context identified by thekey
parameter.void
removeAll(Iterable<String> keys)
Removes all given context map keys from the current thread's context map.int
size()
Returns the number of key-value pairs in this collection.Map<String,String>
toMap()
Returns a non-null
mutableMap<String, String>
containing a snapshot of this data structure.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.logging.log4j.spi.ThreadContextMap
getReadOnlyContextData, putAllValues, putValue
-
-
-
-
Method Detail
-
put
public void put(String key, String value)
Description copied from interface:ThreadContextMap
Puts a context value (theo
parameter) as identified with thekey
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 interfaceThreadContextMap
- Parameters:
key
- The key name.value
- The key value.
-
putAll
public void putAll(Map<String,String> m)
Description copied from interface:ThreadContextMap
Puts all given context map entries 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:
putAll
in interfaceThreadContextMap
- Parameters:
m
- The map.
-
get
public String get(String key)
Description copied from interface:ThreadContextMap
Gets the context identified by thekey
parameter.This method has no side effects.
- Specified by:
get
in interfaceThreadContextMap
- 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 thekey
parameter.- Specified by:
remove
in interfaceThreadContextMap
- Parameters:
key
- The key to remove.
-
removeAll
public void removeAll(Iterable<String> keys)
Description copied from interface:ThreadContextMap
Removes all given context map keys from 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:
removeAll
in interfaceThreadContextMap
- Parameters:
keys
- The keys.
-
clear
public void clear()
Description copied from interface:ThreadContextMap
Clears the context.- Specified by:
clear
in interfaceThreadContextMap
-
toMap
public Map<String,String> toMap()
Description copied from interface:ReadOnlyStringMap
Returns a non-null
mutableMap<String, String>
containing a snapshot of this data structure.- Specified by:
toMap
in interfaceReadOnlyStringMap
- Returns:
- a mutable copy of this data structure in
Map<String, String>
form.
-
containsKey
public boolean containsKey(String key)
Description copied from interface:ThreadContextMap
Determines if the key is in the context.- Specified by:
containsKey
in interfaceReadOnlyStringMap
- Specified by:
containsKey
in interfaceThreadContextMap
- Parameters:
key
- The key to locate.- Returns:
- True if the key is in the context, false otherwise.
-
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
'sBiConsumer.accept(Object, Object)
accept} method may cause aConcurrentModificationException
to be thrown.- Specified by:
forEach
in 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<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
'saccept
method may cause aConcurrentModificationException
to be thrown.- Specified by:
forEach
in 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(String key)
Description copied from interface:ThreadContextMap
Returns the Object value for the specified key, ornull
if the specified key does not exist in this collection.- Specified by:
getValue
in interfaceReadOnlyStringMap
- Specified by:
getValue
in interfaceThreadContextMap
- Type Parameters:
V
- The type of the returned value.- Parameters:
key
- the key whose value to return- Returns:
- the value for the specified key or
null
-
getCopy
public Map<String,String> getCopy()
Description copied from interface:ThreadContextMap
Gets a non-null
mutable copy of current thread's context Map.- Specified by:
getCopy
in interfaceThreadContextMap
- Returns:
- a mutable copy of the context.
-
getImmutableMapOrNull
public Map<String,String> getImmutableMapOrNull()
Description copied from interface:ThreadContextMap
Returns an immutable view on the context Map ornull
if the context map is empty.- Specified by:
getImmutableMapOrNull
in interfaceThreadContextMap
- Returns:
- an immutable context Map or
null
.
-
isEmpty
public boolean isEmpty()
Description copied from interface:ThreadContextMap
Returns true if the Map is empty.- Specified by:
isEmpty
in interfaceReadOnlyStringMap
- Specified by:
isEmpty
in interfaceThreadContextMap
- Returns:
- true if the Map is empty, false otherwise.
-
size
public int size()
Description copied from interface:ReadOnlyStringMap
Returns the number of key-value pairs in this collection.- Specified by:
size
in interfaceReadOnlyStringMap
- Returns:
- the number of key-value pairs in this collection.
-
-