Top Description Fields Constructors Methods
org.apache.logging.log4j.spi

public Class DefaultThreadContextMap

extends Object
implements ThreadContextMap, ReadOnlyStringMap
Class Inheritance
All Implemented Interfaces
org.apache.logging.log4j.util.ReadOnlyStringMap, java.io.Serializable, org.apache.logging.log4j.spi.ThreadContextMap
Imports
java.util.Collections, .HashMap, .Map, .Objects, org.apache.logging.log4j.util.BiConsumer, .PropertiesUtil, .ReadOnlyStringMap, .TriConsumer

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.

Field Summary

Modifier and TypeField and Description
public static final String
INHERITABLE_MAP

Property name ( ) for selecting InheritableThreadLocal (value "true") or plain ThreadLocal (value is not "true") in the implementation.

private static boolean
private final ThreadLocal<Map<String, String>>
private static final long
private final boolean

Constructor Summary

AccessConstructor and Description
public
public
DefaultThreadContextMap(final boolean useMap)

Method Summary

Modifier and TypeMethod and Description
public void
public boolean
pack-priv static ThreadLocal<Map<String, String>>
createThreadLocalMap(final boolean isMapEnabled)

public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public <V> void
forEach(final BiConsumer<String, ? super V>
The action to be performed for each key-value pair in this collection.
action
)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

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.

public <V, S> void
forEach(final TriConsumer<String, ? super V, S>
The action to be performed for each key-value pair in this collection.
action
,
final S
the object to be passed as the third parameter to each invocation on the specified triconsumer.
state
)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

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.

public String
get(final String
The key to locate.
key
)

Implements org.apache.logging.log4j.spi.ThreadContextMap.get.

Gets the context identified by the key parameter.

public Map<String, String>
getCopy()

Implements org.apache.logging.log4j.spi.ThreadContextMap.getCopy.

Gets a non-null mutable copy of current thread's context Map.

public Map<String, String>
getImmutableMapOrNull()

Implements org.apache.logging.log4j.spi.ThreadContextMap.getImmutableMapOrNull.

Returns an immutable view on the context Map or null if the context map is empty.

public <V> V
getValue(final String
the key whose value to return.
key
)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.getValue.

Returns the value for the specified key, or null if the specified key does not exist in this collection.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

pack-priv static void
init()

public boolean
public void
put(final String
The key name.
key
,
final String
The key value.
value
)

Implements org.apache.logging.log4j.spi.ThreadContextMap.put.

Puts a context value (the o parameter) as identified with the key parameter into the current thread's context map.

public void
putAll(final Map<String, String> m)

public void
remove(final String
The key to remove.
key
)

Implements org.apache.logging.log4j.spi.ThreadContextMap.remove.

Removes the context identified by the key parameter.

public void
removeAll(final Iterable<String> keys)

public int
size()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.size.

Returns the number of key-value pairs in this collection.

public Map<String, String>
toMap()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.toMap.

Returns a non-null mutable Map<String, String> containing a snapshot of this data structure.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

INHERITABLE_MAPback to summary
public static final String INHERITABLE_MAP

Property name ( ) for selecting InheritableThreadLocal (value "true") or plain ThreadLocal (value is not "true") in the implementation.

inheritableMapback to summary
private static boolean inheritableMap
localMapback to summary
private final ThreadLocal<Map<String, String>> localMap
serialVersionUIDback to summary
private static final long serialVersionUID
useMapback to summary
private final boolean useMap

Constructor Detail

DefaultThreadContextMapback to summary
public DefaultThreadContextMap()
DefaultThreadContextMapback to summary
public DefaultThreadContextMap(final boolean useMap)

Method Detail

clearback to summary
public void clear()

Implements org.apache.logging.log4j.spi.ThreadContextMap.clear.

Doc from org.apache.logging.log4j.spi.ThreadContextMap.clear.

Clears the context.

Annotations
@Override
containsKeyback to summary
public boolean containsKey(final String key)

Implements org.apache.logging.log4j.spi.ThreadContextMap.containsKey, org.apache.logging.log4j.util.ReadOnlyStringMap.containsKey.

Doc from org.apache.logging.log4j.spi.ThreadContextMap.containsKey.

Determines if the key is in the context.

Parameters
key:String

The key to locate.

Returns:boolean

True if the key is in the context, false otherwise.

Annotations
@Override
createThreadLocalMapback to summary
pack-priv static ThreadLocal<Map<String, String>> createThreadLocalMap(final boolean isMapEnabled)
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
forEachback to summary
public <V> void forEach(final BiConsumer<String, ? super V> action)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

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.

Parameters
<V>
type of the value.
action:BiConsumer<String, ? super V>

The action to be performed for each key-value pair in this collection.

Annotations
@Override
forEachback to summary
public <V, S> void forEach(final TriConsumer<String, ? super V, S> action, final S state)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

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.

Parameters
<V>
type of the value.
<S>
type of the third parameter.
action:TriConsumer<String, ? super V, S>

The action to be performed for each key-value pair in this collection.

state:S

the object to be passed as the third parameter to each invocation on the specified triconsumer.

Annotations
@Override
getback to summary
public String get(final String key)

Implements org.apache.logging.log4j.spi.ThreadContextMap.get.

Doc from org.apache.logging.log4j.spi.ThreadContextMap.get.

Gets the context identified by the key parameter.

This method has no side effects.

Parameters
key:String

The key to locate.

Returns:String

The value associated with the key or null.

Annotations
@Override
getCopyback to summary
public Map<String, String> getCopy()

Implements org.apache.logging.log4j.spi.ThreadContextMap.getCopy.

Doc from org.apache.logging.log4j.spi.ThreadContextMap.getCopy.

Gets a non-null mutable copy of current thread's context Map.

Returns:Map<String, String>

a mutable copy of the context.

Annotations
@Override
getImmutableMapOrNullback to summary
public Map<String, String> getImmutableMapOrNull()

Implements org.apache.logging.log4j.spi.ThreadContextMap.getImmutableMapOrNull.

Doc from org.apache.logging.log4j.spi.ThreadContextMap.getImmutableMapOrNull.

Returns an immutable view on the context Map or null if the context map is empty.

Returns:Map<String, String>

an immutable context Map or null.

Annotations
@Override
getValueback to summary
public <V> V getValue(final String key)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.getValue.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.getValue.

Returns the value for the specified key, or null if the specified key does not exist in this collection.

Parameters
key:String

the key whose value to return.

Returns:V

the value for the specified key or null.

Annotations
@SuppressWarnings:unchecked
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
initback to summary
pack-priv static void init()
isEmptyback to summary
public boolean isEmpty()

Implements org.apache.logging.log4j.spi.ThreadContextMap.isEmpty, org.apache.logging.log4j.util.ReadOnlyStringMap.isEmpty.

Doc from org.apache.logging.log4j.spi.ThreadContextMap.isEmpty.

Returns true if the Map is empty.

Returns:boolean

true if the Map is empty, false otherwise.

Annotations
@Override
putback to summary
public void put(final String key, final String value)

Implements org.apache.logging.log4j.spi.ThreadContextMap.put.

Doc from org.apache.logging.log4j.spi.ThreadContextMap.put.

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.

Parameters
key:String

The key name.

value:String

The key value.

Annotations
@Override
putAllback to summary
public void putAll(final Map<String, String> m)
removeback to summary
public void remove(final String key)

Implements org.apache.logging.log4j.spi.ThreadContextMap.remove.

Doc from org.apache.logging.log4j.spi.ThreadContextMap.remove.

Removes the context identified by the key parameter.

Parameters
key:String

The key to remove.

Annotations
@Override
removeAllback to summary
public void removeAll(final Iterable<String> keys)
sizeback to summary
public int size()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.size.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.size.

Returns the number of key-value pairs in this collection.

Returns:int

the number of key-value pairs in this collection.

Annotations
@Override
toMapback to summary
public Map<String, String> toMap()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.toMap.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.toMap.

Returns a non-null mutable Map<String, String> containing a snapshot of this data structure.

Returns:Map<String, String>

a mutable copy of this data structure in Map<String, String> form.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override