Class TryThreadLocal<T>

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

public class TryThreadLocal<T> extends ThreadLocal<@Nullable T>
Thread-local variable that returns a handle that can be closed.
  • Method Details

    • of

      public static <T> TryThreadLocal<T> of(T initialValue)
      Creates a TryThreadLocal.
      Parameters:
      initialValue - Initial value
    • initialValue

      protected final T initialValue()
      Overrides:
      initialValue in class ThreadLocal<@Nullable T>
    • 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 void restoreTo(T previous)
      Sets the value back to a previous value.

      If the previous value was initialValue, calls ThreadLocal.remove(). There's no way to tell whether ThreadLocal.set(T) has been called previously, but the effect is the same.

    • 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 afterwards.

      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 afterwards.

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