Skip to content

Commit

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

Signed-off-by: Sjoerd Talsma <[email protected]>
  • Loading branch information
sjoerdtalsma committed Nov 30, 2024
1 parent e1e60fd commit 8b2f8d1
Show file tree
Hide file tree
Showing 32 changed files with 73 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* <p>
* Currently, the following timed operations are updated:
* <ul>
* <li>{@code ContextManagers.createContextSnapshot}</li>
* <li>{@linkplain ContextSnapshot#capture()}</li>
* <li>{@linkplain ContextSnapshot#reactivate()}</li>
* <li>{@linkplain ContextManager#getActiveContextValue()} (*)</li>
* <li>{@linkplain ContextManager#initializeNewContext(Object)} (*)</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2016-2024 Talsma ICT
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.talsmasoftware.context.dummy;

import nl.talsmasoftware.context.api.ContextTimer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.api.ContextSnapshot.Reactivation;
import nl.talsmasoftware.context.core.ContextManagers;
import nl.talsmasoftware.context.core.delegation.DelegatingExecutorService;

import java.util.concurrent.Callable;
Expand Down Expand Up @@ -47,7 +46,7 @@ public ContextAwareExecutorService(ExecutorService delegate) {

@Override
protected <T> Callable<T> wrap(final Callable<T> callable) {
final ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
final ContextSnapshot snapshot = ContextSnapshot.capture();
return () -> {
try (Reactivation reactivation = snapshot.reactivate()) {
return callable.call();
Expand All @@ -57,7 +56,7 @@ protected <T> Callable<T> wrap(final Callable<T> callable) {

@Override
protected Runnable wrap(final Runnable runnable) {
final ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
final ContextSnapshot snapshot = ContextSnapshot.capture();
return () -> {
try (Reactivation reactivation = snapshot.reactivate()) {
runnable.run();
Expand Down
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 java.util.function.Consumer;
import java.util.function.Supplier;
Expand All @@ -40,7 +39,7 @@ final class ContextSnapshotHolder implements Consumer<ContextSnapshot>, Supplier
* Optional, if {@code null} the holder will initialize with a new snapshot.
*/
ContextSnapshotHolder(ContextSnapshot snapshot) {
this.snapshot = (snapshot == null ? ContextManagers.createContextSnapshot() : snapshot);
this.snapshot = (snapshot == null ? ContextSnapshot.capture() : snapshot);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void accept(T in1, U in2) {
delegate().accept(in1, in2);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
after.accept(l, r);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public OUT apply(IN1 in1, IN2 in2) {
return delegate().apply(in1, in2);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ public <V> BiFunction<IN1, IN2, V> andThen(Function<? super OUT, ? extends V> af
return after.apply(delegate().apply(in1, in2));
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public boolean test(IN1 in1, IN2 in2) {
return delegate().test(in1, in2);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public BiPredicate<IN1, IN2> and(BiPredicate<? super IN1, ? super IN2> other) {
return delegate().test(in1, in2) && other.test(in1, in2);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down Expand Up @@ -218,7 +218,7 @@ public BiPredicate<IN1, IN2> or(BiPredicate<? super IN1, ? super IN2> other) {
return delegate().test(in1, in2) || other.test(in1, in2);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public boolean getAsBoolean() {
return delegate().getAsBoolean();
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void accept(T value) {
delegate().accept(value);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down Expand Up @@ -156,7 +156,7 @@ public Consumer<T> andThen(Consumer<? super T> after) {
after.accept(t);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public OUT apply(IN in) {
return delegate().apply(in);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public <V> Function<V, OUT> compose(Function<? super V, ? extends IN> before) {
return delegate().apply(before.apply(v));
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down Expand Up @@ -202,7 +202,7 @@ public <V> Function<IN, V> andThen(Function<? super OUT, ? extends V> after) {
return after.apply(delegate().apply(in));
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public boolean test(T value) {
return delegate().test(value);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down Expand Up @@ -171,7 +171,7 @@ public Predicate<T> and(Predicate<? super T> other) {
return delegate().test(t) && other.test(t);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down Expand Up @@ -216,7 +216,7 @@ public Predicate<T> or(Predicate<? super T> other) {
return delegate().test(t) || other.test(t);
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void run() {
delegate().run();
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.finest(() -> "Captured context snapshot after delegation: " + resultSnapshot + ".");
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public T get() {
return delegate().get();
} finally {
if (contextSnapshotConsumer != null) {
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
ContextSnapshot resultSnapshot = ContextSnapshot.capture();
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
contextSnapshotConsumer.accept(resultSnapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public class ContextSnapshotTest {

@Test
public void testSnapshotToString() {
assertThat(ContextManagers.createContextSnapshot(), hasToString(startsWith("ContextSnapshot{size=")));
assertThat(ContextSnapshot.capture(), hasToString(startsWith("ContextSnapshot{size=")));
}

@Test
public void testSnapshotReactivate() {
try (Context<String> ctx = MGR.initializeNewContext("Old value")) {
ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
ContextSnapshot snapshot = ContextSnapshot.capture();
try (Context<String> ctx2 = MGR.initializeNewContext("New value")) {
assertThat(MGR.getActiveContextValue(), is("New value"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void resetDefaultClassLoader() {
@Test
public void testReactivate_withoutContextManagers() {
Context<String> ctx1 = new DummyContext("foo");
ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
ContextSnapshot snapshot = ContextSnapshot.capture();
ctx1.close();

ContextSnapshot.Reactivation reactivated = snapshot.reactivate();
Expand All @@ -59,7 +59,7 @@ public void testReactivate_withoutContextManagers() {

@Test
public void testCreateSnapshot_withoutContextManagers() {
ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
ContextSnapshot snapshot = ContextSnapshot.capture();
assertThat(snapshot, is(notNullValue()));

ContextSnapshot.Reactivation reactivated = snapshot.reactivate();
Expand Down
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.dummy.DummyContextManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -56,7 +55,7 @@ public void testCompletedFutureTakesNewSnapshot() throws ExecutionException, Int
@Test
public void testCompletedFutureAppliesGivenSnapshot() throws ExecutionException, InterruptedException {
manager.initializeNewContext("Mr. Blonde");
final ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
final ContextSnapshot snapshot = ContextSnapshot.capture();
manager.initializeNewContext("Mr. Brown");
final CompletableFuture<String> completed = ContextAwareCompletableFuture.completedFuture("Mr. Blue", snapshot);
manager.initializeNewContext("Mr. Orange");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import nl.talsmasoftware.context.api.Context;
import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;
import nl.talsmasoftware.context.dummy.DummyContext;
import nl.talsmasoftware.context.dummy.DummyContextManager;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -112,7 +111,7 @@ public void testSupplyAsync_executor() throws ExecutionException, InterruptedExc
@Test
public void testSupplyAsync_executor_snapshot() throws ExecutionException, InterruptedException {
try (Context<String> ctx = manager.initializeNewContext("Vincent Vega")) {
ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
ContextSnapshot snapshot = ContextSnapshot.capture();
DummyContext.setCurrentValue("Jules Winnfield");
assertContext("Jules Winnfield");

Expand All @@ -124,7 +123,7 @@ public void testSupplyAsync_executor_snapshot() throws ExecutionException, Inter
@Test
public void testSupplyAsync_executor_snapshot_takeNewSnapshot() throws ExecutionException, InterruptedException {
try (Context<String> ctx = manager.initializeNewContext("Vincent Vega")) {
ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
ContextSnapshot snapshot = ContextSnapshot.capture();
DummyContext.setCurrentValue("Jules Winnfield");
assertContext("Jules Winnfield");

Expand Down Expand Up @@ -160,7 +159,7 @@ public void testRunAsync_executor() throws ExecutionException, InterruptedExcept
@Test
public void testRunAsync_executor_snapshot() throws ExecutionException, InterruptedException {
try (Context<String> ctx = manager.initializeNewContext("Pumpkin")) {
ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
ContextSnapshot snapshot = ContextSnapshot.capture();
DummyContext.setCurrentValue("Honey Bunny");
assertContext("Honey Bunny");

Expand All @@ -172,7 +171,7 @@ public void testRunAsync_executor_snapshot() throws ExecutionException, Interrup
@Test
public void testRunAsync_executor_snapshot_takeNewSnapshot() throws ExecutionException, InterruptedException {
try (Context<String> ctx = manager.initializeNewContext("Pumpkin")) {
ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
ContextSnapshot snapshot = ContextSnapshot.capture();
DummyContext.setCurrentValue("Honey Bunny");
assertContext("Honey Bunny");

Expand Down Expand Up @@ -1407,7 +1406,7 @@ public void testAllOf() throws ExecutionException, InterruptedException {
@Test
public void testAllOfWithSpecificSnapshot() throws ExecutionException, InterruptedException {
DummyContext.setCurrentValue("Vincent Vega");
final ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
final ContextSnapshot snapshot = ContextSnapshot.capture();
DummyContext.setCurrentValue("Marcellus Wallace");
CompletableFuture<String> cf1 = new CompletableFuture<>();
CompletableFuture<String> cf2 = new ContextAwareCompletableFuture<String>().takeNewSnapshot().thenApply(s -> {
Expand Down Expand Up @@ -1446,7 +1445,7 @@ public void testAnyOf() throws ExecutionException, InterruptedException {
@Test
public void testAnyOfWithSpecificSnapshot() throws ExecutionException, InterruptedException {
DummyContext.setCurrentValue("Vincent Vega");
final ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
final ContextSnapshot snapshot = ContextSnapshot.capture();
DummyContext.setCurrentValue("Marcellus Wallace");
CompletableFuture<String> cf1 = new CompletableFuture<>();
CompletableFuture<String> cf2 = new ContextAwareCompletableFuture<String>().takeNewSnapshot().thenApply(s -> {
Expand Down
Loading

0 comments on commit 8b2f8d1

Please sign in to comment.