44import static datadog .context .ContextProviders .manager ;
55
66import javax .annotation .Nullable ;
7+ import javax .annotation .ParametersAreNonnullByDefault ;
78
89/**
910 * Immutable context scoped to an execution unit or carrier object.
1011 *
11- * <p>Each element of the context is accessible by its {@link ContextKey}. Keys represents product
12- * or functional areas and should be created sparingly. Elements in the context may themselves be
13- * mutable.
12+ * <p>There are three ways to get a Context instance:
13+ *
14+ * <ul>
15+ * <li>The first one is to retrieve the one from the current execution unit using {@link
16+ * #current()}. A Context instance can be marked as current using {@link #attach()} within the
17+ * execution unit.
18+ * <li>The second one is to retrieve one from a carrier object using {@link #from(Object
19+ * carrier)}. A Context instance would need to be attached to the carrier first using {@link
20+ * #attachTo(Object carrier)} attached.
21+ * <li>Finally, the third option is to get the default root Context instance calling {@link
22+ * #root()}.
23+ * </ul>
24+ *
25+ * <p>When there is no context attached to the current execution unit, {@link #current()} will
26+ * return the root context. Similarly, {@link #from(Object carrier)} will return the root context
27+ * when there is no context attached to the carrier.
28+ *
29+ * <p>From a {@link Context} instance, each value is stored and retrieved by its {@link ContextKey},
30+ * using {@link #with(ContextKey key, Object value)} to store a value (creating a new immutable
31+ * {@link Context} instance), and {@link #get(ContextKey)} to retrieve it. {@link ContextKey}s
32+ * represent product of functional areas, and should be created sparingly.
33+ *
34+ * <p>{@link Context} instances are thread safe as they are immutable (including their {@link
35+ * ContextKey}) but the value they hold may themselves be mutable.
36+ *
37+ * @see ContextKey
1438 */
39+ @ ParametersAreNonnullByDefault
1540public interface Context {
16-
1741 /**
1842 * Returns the root context.
1943 *
20- * <p>This is the initial local context that all contexts extend.
44+ * @return the initial local context that all contexts extend.
2145 */
2246 static Context root () {
2347 return manager ().root ();
@@ -26,7 +50,7 @@ static Context root() {
2650 /**
2751 * Returns the context attached to the current execution unit.
2852 *
29- * @return Attached context; {@link #root()} if there is none
53+ * @return the attached context; {@link #root()} if there is none.
3054 */
3155 static Context current () {
3256 return manager ().current ();
@@ -35,7 +59,7 @@ static Context current() {
3559 /**
3660 * Attaches this context to the current execution unit.
3761 *
38- * @return Scope to be closed when the context is invalid.
62+ * @return a scope to be closed when the context is invalid.
3963 */
4064 default ContextScope attach () {
4165 return manager ().attach (this );
@@ -44,7 +68,7 @@ default ContextScope attach() {
4468 /**
4569 * Swaps this context with the one attached to current execution unit.
4670 *
47- * @return Previously attached context; {@link #root()} if there was none
71+ * @return the previously attached context; {@link #root()} if there was none.
4872 */
4973 default Context swap () {
5074 return manager ().swap (this );
@@ -53,21 +77,27 @@ default Context swap() {
5377 /**
5478 * Returns the context attached to the given carrier object.
5579 *
56- * @return Attached context; {@link #root()} if there is none
80+ * @param carrier the carrier object to get the context from.
81+ * @return the attached context; {@link #root()} if there is none.
5782 */
5883 static Context from (Object carrier ) {
5984 return binder ().from (carrier );
6085 }
6186
62- /** Attaches this context to the given carrier object. */
87+ /**
88+ * Attaches this context to the given carrier object.
89+ *
90+ * @param carrier the object to carry the context.
91+ */
6392 default void attachTo (Object carrier ) {
6493 binder ().attachTo (carrier , this );
6594 }
6695
6796 /**
6897 * Detaches the context attached to the given carrier object, leaving it context-less.
6998 *
70- * @return Previously attached context; {@link #root()} if there was none
99+ * @param carrier the carrier object to detach its context from.
100+ * @return the previously attached context; {@link #root()} if there was none.
71101 */
72102 static Context detachFrom (Object carrier ) {
73103 return binder ().detachFrom (carrier );
@@ -76,24 +106,37 @@ static Context detachFrom(Object carrier) {
76106 /**
77107 * Gets the value stored in this context under the given key.
78108 *
79- * @return Value stored under the key; {@code null} if there is no value.
109+ * @param <T> the type of the value.
110+ * @param key the key used to store the value.
111+ * @return the value stored under the key; {@code null} if there is none.
80112 */
81113 @ Nullable
82114 <T > T get (ContextKey <T > key );
83115
84116 /**
85- * Creates a new context from the same elements, except the key is now mapped to the given value.
117+ * Creates a copy of this context with the given key-value set.
118+ *
119+ * <p>Existing value with the given key will be replaced, and mapping to a {@code null} value will
120+ * remove the key-value from the context copy.
86121 *
87- * @return New context with the key-value mapping.
122+ * @param <T> the type of the value.
123+ * @param key the key to store the value.
124+ * @param value the value to store.
125+ * @return a new context with the key-value set.
88126 */
89- <T > Context with (ContextKey <T > key , T value );
127+ <T > Context with (ContextKey <T > key , @ Nullable T value );
90128
91129 /**
92- * Creates a new context from the same elements, except the implicit key is mapped to this value.
130+ * Creates a copy of this context with the implicit key is mapped to the value.
93131 *
94- * @return New context with the implicitly keyed value.
132+ * @param value the value to store.
133+ * @return a new context with the implicitly keyed value set.
134+ * @see #with(ContextKey, Object)
95135 */
96- default Context with (ImplicitContextKeyed value ) {
136+ default Context with (@ Nullable ImplicitContextKeyed value ) {
137+ if (value == null ) {
138+ return root ();
139+ }
97140 return value .storeInto (this );
98141 }
99142}
0 commit comments