public abstract class AbstractThreadLocalContext<T> extends Object implements Context<T>
ThreadLocal instance for each concrete
subclass of this type. This threadlocal can be accessed by subclasses through the protected method:
threadLocalInstanceOf(Class).| Modifier and Type | Field and Description |
|---|---|
protected Context<T> |
parentContext
The parent context that was active at the time this context was created (if any)
or
null in case there was no active context when this context was created. |
protected T |
value
The actual value, so subclasses can still access it after the context has been closed,
because the default
getValue() implementation will return null in that case.Please be careful accessing the value after the context was closed: There is no pre-defined meaningful way to handle this situation, as this depends heavily on the desired features of the particular implementation. |
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractThreadLocalContext(T newValue)
Instantiates a new context with the specified value.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this context and in case this context is the active context,
restores the active context to the (unclosed) parent context.
If no unclosed parent context exists, the 'active context' is cleared. |
protected static <T,CTX extends AbstractThreadLocalContext<T>> |
current(Class<? extends CTX> contextType) |
T |
getValue()
Returns the value of this context instance, or
null if it was already closed. |
protected boolean |
isClosed()
Returns whether this context is closed or not.
|
protected static <T,CTX extends AbstractThreadLocalContext<T>> |
threadLocalInstanceOf(Class<? extends CTX> contextType)
Returns the shared, static
ThreadLocal instance for the specified context type. |
String |
toString()
Returns the classname of this context followed by
"{closed}" if it has been closed already;
otherwise the contained value by this context will be added. |
protected final Context<T> parentContext
null in case there was no active context when this context was created.protected final T value
getValue() implementation will return null in that case.protected AbstractThreadLocalContext(T newValue)
newValue - The new value to become active in this new context
(or null to register a new context with 'no value').protected boolean isClosed()
public T getValue()
null if it was already closed.public void close()
This method has no side-effects if the context was already closed (it is safe to call multiple times).
public String toString()
"{closed}" if it has been closed already;
otherwise the contained value by this context will be added.protected static <T,CTX extends AbstractThreadLocalContext<T>> ThreadLocal<CTX> threadLocalInstanceOf(Class<? extends CTX> contextType)
ThreadLocal instance for the specified context type.T - The type being managed by the context.CTX - The first non-abstract context subclass of AbstractThreadLocalContext.contextType - The first concrete subclass of the abstract threadlocal context
(So values from separate subclasses do not get mixed up).null shared ThreadLocal instance to register these contexts on.protected static <T,CTX extends AbstractThreadLocalContext<T>> CTX current(Class<? extends CTX> contextType)
Copyright © 2016–2017 Talsma ICT. All rights reserved.