Class GarbageFreeSortedArrayThreadContextMap
- java.lang.Object
-
- org.apache.logging.log4j.spi.GarbageFreeSortedArrayThreadContextMap
-
- All Implemented Interfaces:
CleanableThreadContextMap,ObjectThreadContextMap,ReadOnlyThreadContextMap,ThreadContextMap,ThreadContextMap2
class GarbageFreeSortedArrayThreadContextMap extends java.lang.Object implements ReadOnlyThreadContextMap, ObjectThreadContextMap
SortedArrayStringMap-based implementation of theThreadContextMapinterface 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
Fields Modifier and Type Field Description protected static intDEFAULT_INITIAL_CAPACITYThe default initial capacity.static java.lang.StringINHERITABLE_MAPProperty name ("isThreadContextMapInheritable" ) for selectingInheritableThreadLocal(value "true") or plainThreadLocal(value is not "true") in the implementation.private static booleaninheritableMapprivate static intinitialCapacityprotected java.lang.ThreadLocal<StringMap>localMapprotected static java.lang.StringPROPERTY_NAME_INITIAL_CAPACITYSystem property name that can be used to control the data structure's initial capacity.
-
Constructor Summary
Constructors Constructor Description GarbageFreeSortedArrayThreadContextMap()
-
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.protected StringMapcreateStringMap()Returns an implementation of theStringMapused to back this thread context map.protected StringMapcreateStringMap(ReadOnlyStringMap original)Returns an implementation of theStringMapused to back this thread context map, pre-populated with the contents of the specified context data.private java.lang.ThreadLocal<StringMap>createThreadLocalMap()booleanequals(java.lang.Object obj)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.StringMapgetReadOnlyContextData()Returns the context data for reading.private StringMapgetThreadLocalMap()<V> VgetValue(java.lang.String key)Returns the Object value for the specified key, ornullif the specified key does not exist in this collection.inthashCode()(package private) static voidinit()Initializes static variables based on system properties.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> values)Puts all given context map entries into the current thread's context map.<V> voidputAllValues(java.util.Map<java.lang.String,V> values)Puts all given key-value pairs into the collection.voidputValue(java.lang.String key, java.lang.Object value)Puts the specified key-value pair into the collection.voidremove(java.lang.String key)Removes the context identified by thekeyparameter.voidremoveAll(java.lang.Iterable<java.lang.String> keys)Removes all given context map keys from the current thread's context map.java.lang.StringtoString()
-
-
-
Field Detail
-
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
-
DEFAULT_INITIAL_CAPACITY
protected static final int DEFAULT_INITIAL_CAPACITY
The default initial capacity.- See Also:
- Constant Field Values
-
PROPERTY_NAME_INITIAL_CAPACITY
protected static final java.lang.String PROPERTY_NAME_INITIAL_CAPACITY
System property name that can be used to control the data structure's initial capacity.- See Also:
- Constant Field Values
-
localMap
protected final java.lang.ThreadLocal<StringMap> localMap
-
initialCapacity
private static volatile int initialCapacity
-
inheritableMap
private static volatile boolean inheritableMap
-
-
Method Detail
-
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
private java.lang.ThreadLocal<StringMap> createThreadLocalMap()
-
createStringMap
protected StringMap createStringMap()
Returns an implementation of theStringMapused to back this thread context map.Subclasses may override.
- Returns:
- an implementation of the
StringMapused to back this thread context map
-
createStringMap
protected StringMap createStringMap(ReadOnlyStringMap original)
Returns an implementation of theStringMapused 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
StringMapused to back this thread context map
-
getThreadLocalMap
private StringMap getThreadLocalMap()
-
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.
-
putValue
public void putValue(java.lang.String key, java.lang.Object value)Description copied from interface:ObjectThreadContextMapPuts the specified key-value pair into the collection.- Specified by:
putValuein interfaceObjectThreadContextMap- Parameters:
key- the key to add or remove. Keys may benull.value- the value to add. Values may benull.
-
putAll
public void putAll(java.util.Map<java.lang.String,java.lang.String> values)
Description copied from interface:ThreadContextMap2Puts 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:
putAllin interfaceThreadContextMap2- Parameters:
values- The map.
-
putAllValues
public <V> void putAllValues(java.util.Map<java.lang.String,V> values)
Description copied from interface:ObjectThreadContextMapPuts all given key-value pairs into the collection.- Specified by:
putAllValuesin interfaceObjectThreadContextMap- Parameters:
values- the map of key-value pairs to add
-
get
public java.lang.String get(java.lang.String key)
Description copied from interface:ReadOnlyThreadContextMapGets the context identified by thekeyparameter.This method has no side effects.
- Specified by:
getin interfaceReadOnlyThreadContextMap- Specified by:
getin interfaceThreadContextMap- Parameters:
key- The key to locate.- Returns:
- The value associated with the key or null.
-
getValue
public <V> V getValue(java.lang.String key)
Description copied from interface:ObjectThreadContextMapReturns the Object value for the specified key, ornullif the specified key does not exist in this collection.- Specified by:
getValuein interfaceObjectThreadContextMap- Parameters:
key- the key whose value to return- Returns:
- the value for the specified 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)
Description copied from interface:CleanableThreadContextMapRemoves 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:
removeAllin interfaceCleanableThreadContextMap- Parameters:
keys- The keys.
-
clear
public void clear()
Description copied from interface:ReadOnlyThreadContextMapClears the context.- Specified by:
clearin interfaceReadOnlyThreadContextMap- Specified by:
clearin interfaceThreadContextMap
-
containsKey
public boolean containsKey(java.lang.String key)
Description copied from interface:ReadOnlyThreadContextMapDetermines if the key is in the context.- Specified by:
containsKeyin interfaceReadOnlyThreadContextMap- Specified by:
containsKeyin interfaceThreadContextMap- Parameters:
key- The key to locate.- Returns:
- True if the key is in the context, false otherwise.
-
getCopy
public java.util.Map<java.lang.String,java.lang.String> getCopy()
Description copied from interface:ReadOnlyThreadContextMapGets a non-nullmutable copy of current thread's context Map.- Specified by:
getCopyin interfaceReadOnlyThreadContextMap- Specified by:
getCopyin interfaceThreadContextMap- Returns:
- a mutable copy of the context.
-
getReadOnlyContextData
public StringMap 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
ReadOnlyThreadContextMapimplementsCopyOnWrite, then the returnedStringMapcan safely be passed to another thread: future changes in the underlying context data will not be reflected in the returnedStringMap.Otherwise, if this
ReadOnlyThreadContextMapdoes not implementCopyOnWrite, then it is not safe to pass the returnedStringMapto 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:
getReadOnlyContextDatain interfaceReadOnlyThreadContextMap- Specified by:
getReadOnlyContextDatain interfaceThreadContextMap2- Returns:
- a
StringMapcontaining context data key-value pairs
-
getImmutableMapOrNull
public java.util.Map<java.lang.String,java.lang.String> getImmutableMapOrNull()
Description copied from interface:ReadOnlyThreadContextMapReturns an immutable view on the context Map ornullif the context map is empty.- Specified by:
getImmutableMapOrNullin interfaceReadOnlyThreadContextMap- Specified by:
getImmutableMapOrNullin interfaceThreadContextMap- Returns:
- an immutable context Map or
null.
-
isEmpty
public boolean isEmpty()
Description copied from interface:ReadOnlyThreadContextMapReturns true if the Map is empty.- Specified by:
isEmptyin interfaceReadOnlyThreadContextMap- Specified by:
isEmptyin interfaceThreadContextMap- Returns:
- true if the Map is empty, false otherwise.
-
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
-
-