Class AbstractThreadLocalContext<T>

    • Field Summary

      Fields 
      Modifier and Type Field 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.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractThreadLocalContext​(Class<? extends ContextManager<? super T>> contextManagerType, T newValue)
      Instantiates a new context with the specified value.
      protected AbstractThreadLocalContext​(T newValue)
      Deprecated.
      Using this constructor makes it impossible to register a ContextObserver!
    • Field Detail

      • parentContext

        protected final 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.
      • value

        protected final 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.
    • Constructor Detail

      • AbstractThreadLocalContext

        @Deprecated
        protected AbstractThreadLocalContext​(T newValue)
        Deprecated.
        Using this constructor makes it impossible to register a ContextObserver!
        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 (or null to 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 (or null to 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, or null if it was already closed.
        Specified by:
        getValue in interface Context<T>
        Returns:
        The value of this context.
      • 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).

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Specified by:
        close in interface Context<T>
      • toString

        public 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.
        Overrides:
        toString in class Object
        Returns:
        String representing this context class and either the current value or the fact that it was closed.
      • threadLocalInstanceOf

        protected static <T,​CTX extends AbstractThreadLocalContext<T>> ThreadLocal<CTX> threadLocalInstanceOf​(Class<? extends CTX> contextType)
        Returns the shared, static ThreadLocal instance 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-null shared ThreadLocal instance to register these contexts on.