Class ThreadContextTransient<T>

java.lang.Object
org.elasticsearch.common.util.concurrent.ThreadContextTransient<T>

public final class ThreadContextTransient<T> extends Object
A utility class for resolving/setting values in the ThreadContext in a typesafe way
See Also:
  • Method Details

    • getKey

      public String getKey()
      Returns:
      The key/name of the transient header
    • exists

      public boolean exists(ThreadContext threadContext)
      Returns:
      true if the thread context contains a non-null value for this key
    • get

      @Nullable public T get(ThreadContext threadContext)
      Returns:
      The current value for this key. May be null.
    • require

      public T require(ThreadContext threadContext)
      Returns:
      The current value for this key. May not be null
      Throws:
      IllegalStateException - if the thread context does not contain a value (or contains null).
    • set

      public void set(ThreadContext threadContext, T value)
      Set the value for the this key. Because transient headers cannot be overwritten, this method will throw an exception if a value already exists
      See Also:
    • setIfEmpty

      public boolean setIfEmpty(ThreadContext threadContext, T value)
      Set the value for the this key, if and only if there is no current value
      Returns:
      true if the value was set, false otherwise
    • transientValue

      public static <T> ThreadContextTransient<T> transientValue(String key, Class<T> type)