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. |
private static boolean |
inheritableMap |
private static int |
initialCapacity |
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() |
<V> V |
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() |
(package private) static void |
init()
Initializes static variables based on system properties.
|
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
private static volatile int initialCapacity
private static volatile boolean inheritableMap
public GarbageFreeSortedArrayThreadContextMap()
static void init()
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)
ThreadContextMap
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.
put
in interface ThreadContextMap
key
- The key name.value
- The key value.public void putValue(java.lang.String key, java.lang.Object value)
ObjectThreadContextMap
putValue
in interface ObjectThreadContextMap
key
- 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)
ThreadContextMap2
If the current thread does not have a context map it is created as a side effect.
putAll
in interface ThreadContextMap2
values
- The map.public <V> void putAllValues(java.util.Map<java.lang.String,V> values)
ObjectThreadContextMap
putAllValues
in interface ObjectThreadContextMap
values
- the map of key-value pairs to addpublic java.lang.String get(java.lang.String key)
ReadOnlyThreadContextMap
key
parameter.
This method has no side effects.
get
in interface ReadOnlyThreadContextMap
get
in interface ThreadContextMap
key
- The key to locate.public <V> V getValue(java.lang.String key)
ObjectThreadContextMap
null
if the specified key does not exist in this
collection.getValue
in interface ObjectThreadContextMap
key
- the key whose value to returnnull
public void remove(java.lang.String key)
ThreadContextMap
key
parameter.remove
in interface ThreadContextMap
key
- The key to remove.public void removeAll(java.lang.Iterable<java.lang.String> keys)
CleanableThreadContextMap
If the current thread does not have a context map it is created as a side effect.
removeAll
in interface CleanableThreadContextMap
keys
- The keys.public void clear()
ReadOnlyThreadContextMap
clear
in interface ReadOnlyThreadContextMap
clear
in interface ThreadContextMap
public boolean containsKey(java.lang.String key)
ReadOnlyThreadContextMap
containsKey
in interface ReadOnlyThreadContextMap
containsKey
in interface ThreadContextMap
key
- The key to locate.public java.util.Map<java.lang.String,java.lang.String> getCopy()
ReadOnlyThreadContextMap
null
mutable copy of current thread's context Map.getCopy
in interface ReadOnlyThreadContextMap
getCopy
in interface ThreadContextMap
public 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 ReadOnlyThreadContextMap
getReadOnlyContextData
in interface ThreadContextMap2
StringMap
containing context data key-value pairspublic java.util.Map<java.lang.String,java.lang.String> getImmutableMapOrNull()
ReadOnlyThreadContextMap
null
if the context map is empty.getImmutableMapOrNull
in interface ReadOnlyThreadContextMap
getImmutableMapOrNull
in interface ThreadContextMap
null
.public boolean isEmpty()
ReadOnlyThreadContextMap
isEmpty
in interface ReadOnlyThreadContextMap
isEmpty
in interface ThreadContextMap
public java.lang.String toString()
toString
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object