Key for indexing values of type
T 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"));
...
}
}