4
4
import static datadog .context .ContextProviders .manager ;
5
5
6
6
import javax .annotation .Nullable ;
7
+ import javax .annotation .ParametersAreNonnullByDefault ;
7
8
8
9
/**
9
10
* Immutable context scoped to an execution unit or carrier object.
10
11
*
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
14
38
*/
39
+ @ ParametersAreNonnullByDefault
15
40
public interface Context {
16
-
17
41
/**
18
42
* Returns the root context.
19
43
*
20
- * <p>This is the initial local context that all contexts extend.
44
+ * @return the initial local context that all contexts extend.
21
45
*/
22
46
static Context root () {
23
47
return manager ().root ();
@@ -26,7 +50,7 @@ static Context root() {
26
50
/**
27
51
* Returns the context attached to the current execution unit.
28
52
*
29
- * @return Attached context; {@link #root()} if there is none
53
+ * @return the attached context; {@link #root()} if there is none.
30
54
*/
31
55
static Context current () {
32
56
return manager ().current ();
@@ -35,7 +59,7 @@ static Context current() {
35
59
/**
36
60
* Attaches this context to the current execution unit.
37
61
*
38
- * @return Scope to be closed when the context is invalid.
62
+ * @return a scope to be closed when the context is invalid.
39
63
*/
40
64
default ContextScope attach () {
41
65
return manager ().attach (this );
@@ -44,7 +68,7 @@ default ContextScope attach() {
44
68
/**
45
69
* Swaps this context with the one attached to current execution unit.
46
70
*
47
- * @return Previously attached context; {@link #root()} if there was none
71
+ * @return the previously attached context; {@link #root()} if there was none.
48
72
*/
49
73
default Context swap () {
50
74
return manager ().swap (this );
@@ -53,21 +77,27 @@ default Context swap() {
53
77
/**
54
78
* Returns the context attached to the given carrier object.
55
79
*
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.
57
82
*/
58
83
static Context from (Object carrier ) {
59
84
return binder ().from (carrier );
60
85
}
61
86
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
+ */
63
92
default void attachTo (Object carrier ) {
64
93
binder ().attachTo (carrier , this );
65
94
}
66
95
67
96
/**
68
97
* Detaches the context attached to the given carrier object, leaving it context-less.
69
98
*
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.
71
101
*/
72
102
static Context detachFrom (Object carrier ) {
73
103
return binder ().detachFrom (carrier );
@@ -76,24 +106,37 @@ static Context detachFrom(Object carrier) {
76
106
/**
77
107
* Gets the value stored in this context under the given key.
78
108
*
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.
80
112
*/
81
113
@ Nullable
82
114
<T > T get (ContextKey <T > key );
83
115
84
116
/**
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.
86
121
*
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.
88
126
*/
89
- <T > Context with (ContextKey <T > key , T value );
127
+ <T > Context with (ContextKey <T > key , @ Nullable T value );
90
128
91
129
/**
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.
93
131
*
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)
95
135
*/
96
- default Context with (ImplicitContextKeyed value ) {
136
+ default Context with (@ Nullable ImplicitContextKeyed value ) {
137
+ if (value == null ) {
138
+ return root ();
139
+ }
97
140
return value .storeInto (this );
98
141
}
99
142
}
0 commit comments