Interface ContextKey<T>


public interface ContextKey<T>
Key for indexing values of type ContextKey stored in a Context. ContextKey are compared by reference, so it is expected that only one ContextKey is created for a particular type of context value.

 public class ContextUser {

   private static final ContextKey<MyState> KEY = ContextKey.named("MyState");

   public Context startWork() {
     return Context.withValues(KEY, new MyState());
   }

   public void continueWork(Context context) {
     MyState state = context.get(KEY);
     // Keys are compared by reference only.
     assert state != Context.current().get(ContextKey.named("MyState"));
     ...
   }
 }

 
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static <T> ContextKey<T>
    named(String name)
    Returns a new ContextKey with the given debug name.
  • Method Details

    • named

      static <T> ContextKey<T> named(String name)
      Returns a new ContextKey with the given debug name. The name does not impact behavior and is only for debugging purposes. Multiple different keys with the same name will be separate keys.