Skip to content

Commit 2f85f53

Browse files
authored
javadoc: Clarify PropagatedContext. (#11692)
1 parent b1f790d commit 2f85f53

File tree

3 files changed

+65
-46
lines changed

3 files changed

+65
-46
lines changed

core/src/main/java/io/micronaut/core/propagation/PropagatedContext.java

+60-40
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,23 @@
2525
import java.util.stream.Stream;
2626

2727
/**
28-
* The propagation across different threads using the context which is immutable and can be extended/reduced by different elements.
29-
* Each element can be a simple data structure carrying its state across the threads, or it can implement {@link ThreadPropagatedContextElement}
30-
* for use-cases when thread-local values needs to be updated.
28+
* A mechanism for propagating state across threads that's easier to use and safer than
29+
* {@link ThreadLocal}.
30+
*
31+
* <p>
32+
* A propagated context is an immutable list of objects. {@link #plus(PropagatedContextElement)
33+
* Adding} or {@link #minus(PropagatedContextElement) deleting} elements from a context creates a
34+
* new context that must then be explicitly brought into scope by {@link #propagate() propagating
35+
* it}.
36+
*
37+
* <p>
38+
* If an element wraps an existing thread local variable then it can implement {@link
39+
* ThreadPropagatedContextElement} to take part in the enter-exit process.
40+
*
41+
* <p>
42+
* In standard usage you would call {@link #getOrEmpty()}, then {@link
43+
* #plus(PropagatedContextElement)} to add some data, then {@link #propagate(Supplier)} to execute a
44+
* lambda with the context in scope.
3145
*
3246
* @author Denis Stepanov
3347
* @since 4.0.0
@@ -76,7 +90,9 @@ static Optional<PropagatedContext> find() {
7690
}
7791

7892
/**
79-
* Wrap runnable for this context to be propagated in.
93+
* Captures the current context and returns a new {@link Runnable} that, when executed, will run
94+
* the given runnable with the captured context in scope. If no context is in scope then the
95+
* given callable is returned as-is.
8096
*
8197
* @param runnable The runnable
8298
* @return new runnable or existing if the context is missing
@@ -87,7 +103,9 @@ static Runnable wrapCurrent(@NonNull Runnable runnable) {
87103
}
88104

89105
/**
90-
* Wrap callable for this context to be propagated in.
106+
* Captures the current context and returns a new {@link Callable} that, when executed, will run
107+
* the given callable with the captured context in scope. If no context is in scope then the
108+
* given callable is returned as-is.
91109
*
92110
* @param callable The callable
93111
* @param <V> The callable type
@@ -99,7 +117,9 @@ static <V> Callable<V> wrapCurrent(@NonNull Callable<V> callable) {
99117
}
100118

101119
/**
102-
* Wrap supplier for this context to be propagated in.
120+
* Captures the current context and returns a new {@link Supplier} that, when executed, will run
121+
* the given supplier with the captured context in scope. If no context is in scope then the
122+
* given callable is returned as-is.
103123
*
104124
* @param supplier The supplier
105125
* @param <V> The supplier type
@@ -111,61 +131,60 @@ static <V> Supplier<V> wrapCurrent(@NonNull Supplier<V> supplier) {
111131
}
112132

113133
/**
114-
* Check if there is a context associated.
134+
* Check if there is a context in scope.
115135
*
116-
* @return true if the context exists
136+
* @return true if a context has been {@link #propagate() propagated}.
117137
*/
118138
static boolean exists() {
119139
return PropagatedContextImpl.exists();
120140
}
121141

122142
/**
123-
* Creates a new element with added element.
124-
* <p>
125-
* NOTE: The new element needs to be propagated.
143+
* Returns a new context extended with the given element. This doesn't add anything
144+
* to the existing in-scope context (if any), so you will need to propagate it
145+
* yourself. You can add multiple elements of the same type.
126146
*
127-
* @param element The element element to be added
128-
* @return new element
147+
* @param element the element to be added
148+
* @return the new context
129149
*/
130150
@NonNull
131151
PropagatedContext plus(@NonNull PropagatedContextElement element);
132152

133153
/**
134-
* Creates a new context without the provided element.
135-
* <p>
136-
* NOTE: The new context needs to be propagated.
154+
* Returns a new context without the provided element. This doesn't remove anything
155+
* from the existing in-scope context (if any), so you will need to propagate it
156+
* yourself. Elements are compared using {@link Object#equals(Object)}.
137157
*
138158
* @param element The context element to be removed
139-
* @return new context
159+
* @return the new context
140160
*/
141161
@NonNull
142162
PropagatedContext minus(@NonNull PropagatedContextElement element);
143163

144164
/**
145-
* Creates a new context with replaced the provided element.
146-
* <p>
147-
* NOTE: The new context needs to be propagated.
165+
* Creates a new context with the given element replaced. This doesn't change anything
166+
* in the existing in-scope context (if any), so you will need to propagate it
167+
* yourself. Elements are compared using {@link Object#equals(Object)}.
148168
*
149-
* @param oldElement The context element to be replaced
150-
* @param newElement The context element to be replaced with
151-
* @return new context
169+
* @param oldElement the element to be replaced
170+
* @param newElement the element that will replace it
171+
* @return the new context
152172
*/
153173
@NonNull
154174
PropagatedContext replace(@NonNull PropagatedContextElement oldElement,
155175
@NonNull PropagatedContextElement newElement);
156176

157177
/**
158-
* Finds optional element of type.
159-
* In a case of multiple element of the same type the last one will be returned.
178+
* Finds the first element of the given type, if any exist.
160179
*
161180
* @param elementType The element type
162181
* @param <T> The element's type
163-
* @return optional element
182+
* @return element if found
164183
*/
165184
<T extends PropagatedContextElement> Optional<T> find(@NonNull Class<T> elementType);
166185

167186
/**
168-
* Find all elements of type. The first element processed by stream will be the last one added.
187+
* Find all elements of the given type. The first element in the stream will be the last element added.
169188
*
170189
* @param elementType The element type
171190
* @param <T> The element's type
@@ -174,11 +193,12 @@ PropagatedContext replace(@NonNull PropagatedContextElement oldElement,
174193
<T extends PropagatedContextElement> Stream<T> findAll(@NonNull Class<T> elementType);
175194

176195
/**
177-
* Gets element of type.
196+
* Gets the first element of the given type.
178197
*
179198
* @param elementType The element type
180199
* @param <T> The element's type
181-
* @return an element or exception
200+
* @return an element
201+
* @throws java.util.NoSuchElementException if no elements of that type are in the context.
182202
*/
183203
<T extends PropagatedContextElement> T get(@NonNull Class<T> elementType);
184204

@@ -190,17 +210,18 @@ PropagatedContext replace(@NonNull PropagatedContextElement oldElement,
190210
List<PropagatedContextElement> getAllElements();
191211

192212
/**
193-
* Propagate the context using try-resource block.
213+
* Brings this context into scope, temporarily replacing the previous context (if any). The returned
214+
* object must be closed to undo the propagation.
194215
*
195216
* @return auto-closeable block to be used in try-resource block.
196217
*/
197218
@NonNull
198219
Scope propagate();
199220

200221
/**
201-
* Wrap runnable for this context to be propagated in.
222+
* Returns a new runnable that runs the given runnable with this context in scope.
202223
*
203-
* @param runnable The runnable
224+
* @param runnable The runnable that will execute with this context in scope.
204225
* @return new runnable
205226
*/
206227
@NonNull
@@ -214,7 +235,7 @@ default Runnable wrap(@NonNull Runnable runnable) {
214235
}
215236

216237
/**
217-
* Wrap callable for this context to be propagated in.
238+
* Returns a new callable that runs the given callable with this context in scope.
218239
*
219240
* @param callable The callable
220241
* @param <V> The callable return type
@@ -231,7 +252,7 @@ default <V> Callable<V> wrap(@NonNull Callable<V> callable) {
231252
}
232253

233254
/**
234-
* Wrap supplier for this context to be propagated in.
255+
* Returns a new supplier that runs the given supplier with this context in scope.
235256
*
236257
* @param supplier The supplier
237258
* @param <V> The supplier return type
@@ -248,22 +269,22 @@ default <V> Supplier<V> wrap(@NonNull Supplier<V> supplier) {
248269
}
249270

250271
/**
251-
* Propagate the context for the supplier.
272+
* Executes the given supplier with this context in scope, restoring the previous context when execution completes.
252273
*
253274
* @param supplier The supplier
254275
* @param <V> The supplier return type
255-
* @return new supplier
276+
* @return the result of calling {@link Supplier#get()}.
256277
*/
257278
@NonNull
258279
default <V> V propagate(@NonNull Supplier<V> supplier) {
259-
PropagatedContext propagatedContext = this;
260-
try (Scope ignore = propagatedContext.propagate()) {
280+
try (Scope ignore = propagate()) {
261281
return supplier.get();
262282
}
263283
}
264284

265285
/**
266-
* Context propagation {@link AutoCloseable} to be used in try-resource block.
286+
* Closing this object undoes the effect of calling {@link #propagate()} on a context. Intended to be used in a
287+
* try-with-resources block.
267288
*
268289
* @author Denis Stepanov
269290
* @since 4.0.0
@@ -272,5 +293,4 @@ interface Scope extends AutoCloseable {
272293
@Override
273294
void close();
274295
}
275-
276296
}

core/src/main/java/io/micronaut/core/propagation/PropagatedContextElement.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import io.micronaut.core.annotation.Experimental;
1919

2020
/**
21-
* Propagated element of {@link PropagatedContext}.
22-
* <p>
23-
* Simple implementation intended for the propagation in the context as a data carrier.
21+
* Marker interface for an object that can be placed into a {@link PropagatedContext}.
2422
*
2523
* @author Denis Stepanov
2624
* @since 4.0.0

core/src/main/java/io/micronaut/core/propagation/ThreadPropagatedContextElement.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
import io.micronaut.core.annotation.Nullable;
2020

2121
/**
22-
* Special version of {@link PropagatedContextElement} that is thread-aware and can update the thread context on the initial propagation entry
23-
* and restore it on the propagation exist.
22+
* A {@link PropagatedContextElement} that bridges to code that uses thread local storage instead of
23+
* {@link PropagatedContext}. The object returned by {@link #updateThreadContext()} will be given
24+
* to {@link #restoreThreadContext(Object)} when the propagation scope is closed.
2425
*
25-
* @param <S> The restore state type
26+
* @param <S> The type held by the wrapped thread-scoped variable.
2627
* @author Denis Stepanov
2728
* @since 4.0.0
2829
*/

0 commit comments

Comments
 (0)