class GarbageFreeSortedArrayThreadContextMap extends java.lang.Object implements ReadOnlyThreadContextMap, ObjectThreadContextMap
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.
| Modifier and Type | Field and Description |
|---|---|
protected static int |
DEFAULT_INITIAL_CAPACITY
The default initial capacity.
|
static java.lang.String |
INHERITABLE_MAP
Property name ("isThreadContextMapInheritable" ) for selecting
InheritableThreadLocal (value "true") or plain
ThreadLocal (value is not "true") in the implementation. |
protected java.lang.ThreadLocal<StringMap> |
localMap |
protected static java.lang.String |
PROPERTY_NAME_INITIAL_CAPACITY
System property name that can be used to control the data structure's initial capacity.
|
| Constructor and Description |
|---|
GarbageFreeSortedArrayThreadContextMap() |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clears the context.
|
boolean |
containsKey(java.lang.String key)
Determines if the key is in the context.
|
protected StringMap |
createStringMap()
Returns an implementation of the
StringMap used to back this thread context map. |
protected StringMap |
createStringMap(ReadOnlyStringMap original)
Returns an implementation of the
StringMap used to back this thread context map, pre-populated
with the contents of the specified context data. |
private java.lang.ThreadLocal<StringMap> |
createThreadLocalMap() |
boolean |
equals(java.lang.Object obj) |
java.lang.String |
get(java.lang.String key)
Gets the context identified by the
key parameter. |
java.util.Map<java.lang.String,java.lang.String> |
getCopy()
Gets a non-
null mutable 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 or
null if the context map is empty. |
StringMap |
getReadOnlyContextData()
Returns the context data for reading.
|
private StringMap |
getThreadLocalMap() |
java.lang.Object |
getValue(java.lang.String key)
Returns the Object value for the specified key, or
null if the specified key does not exist in this
collection. |
int |
hashCode() |
boolean |
isEmpty()
Returns true if the Map is empty.
|
void |
put(java.lang.String key,
java.lang.String value)
Puts a context value (the
o parameter) as identified
with the key parameter into the current thread's
context map. |
void |
putAll(java.util.Map<java.lang.String,java.lang.String> values)
Puts all given context map entries into the current thread's
context map.
|
<V> void |
putAllValues(java.util.Map<java.lang.String,V> values)
Puts all given key-value pairs into the collection.
|
void |
putValue(java.lang.String key,
java.lang.Object value)
Puts the specified key-value pair into the collection.
|
void |
remove(java.lang.String key)
Removes the the context identified by the
key
parameter. |
void |
removeAll(java.lang.Iterable<java.lang.String> keys)
Removes all given context map keys from the current thread's context map.
|
java.lang.String |
toString() |
public static final java.lang.String INHERITABLE_MAP
InheritableThreadLocal (value "true") or plain
ThreadLocal (value is not "true") in the implementation.protected static final int DEFAULT_INITIAL_CAPACITY
protected static final java.lang.String PROPERTY_NAME_INITIAL_CAPACITY
protected final java.lang.ThreadLocal<StringMap> localMap
public GarbageFreeSortedArrayThreadContextMap()
private java.lang.ThreadLocal<StringMap> createThreadLocalMap()
protected StringMap createStringMap()
StringMap used to back this thread context map.
Subclasses may override.
StringMap used to back this thread context mapprotected StringMap createStringMap(ReadOnlyStringMap original)
StringMap used to back this thread context map, pre-populated
with the contents of the specified context data.
Subclasses may override.
original - the key-value pairs to initialize the returned context data withStringMap used to back this thread context mapprivate StringMap getThreadLocalMap()
public void put(java.lang.String key,
java.lang.String value)
ThreadContextMapo 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.
put in interface ThreadContextMapkey - The key name.value - The key value.public void putValue(java.lang.String key,
java.lang.Object value)
ObjectThreadContextMapputValue in interface ObjectThreadContextMapkey - the key to add or remove. Keys may be null.value - the value to add. Values may be null.public void putAll(java.util.Map<java.lang.String,java.lang.String> values)
ThreadContextMap2If the current thread does not have a context map it is created as a side effect.
putAll in interface ThreadContextMap2values - The map.public <V> void putAllValues(java.util.Map<java.lang.String,V> values)
ObjectThreadContextMapputAllValues in interface ObjectThreadContextMapvalues - the map of key-value pairs to addpublic java.lang.String get(java.lang.String key)
ReadOnlyThreadContextMapkey parameter.
This method has no side effects.
get in interface ReadOnlyThreadContextMapget in interface ThreadContextMapkey - The key to locate.public java.lang.Object getValue(java.lang.String key)
ObjectThreadContextMapnull if the specified key does not exist in this
collection.getValue in interface ObjectThreadContextMapkey - the key whose value to returnnullpublic void remove(java.lang.String key)
ThreadContextMapkey
parameter.remove in interface ThreadContextMapkey - The key to remove.public void removeAll(java.lang.Iterable<java.lang.String> keys)
CleanableThreadContextMapIf the current thread does not have a context map it is created as a side effect.
removeAll in interface CleanableThreadContextMapkeys - The keys.public void clear()
ReadOnlyThreadContextMapclear in interface ReadOnlyThreadContextMapclear in interface ThreadContextMappublic boolean containsKey(java.lang.String key)
ReadOnlyThreadContextMapcontainsKey in interface ReadOnlyThreadContextMapcontainsKey in interface ThreadContextMapkey - The key to locate.public java.util.Map<java.lang.String,java.lang.String> getCopy()
ReadOnlyThreadContextMapnull mutable copy of current thread's context Map.getCopy in interface ReadOnlyThreadContextMapgetCopy in interface ThreadContextMappublic StringMap getReadOnlyContextData()
Thread safety note:
If this ReadOnlyThreadContextMap implements CopyOnWrite, then the returned StringMap can
safely be passed to another thread: future changes in the underlying context data will not be reflected in the
returned StringMap.
Otherwise, if this ReadOnlyThreadContextMap does not implement CopyOnWrite, then it is
not safe to pass the returned StringMap 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.
getReadOnlyContextData in interface ReadOnlyThreadContextMapgetReadOnlyContextData in interface ThreadContextMap2StringMap containing context data key-value pairspublic java.util.Map<java.lang.String,java.lang.String> getImmutableMapOrNull()
ReadOnlyThreadContextMapnull if the context map is empty.getImmutableMapOrNull in interface ReadOnlyThreadContextMapgetImmutableMapOrNull in interface ThreadContextMapnull.public boolean isEmpty()
ReadOnlyThreadContextMapisEmpty in interface ReadOnlyThreadContextMapisEmpty in interface ThreadContextMappublic java.lang.String toString()
toString in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Object