Class AbstractThreadLocalContext<T>
- java.lang.Object
-
- nl.talsmasoftware.context.threadlocal.AbstractThreadLocalContext<T>
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Context<T>
public abstract class AbstractThreadLocalContext<T> extends Object implements Context<T>
Abstract base class maintaining a shared, staticThreadLocalinstance for each concrete subclass. This threadlocal can be accessed by subclasses through the protected method:threadLocalInstanceOf(Class).- Author:
- Sjoerd Talsma
-
-
Field Summary
Fields Modifier and Type Field Description protected Context<T>parentContextThe parent context that was active at the time this context was created (if any) ornullin case there was no active context when this context was created.protected TvalueThe actual value, so subclasses can still access it after the context has been closed, because the defaultgetValue()implementation will returnnullin 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.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractThreadLocalContext(Class<? extends ContextManager<? super T>> contextManagerType, T newValue)Instantiates a new context with the specified value.protectedAbstractThreadLocalContext(T newValue)Deprecated.Using this constructor makes it impossible to register aContextObserver!
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()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>>
CTXcurrent(Class<? extends CTX> contextType)TgetValue()Returns the value of this context instance, ornullif it was already closed.protected booleanisClosed()Returns whether this context is closed or not.protected static <T,CTX extends AbstractThreadLocalContext<T>>
ThreadLocal<CTX>threadLocalInstanceOf(Class<? extends CTX> contextType)Returns the shared, staticThreadLocalinstance for the specified context type.StringtoString()Returns the classname of this context followed by"{closed}"if it has been closed already; otherwise the containedvalueby this context will be added.
-
-
-
Field Detail
-
parentContext
protected final Context<T> parentContext
The parent context that was active at the time this context was created (if any) ornullin case there was no active context when this context was created.
-
value
protected final T value
The actual value, so subclasses can still access it after the context has been closed, because the defaultgetValue()implementation will returnnullin 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.
-
-
Constructor Detail
-
AbstractThreadLocalContext
@Deprecated protected AbstractThreadLocalContext(T newValue)
Deprecated.Using this constructor makes it impossible to register aContextObserver!Instantiates a new context with the specified value. The new context will be made the active context for the current thread.- Parameters:
newValue- The new value to become active in this new context (ornullto register a new context with 'no value').
-
AbstractThreadLocalContext
protected AbstractThreadLocalContext(Class<? extends ContextManager<? super T>> contextManagerType, T newValue)
Instantiates a new context with the specified value. The new context will be made the active context for the current thread.- Parameters:
contextManagerType- The context manager type (required to notify appropriate observers)newValue- The new value to become active in this new context (ornullto register a new context with 'no value').
-
-
Method Detail
-
isClosed
protected boolean isClosed()
Returns whether this context is closed or not.- Returns:
- Whether the context is already closed.
-
getValue
public T getValue()
Returns the value of this context instance, ornullif it was already closed.
-
close
public 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.This method has no side-effects if the context was already closed (it is safe to call multiple times).
-
toString
public String toString()
Returns the classname of this context followed by"{closed}"if it has been closed already; otherwise the containedvalueby this context will be added.
-
threadLocalInstanceOf
protected static <T,CTX extends AbstractThreadLocalContext<T>> ThreadLocal<CTX> threadLocalInstanceOf(Class<? extends CTX> contextType)
Returns the shared, staticThreadLocalinstance for the specified context type.- Type Parameters:
T- The type being managed by the context.CTX- The first non-abstract context subclass of AbstractThreadLocalContext.- Parameters:
contextType- The first concrete subclass of the abstract threadlocal context (So values from separate subclasses do not get mixed up).- Returns:
- The non-
nullsharedThreadLocalinstance to register these contexts on.
-
current
protected static <T,CTX extends AbstractThreadLocalContext<T>> CTX current(Class<? extends CTX> contextType)
-
-