Skip to content

Commit

Permalink
Replace ContextManagers.createContextSnapshot() with ContextSnapshot.…
Browse files Browse the repository at this point in the history
…capture() in documentation.

Signed-off-by: Sjoerd Talsma <[email protected]>
  • Loading branch information
sjoerdtalsma committed Nov 30, 2024
1 parent 8b2f8d1 commit 2c2fc17
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 81 deletions.
45 changes: 22 additions & 23 deletions context-propagation-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ although technically these use cases are not appropriate.
Manages contexts by initializing and maintaining an active context value.

Normally it is not necessary to interact directly with individual context managers.
The `ContextManagers` utility class detects available context managers and lets
you take a [_snapshot_](#context-snapshot) of **all** active contexts at once.
The api detects available context managers and lets
you capture a [_snapshot_](#context-snapshot) of **all** active contexts at once.

- [ContextManager javadoc][contextmanager]
- [ContextManagers javadoc][contextmanagers]
- [ContextSnapshot javadoc][contextsnapshot]

### Context Snapshot

A context snapshot is created by the [ContextManagers]' `createContextSnapshot()` method.
A context snapshot is captured by the [ContextSnapshot]' `capture()` method.
The snapshot contains active context values from all known [ContextManager] implementations.
Once created, the captured _values_ in such context snapshot will not change anymore,
even when the active context is later modified.
Expand All @@ -52,7 +52,6 @@ They stay active until the reactivation is closed again (or are overwritten by n
Closing the reactivated object is mandatory (from the thread where the reactivation was called).

- [ContextSnapshot javadoc](https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/ContextSnapshot.html)
- [ContextManagers javadoc](https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/ContextManagers.html)

## Creating your own context manager

Expand All @@ -68,23 +67,24 @@ It should contain the fully qualified classname of your implementation.

```java
public class DummyContextManager implements ContextManager<String> {
public Context<String> initializeNewContext(String value) {
return new DummyContext(value);
public Context<String> initializeNewContext(String value) {
return new DummyContext(value);
}

public Context<String> getActiveContextValue() {
DummyContext current = DummyContext.current();
return current != null ? current.getValue() : null;
}

private static final class DummyContext extends AbstractThreadLocalContext<String> {
private DummyContext(String newValue) {
super(newValue);
}

public Context<String> getActiveContext() {
return DummyContext.current();
}

private static final class DummyContext extends AbstractThreadLocalContext<String> {
private DummyContext(String newValue) {
super(newValue);
}

private static Context<String> current() {
return AbstractThreadLocalContext.current(DummyContext.class);
}
private static Context<String> current() {
return AbstractThreadLocalContext.current(DummyContext.class);
}
}
}
```

Expand All @@ -95,7 +95,6 @@ public class DummyContextManager implements ContextManager<String> {
[javadoc]: https://www.javadoc.io/doc/nl.talsmasoftware.context/context-propagation

[threadlocal]: https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html
[context]: https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/Context.html
[contextsnapshot]: https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/ContextSnapshot.html
[contextmanager]: https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/ContextManager.html
[contextmanagers]: https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/ContextManagers.html
[context]: https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/api/Context.html
[contextsnapshot]: https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/api/ContextSnapshot.html
[contextmanager]: https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/api/ContextManager.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package nl.talsmasoftware.context.core.concurrent;

import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;
import nl.talsmasoftware.context.core.function.BiConsumerWithContext;
import nl.talsmasoftware.context.core.function.BiFunctionWithContext;
import nl.talsmasoftware.context.core.function.ConsumerWithContext;
Expand Down Expand Up @@ -61,10 +60,10 @@ public class ContextAwareCompletableFuture<T> extends CompletableFuture<T> {
private final boolean takeNewSnapshot;

/**
* Creates a new {@link ContextSnapshot} and remembers that in this completable future,
* Captures a new {@link ContextSnapshot} and remembers that in this completable future,
* running all completion methods within this snapshot.
*
* @see ContextManagers#createContextSnapshot()
* @see ContextSnapshot#capture()
*/
public ContextAwareCompletableFuture() {
this((ContextSnapshot) null);
Expand All @@ -75,8 +74,8 @@ public ContextAwareCompletableFuture() {
* snapshot context.
*
* @param snapshot the snapshot to run completion methods in.
* Optional, the completable future will take a new snaphot if {@code null} is provided.
* @see ContextManagers#createContextSnapshot()
* Optional, the completable future will capture a new snapshot if {@code null} is provided.
* @see ContextSnapshot#capture()
*/
public ContextAwareCompletableFuture(ContextSnapshot snapshot) {
this(new ContextSnapshotHolder(snapshot), false);
Expand Down Expand Up @@ -126,8 +125,8 @@ public static <U> ContextAwareCompletableFuture<U> supplyAsync(Supplier<U> suppl
* This method is lenient to {@code null} values for {@code executor} and {@code snapshot}:<br>
* If {@code executor == null} the common {@link java.util.concurrent.ForkJoinPool ForkJoinPool} is used as
* specified by {@link CompletableFuture#supplyAsync(Supplier)}.<br>
* If {@code snapshot == null} a {@link ContextManagers#createContextSnapshot() new context snapshot} is
* created for the {@link Supplier} (if not already a {@link SupplierWithContext}).
* If {@code snapshot == null} a {@link ContextSnapshot#capture() new context snapshot} is
* captured for the {@link Supplier} (if not already a {@link SupplierWithContext}).
*
* @param supplier a function returning the value to be used to complete the returned CompletableFuture
* @param executor the executor to use for asynchronous execution
Expand All @@ -150,8 +149,8 @@ public static <U> ContextAwareCompletableFuture<U> supplyAsync(Supplier<U> suppl
* This method is lenient to {@code null} values for {@code executor} and {@code snapshot}:<br>
* If {@code executor == null} the common {@link java.util.concurrent.ForkJoinPool ForkJoinPool} is used as
* specified by {@link CompletableFuture#supplyAsync(Supplier)}.<br>
* If {@code snapshot == null} a {@link ContextManagers#createContextSnapshot() new context snapshot} is
* created for the {@link Supplier} (if not already a {@link SupplierWithContext}).
* If {@code snapshot == null} a {@link ContextSnapshot#capture() new context snapshot} is
* captured for the {@link Supplier} (if not already a {@link SupplierWithContext}).
*
* @param supplier a function returning the value to be used to complete the returned CompletableFuture
* @param executor the executor to use for asynchronous execution
Expand Down Expand Up @@ -215,8 +214,8 @@ public static ContextAwareCompletableFuture<Void> runAsync(Runnable runnable, Ex
* This method is lenient to {@code null} values for {@code executor} and {@code snapshot}:<br>
* If {@code executor == null} the common {@link java.util.concurrent.ForkJoinPool ForkJoinPool} is used as
* specified by {@link CompletableFuture#supplyAsync(Supplier)}.<br>
* If {@code snapshot == null} a {@link ContextManagers#createContextSnapshot() new context snapshot} is
* created for the {@link Supplier} (if not already a {@link SupplierWithContext}).
* If {@code snapshot == null} a {@link ContextSnapshot#capture() new context snapshot} is
* captured for the {@link Supplier} (if not already a {@link SupplierWithContext}).
*
* @param runnable the action to run before completing the returned CompletableFuture
* @param executor the executor to use for asynchronous execution
Expand All @@ -237,8 +236,8 @@ public static ContextAwareCompletableFuture<Void> runAsync(Runnable runnable, Ex
* This method is lenient to {@code null} values for {@code executor} and {@code snapshot}:<br>
* If {@code executor == null} the common {@link java.util.concurrent.ForkJoinPool ForkJoinPool} is used as
* specified by {@link CompletableFuture#supplyAsync(Supplier)}.<br>
* If {@code snapshot == null} a {@link ContextManagers#createContextSnapshot() new context snapshot} is
* created for the {@link Supplier} (if not already a {@link SupplierWithContext}).
* If {@code snapshot == null} a {@link ContextSnapshot#capture() new context snapshot} is
* captured for the {@link Supplier} (if not already a {@link SupplierWithContext}).
*
* @param runnable the action to run before completing the returned CompletableFuture
* @param executor the executor to use for asynchronous execution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected WrapperWithContext(final ContextSnapshot snapshot, final T delegate) {
* <p>
* <strong>Note:</strong> <em>Make sure the supplier function does <strong>not</strong> obtain the context snapshot
* from any threadlocal storage! The wrapper is designed to propagate contexts from one thread to another.
* Therefore, the snapshot must be {@link nl.talsmasoftware.context.core.ContextManagers#createContextSnapshot() captured}
* Therefore, the snapshot must be {@link ContextSnapshot#capture() captured}
* in the source thread and {@link ContextSnapshot#reactivate() reactivated} in the target thread.
* If unsure, please use the
* {@link #WrapperWithContext(ContextSnapshot, Object) constructor with snapshot} instead.</em>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package nl.talsmasoftware.context.core.function;

import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand Down Expand Up @@ -64,7 +63,7 @@ public BiConsumerWithContext(ContextSnapshot snapshot, BiConsumer<T, U> delegate
* <li>{@linkplain ContextSnapshot#reactivate() reactivate} the given snapshot
* <li>accept the values by passing them to the delegate bi-consumer
* <li><em>if snapshot consumer is non-null,</em>
* pass it a {@linkplain ContextManagers#createContextSnapshot() new context snapshot}
* pass it a {@linkplain ContextSnapshot#capture() new context snapshot}
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* </ol>
*
Expand Down Expand Up @@ -103,7 +102,7 @@ protected BiConsumerWithContext(Supplier<ContextSnapshot> snapshotSupplier, BiCo
* <li>{@linkplain ContextSnapshot#reactivate() reactivate} the given snapshot
* <li>accept the values by passing them to the delegate bi-consumer
* <li><em>if snapshot consumer is non-null,</em>
* pass it a {@linkplain ContextManagers#createContextSnapshot() new context snapshot}
* pass it a {@linkplain ContextSnapshot#capture() new context snapshot}
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* </ol>
*
Expand Down Expand Up @@ -141,7 +140,7 @@ public void accept(T in1, U in2) {
* <li>passing them to the {@code after} bi-consumer
* </ol>
* <li><em>if snapshot consumer is non-null,</em>
* pass it a {@linkplain ContextManagers#createContextSnapshot() new context snapshot}
* pass it a {@linkplain ContextSnapshot#capture() new context snapshot}
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* </ol>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package nl.talsmasoftware.context.core.function;

import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.util.function.BiFunction;
import java.util.function.Consumer;
Expand Down Expand Up @@ -73,7 +72,7 @@ public BiFunctionWithContext(ContextSnapshot snapshot, BiFunction<IN1, IN2, OUT>
* <li>{@linkplain ContextSnapshot#reactivate() reactivate} the given snapshot
* <li>apply the delegate function
* <li><em>if snapshot consumer is non-null,</em>
* pass a {@linkplain ContextManagers#createContextSnapshot() new context snapshot} to the consumer
* pass a {@linkplain ContextSnapshot#capture() new context snapshot} to the consumer
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return the result from the delegate function call (or throw runtime exception if the delegate did).
* </ol>
Expand Down Expand Up @@ -113,7 +112,7 @@ protected BiFunctionWithContext(Supplier<ContextSnapshot> snapshotSupplier, BiFu
* <li>{@linkplain ContextSnapshot#reactivate() reactivate} the given snapshot
* <li>apply the delegate bi-function and get the result
* <li><em>if context snapshot consumer is non-null,</em>
* pass a {@linkplain ContextManagers#createContextSnapshot() new context snapshot} to the consumer
* pass a {@linkplain ContextSnapshot#capture() new context snapshot} to the consumer
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return the result</li>
* </ol>
Expand Down Expand Up @@ -150,7 +149,7 @@ public OUT apply(IN1 in1, IN2 in2) {
* <li>apply the delegate bi-function and get the result
* <li>apply the {@code after} function to the result to get the end result
* <li><em>if context snapshot consumer is non-null,</em>
* pass a {@linkplain ContextManagers#createContextSnapshot() new context snapshot} to the consumer
* pass a {@linkplain ContextSnapshot#capture() new context snapshot} to the consumer
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return the end result</li>
* </ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package nl.talsmasoftware.context.core.function;

import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.util.function.BiPredicate;
import java.util.function.Consumer;
Expand Down Expand Up @@ -71,7 +70,7 @@ public BiPredicateWithContext(ContextSnapshot snapshot, BiPredicate<IN1, IN2> de
* <li>{@linkplain ContextSnapshot#reactivate() reactivate} the given snapshot
* <li>test the delegate bi-predicate and get the outcome
* <li><em>if snapshot consumer is non-null,</em>
* pass a {@linkplain ContextManagers#createContextSnapshot() new context snapshot} to the consumer
* pass a {@linkplain ContextSnapshot#capture() new context snapshot} to the consumer
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return bi-predicate outcome (or throw runtime exception if the delegate did).
* </ol>
Expand Down Expand Up @@ -111,7 +110,7 @@ protected BiPredicateWithContext(Supplier<ContextSnapshot> snapshotSupplier, BiP
* <li>{@linkplain ContextSnapshot#reactivate() reactivate} the given snapshot
* <li>test the bi-delegate predicate and get the outcome
* <li><em>if context snapshot consumer is non-null,</em>
* pass a {@linkplain ContextManagers#createContextSnapshot() new context snapshot} to the consumer
* pass a {@linkplain ContextSnapshot#capture() new context snapshot} to the consumer
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return the outcome</li>
* </ol>
Expand Down Expand Up @@ -149,7 +148,7 @@ public boolean test(IN1 in1, IN2 in2) {
* <li>test the delegate bi-predicate and get the outcome
* <li><em>if the outcome is true, </em> test the {@code other} bi-predicate and return that outcome</li>
* <li><em>if context snapshot consumer is non-null,</em>
* pass a {@linkplain ContextManagers#createContextSnapshot() new context snapshot} to the consumer
* pass a {@linkplain ContextSnapshot#capture() new context snapshot} to the consumer
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return the final outcome</li>
* </ol>
Expand Down Expand Up @@ -195,7 +194,7 @@ public BiPredicate<IN1, IN2> and(BiPredicate<? super IN1, ? super IN2> other) {
* <li>test the delegate bi-predicate and get the outcome
* <li><em>if the outcome is false, </em> test the {@code other} vi-predicate and return that outcome</li>
* <li><em>if context snapshot consumer is non-null,</em>
* pass a {@linkplain ContextManagers#createContextSnapshot() new context snapshot} to the consumer
* pass a {@linkplain ContextSnapshot#capture() new context snapshot} to the consumer
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return the final outcome</li>
* </ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package nl.talsmasoftware.context.core.function;

import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.util.function.BinaryOperator;
import java.util.function.Consumer;
Expand Down Expand Up @@ -64,7 +63,7 @@ public BinaryOperatorWithContext(ContextSnapshot snapshot, BinaryOperator<T> del
* <li>{@linkplain ContextSnapshot#reactivate() reactivate} the given snapshot
* <li>apply the delegate binary operator
* <li><em>if snapshot consumer is non-null,</em>
* pass a {@linkplain ContextManagers#createContextSnapshot() new context snapshot} to the consumer
* pass a {@linkplain ContextSnapshot#capture() new context snapshot} to the consumer
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return the result from the delegate operator call (or throw runtime exception if the delegate did).
* </ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package nl.talsmasoftware.context.core.function;

import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
Expand Down Expand Up @@ -67,7 +66,7 @@ public BooleanSupplierWithContext(ContextSnapshot snapshot, BooleanSupplier dele
* <li>{@linkplain ContextSnapshot#reactivate() reactivate} the given snapshot
* <li>getting the outcome from the delegate boolean supplier
* <li><em>if snapshot consumer is non-null,</em>
* pass it a {@linkplain ContextManagers#createContextSnapshot() new context snapshot}
* pass it a {@linkplain ContextSnapshot#capture() new context snapshot}
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return the outcome from the delegate boolean supplier call (or throw runtime exception if the delegate did).
* </ol>
Expand Down Expand Up @@ -107,7 +106,7 @@ protected BooleanSupplierWithContext(Supplier<ContextSnapshot> snapshotSupplier,
* <li>{@linkplain ContextSnapshot#reactivate() reactivate} the given snapshot
* <li>get the outcome from the delegate boolean supplier
* <li><em>if context snapshot consumer is non-null,</em>
* pass a {@linkplain ContextManagers#createContextSnapshot() new context snapshot} to the consumer
* pass a {@linkplain ContextSnapshot#capture() new context snapshot} to the consumer
* <li>close the {@linkplain ContextSnapshot.Reactivation reactivation}
* <li>return the outcome</li>
* </ol>
Expand Down
Loading

0 comments on commit 2c2fc17

Please sign in to comment.