Class GarbageFreeSortedArrayThreadContextMap
- All Implemented Interfaces:
CleanableThreadContextMap
,ObjectThreadContextMap
,ReadOnlyThreadContextMap
,ThreadContextMap
,ThreadContextMap2
SortedArrayStringMap
-based implementation of the ThreadContextMap
interface that attempts not to
create temporary objects. Adding and removing key-value pairs will not create temporary objects.
This implementation does not make a copy of its contents on every operation, so this data structure cannot be passed to log events. Instead, client code needs to copy the contents when interacting with another thread.
- Since:
- 2.7
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final int
The default initial capacity.static final String
Property name ("isThreadContextMapInheritable" ) for selectingInheritableThreadLocal
(value "true") or plainThreadLocal
(value is not "true") in the implementation.private static boolean
private static int
protected final ThreadLocal<StringMap>
protected static final String
System property name that can be used to control the data structure's initial capacity. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears the context.boolean
containsKey
(String key) Determines if the key is in the context.protected StringMap
Returns an implementation of theStringMap
used to back this thread context map.protected StringMap
createStringMap
(ReadOnlyStringMap original) Returns an implementation of theStringMap
used to back this thread context map, pre-populated with the contents of the specified context data.private ThreadLocal<StringMap>
boolean
Gets the context identified by thekey
parameter.getCopy()
Gets a non-null
mutable copy of current thread's context Map.Returns an immutable view on the context Map ornull
if the context map is empty.Returns the context data for reading.private StringMap
<V> V
Returns the Object value for the specified key, ornull
if the specified key does not exist in this collection.int
hashCode()
(package private) static void
init()
Initializes static variables based on system properties.boolean
isEmpty()
Returns true if the Map is empty.void
Puts a context value (theo
parameter) as identified with thekey
parameter into the current thread's context map.void
Puts all given context map entries into the current thread's context map.<V> void
putAllValues
(Map<String, V> values) Puts all given key-value pairs into the collection.void
Puts the specified key-value pair into the collection.void
Removes the context identified by thekey
parameter.void
Removes all given context map keys from the current thread's context map.toString()
-
Field Details
-
INHERITABLE_MAP
Property name ("isThreadContextMapInheritable" ) for selectingInheritableThreadLocal
(value "true") or plainThreadLocal
(value is not "true") in the implementation.- See Also:
-
DEFAULT_INITIAL_CAPACITY
protected static final int DEFAULT_INITIAL_CAPACITYThe default initial capacity.- See Also:
-
PROPERTY_NAME_INITIAL_CAPACITY
System property name that can be used to control the data structure's initial capacity.- See Also:
-
localMap
-
initialCapacity
private static volatile int initialCapacity -
inheritableMap
private static volatile boolean inheritableMap
-
-
Constructor Details
-
GarbageFreeSortedArrayThreadContextMap
public GarbageFreeSortedArrayThreadContextMap()
-
-
Method Details
-
init
static void init()Initializes static variables based on system properties. Normally called when this class is initialized by the VM and when Log4j is reconfigured. -
createThreadLocalMap
-
createStringMap
Returns an implementation of theStringMap
used to back this thread context map.Subclasses may override.
- Returns:
- an implementation of the
StringMap
used to back this thread context map
-
createStringMap
Returns an implementation of theStringMap
used to back this thread context map, pre-populated with the contents of the specified context data.Subclasses may override.
- Parameters:
original
- the key-value pairs to initialize the returned context data with- Returns:
- an implementation of the
StringMap
used to back this thread context map
-
getThreadLocalMap
-
put
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.
-
putValue
Description copied from interface:ObjectThreadContextMap
Puts the specified key-value pair into the collection.- Specified by:
putValue
in interfaceObjectThreadContextMap
- Parameters:
key
- the key to add or remove. Keys may benull
.value
- the value to add. Values may benull
.
-
putAll
Description copied from interface:ThreadContextMap2
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 interfaceThreadContextMap2
- Parameters:
values
- The map.
-
putAllValues
Description copied from interface:ObjectThreadContextMap
Puts all given key-value pairs into the collection.- Specified by:
putAllValues
in interfaceObjectThreadContextMap
- Parameters:
values
- the map of key-value pairs to add
-
get
Description copied from interface:ReadOnlyThreadContextMap
Gets the context identified by thekey
parameter.This method has no side effects.
- Specified by:
get
in interfaceReadOnlyThreadContextMap
- Specified by:
get
in interfaceThreadContextMap
- Parameters:
key
- The key to locate.- Returns:
- The value associated with the key or null.
-
getValue
Description copied from interface:ObjectThreadContextMap
Returns the Object value for the specified key, ornull
if the specified key does not exist in this collection.- Specified by:
getValue
in interfaceObjectThreadContextMap
- Parameters:
key
- the key whose value to return- Returns:
- the value for the specified key or
null
-
remove
Description copied from interface:ThreadContextMap
Removes the context identified by thekey
parameter.- Specified by:
remove
in interfaceThreadContextMap
- Parameters:
key
- The key to remove.
-
removeAll
Description copied from interface:CleanableThreadContextMap
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 interfaceCleanableThreadContextMap
- Parameters:
keys
- The keys.
-
clear
public void clear()Description copied from interface:ReadOnlyThreadContextMap
Clears the context.- Specified by:
clear
in interfaceReadOnlyThreadContextMap
- Specified by:
clear
in interfaceThreadContextMap
-
containsKey
Description copied from interface:ReadOnlyThreadContextMap
Determines if the key is in the context.- Specified by:
containsKey
in interfaceReadOnlyThreadContextMap
- Specified by:
containsKey
in interfaceThreadContextMap
- Parameters:
key
- The key to locate.- Returns:
- True if the key is in the context, false otherwise.
-
getCopy
Description copied from interface:ReadOnlyThreadContextMap
Gets a non-null
mutable copy of current thread's context Map.- Specified by:
getCopy
in interfaceReadOnlyThreadContextMap
- Specified by:
getCopy
in interfaceThreadContextMap
- Returns:
- a mutable copy of the context.
-
getReadOnlyContextData
Returns the context data for reading. Note that regardless of whether the returned context data has been frozen (made read-only) or not, callers should not attempt to modify the returned data structure.Thread safety note:
If this
ReadOnlyThreadContextMap
implementsCopyOnWrite
, then the returnedStringMap
can safely be passed to another thread: future changes in the underlying context data will not be reflected in the returnedStringMap
.Otherwise, if this
ReadOnlyThreadContextMap
does not implementCopyOnWrite
, then it is not safe to pass the returnedStringMap
to another thread because changes in the underlying context may be reflected in the returned object. It is the responsibility of the caller to make a copy to pass to another thread.- Specified by:
getReadOnlyContextData
in interfaceReadOnlyThreadContextMap
- Specified by:
getReadOnlyContextData
in interfaceThreadContextMap2
- Returns:
- a
StringMap
containing context data key-value pairs
-
getImmutableMapOrNull
Description copied from interface:ReadOnlyThreadContextMap
Returns an immutable view on the context Map ornull
if the context map is empty.- Specified by:
getImmutableMapOrNull
in interfaceReadOnlyThreadContextMap
- Specified by:
getImmutableMapOrNull
in interfaceThreadContextMap
- Returns:
- an immutable context Map or
null
.
-
isEmpty
public boolean isEmpty()Description copied from interface:ReadOnlyThreadContextMap
Returns true if the Map is empty.- Specified by:
isEmpty
in interfaceReadOnlyThreadContextMap
- Specified by:
isEmpty
in interfaceThreadContextMap
- Returns:
- true if the Map is empty, false otherwise.
-
toString
-
hashCode
public int hashCode() -
equals
-