Class TryThreadLocal<T>

java.lang.Object
java.lang.ThreadLocal<@Nullable T>
org.apache.calcite.util.TryThreadLocal<T>
Type Parameters:
T - Value type

public abstract class TryThreadLocal<T> extends ThreadLocal<@Nullable T>
Thread-local variable that returns a handle that can be closed.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Remembers to set the value back.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    get()
     
    void
    letIn(T t, Runnable runnable)
    Performs an action with this ThreadLocal set to a particular value in this thread, and restores the previous value afterward.
    <R> R
    letIn(T t, Supplier<R> supplier)
    Calls a Supplier with this ThreadLocal set to a particular value, in this thread, and restores the previous value afterward.
    static <S> TryThreadLocal<S>
    of(S initialValue)
    Creates a TryThreadLocal with a fixed initial value.
    static <S> TryThreadLocal<@NonNull S>
    ofNonNull(S initialValue)
    Creates a TryThreadLocal with a fixed initial value whose values are never null.
    push(T value)
    Assigns the value as value for the current thread.
    protected abstract void
    restoreTo(T previous)
    Sets the value back to a previous value.
    static <S> TryThreadLocal<@NonNull S>
    withInitial(Supplier<? extends @NonNull S> supplier)
    Creates a TryThreadLocal with a supplier for the initial value.

    Methods inherited from class java.lang.ThreadLocal

    initialValue, remove, set

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TryThreadLocal

      public TryThreadLocal()
  • Method Details

    • of

      public static <S> TryThreadLocal<S> of(S initialValue)
      Creates a TryThreadLocal with a fixed initial value.
      Parameters:
      initialValue - Initial value
    • ofNonNull

      public static <S> TryThreadLocal<@NonNull S> ofNonNull(S initialValue)
      Creates a TryThreadLocal with a fixed initial value whose values are never null.

      The value returned from get() is never null; the initial value must not be null; you must not call ThreadLocal.set(Object) with a null value.

      Parameters:
      initialValue - Initial value
    • withInitial

      public static <S> TryThreadLocal<@NonNull S> withInitial(Supplier<? extends @NonNull S> supplier)
      Creates a TryThreadLocal with a supplier for the initial value.

      The value returned from get() is never null; the supplier must never return null; you must not call ThreadLocal.set(Object) with a null value.

      Parameters:
      supplier - Supplier
    • get

      public T get()
      Overrides:
      get in class ThreadLocal<@Nullable T>
    • push

      public TryThreadLocal.Memo push(T value)
      Assigns the value as value for the current thread. Returns a TryThreadLocal.Memo which, when closed, will assign the value back to the previous value.
    • restoreTo

      protected abstract void restoreTo(T previous)
      Sets the value back to a previous value.
    • letIn

      public void letIn(T t, Runnable runnable)
      Performs an action with this ThreadLocal set to a particular value in this thread, and restores the previous value afterward.

      This method is named after the Standard ML let construct, for example let val x = 1 in x + 2 end.

    • letIn

      public <R> R letIn(T t, Supplier<R> supplier)
      Calls a Supplier with this ThreadLocal set to a particular value, in this thread, and restores the previous value afterward.

      This method is named after the Standard ML let construct, for example let val x = 1 in x + 2 end.