Skip to content

Commit

Permalink
Clean up: remove Closeable / IOException from test that were introduc…
Browse files Browse the repository at this point in the history
…ed temporarily.

Signed-off-by: Sjoerd Talsma <sjoerdtalsma@users.noreply.github.com>
  • Loading branch information
sjoerdtalsma committed Nov 8, 2024
1 parent 8eac61d commit 4628962
Show file tree
Hide file tree
Showing 25 changed files with 54 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private ContextManagers() {
* within a try-with-resources construct.
*/
@SuppressWarnings("rawtypes")
public static nl.talsmasoftware.context.api.ContextSnapshot createContextSnapshot() {
public static ContextSnapshot createContextSnapshot() {
final long start = System.nanoTime();
final List<ContextManager> managers = ServiceCache.cached(ContextManager.class); // Cached list is immutable
final Object[] values = new Object[managers.size()];
Expand Down Expand Up @@ -188,7 +188,7 @@ public Reactivation reactivate() {
}

ReactivationImpl reactivation = new ReactivationImpl(reactivatedContexts);
Timers.timed(System.nanoTime() - start, nl.talsmasoftware.context.api.ContextSnapshot.class, "reactivate");
Timers.timed(System.nanoTime() - start, ContextSnapshot.class, "reactivate");
return reactivation;
} catch (RuntimeException reactivationException) {
// TODO think about simplifying by catching & handling in reactivate(manager, value) method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -51,7 +49,7 @@ protected BiConsumerWithContext(Supplier<ContextSnapshot> supplier, BiConsumer<T

@Override
public void accept(T t, U u) {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating accept method with {0} to {1}.", new Object[]{context, delegate()});
delegate().accept(t, u);
Expand All @@ -62,16 +60,14 @@ public void accept(T t, U u) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

@Override
public BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
requireNonNull(after, "Cannot post-process with after bi-consumer <null>.");
return (l, r) -> {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating andThen method with {0} to {1}.", new Object[]{context, delegate()});
delegate().accept(l, r);
Expand All @@ -83,8 +79,6 @@ public BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -52,7 +50,7 @@ protected BiFunctionWithContext(Supplier<ContextSnapshot> supplier, BiFunction<I

@Override
public OUT apply(IN1 in1, IN2 in2) {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating apply method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().apply(in1, in2);
Expand All @@ -63,16 +61,14 @@ public OUT apply(IN1 in1, IN2 in2) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

@Override
public <V> BiFunction<IN1, IN2, V> andThen(Function<? super OUT, ? extends V> after) {
requireNonNull(after, "Cannot post-process bi-function with after function <null>.");
return (IN1 in1, IN2 in2) -> {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating andThen method with {0} to {1}.", new Object[]{context, delegate()});
return after.apply(delegate().apply(in1, in2));
Expand All @@ -83,8 +79,6 @@ public <V> BiFunction<IN1, IN2, V> andThen(Function<? super OUT, ? extends V> af
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -51,7 +49,7 @@ protected BiPredicateWithContext(Supplier<ContextSnapshot> supplier, BiPredicate

@Override
public boolean test(IN1 in1, IN2 in2) {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating test method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().test(in1, in2);
Expand All @@ -62,16 +60,14 @@ public boolean test(IN1 in1, IN2 in2) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

@Override
public BiPredicate<IN1, IN2> and(BiPredicate<? super IN1, ? super IN2> other) {
requireNonNull(other, "Cannot combine bi-predicate with 'and' <null>.");
return (IN1 in1, IN2 in2) -> {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating 'and' method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().test(in1, in2) && other.test(in1, in2);
Expand All @@ -82,8 +78,6 @@ public BiPredicate<IN1, IN2> and(BiPredicate<? super IN1, ? super IN2> other) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
};
}
Expand All @@ -92,7 +86,7 @@ public BiPredicate<IN1, IN2> and(BiPredicate<? super IN1, ? super IN2> other) {
public BiPredicate<IN1, IN2> or(BiPredicate<? super IN1, ? super IN2> other) {
requireNonNull(other, "Cannot combine bi-predicate with 'or' <null>.");
return (IN1 in1, IN2 in2) -> {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating 'or' method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().test(in1, in2) || other.test(in1, in2);
Expand All @@ -103,8 +97,6 @@ public BiPredicate<IN1, IN2> or(BiPredicate<? super IN1, ? super IN2> other) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -49,7 +47,7 @@ protected BooleanSupplierWithContext(Supplier<ContextSnapshot> supplier, Boolean

@Override
public boolean getAsBoolean() {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating getAsBoolean method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().getAsBoolean();
Expand All @@ -60,8 +58,6 @@ public boolean getAsBoolean() {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
Expand Down Expand Up @@ -50,7 +48,7 @@ protected ConsumerWithContext(Supplier<ContextSnapshot> supplier, Consumer<T> de

@Override
public void accept(T t) {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating accept method with {0} to {1}.", new Object[]{context, delegate()});
delegate().accept(t);
Expand All @@ -61,16 +59,14 @@ public void accept(T t) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new IllegalStateException("Error closing snapshot reactivation: " + e.getMessage(), e);
}
}

@Override
public Consumer<T> andThen(Consumer<? super T> after) {
requireNonNull(after, "Cannot follow ConsumerWithContext with after consumer <null>.");
return (T t) -> {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating andThen method with {0} to {1}.", new Object[]{context, delegate()});
delegate().accept(t);
Expand All @@ -82,8 +78,6 @@ public Consumer<T> andThen(Consumer<? super T> after) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -50,7 +48,7 @@ protected FunctionWithContext(Supplier<ContextSnapshot> supplier, Function<IN, O
}

public OUT apply(IN in) {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating apply method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().apply(in);
Expand All @@ -61,15 +59,13 @@ public OUT apply(IN in) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new IllegalStateException("Error closing snapshot reactivation: " + e.getMessage(), e);
}
}

public <V> Function<V, OUT> compose(Function<? super V, ? extends IN> before) {
requireNonNull(before, "Cannot compose with before function <null>.");
return (V v) -> {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating compose method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().apply(before.apply(v));
Expand All @@ -80,16 +76,14 @@ public <V> Function<V, OUT> compose(Function<? super V, ? extends IN> before) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
};
}

public <V> Function<IN, V> andThen(Function<? super OUT, ? extends V> after) {
requireNonNull(after, "Cannot transform with after function <null>.");
return (IN in) -> {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating andThen method with {0} to {1}.", new Object[]{context, delegate()});
return after.apply(delegate().apply(in));
Expand All @@ -100,8 +94,6 @@ public <V> Function<IN, V> andThen(Function<? super OUT, ? extends V> after) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand Down Expand Up @@ -51,7 +49,7 @@ protected PredicateWithContext(Supplier<ContextSnapshot> supplier, Predicate<T>

@Override
public boolean test(T t) {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating test method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().test(t);
Expand All @@ -62,16 +60,14 @@ public boolean test(T t) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

@Override
public Predicate<T> and(Predicate<? super T> other) {
requireNonNull(other, "Cannot combine predicate with 'and' <null>.");
return (t) -> {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating 'and' method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().test(t) && other.test(t);
Expand All @@ -82,8 +78,6 @@ public Predicate<T> and(Predicate<? super T> other) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
};
}
Expand All @@ -92,7 +86,7 @@ public Predicate<T> and(Predicate<? super T> other) {
public Predicate<T> or(Predicate<? super T> other) {
requireNonNull(other, "Cannot combine predicate with 'or' <null>.");
return (t) -> {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating 'or' method with {0} to {1}.", new Object[]{context, delegate()});
return delegate().test(t) || other.test(t);
Expand All @@ -103,8 +97,6 @@ public Predicate<T> or(Predicate<? super T> other) {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import nl.talsmasoftware.context.api.ContextSnapshot;
import nl.talsmasoftware.context.core.ContextManagers;

import java.io.Closeable;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
Expand Down Expand Up @@ -72,7 +70,7 @@ protected RunnableWithContext(Supplier<ContextSnapshot> supplier, Runnable deleg

@Override
public void run() {
try (Closeable context = snapshot().reactivate()) {
try (ContextSnapshot.Reactivation context = snapshot().reactivate()) {
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
LOGGER.log(Level.FINEST, "Delegating run method with {0} to {1}.", new Object[]{context, delegate()});
delegate().run();
Expand All @@ -83,8 +81,6 @@ public void run() {
contextSnapshotConsumer.accept(resultSnapshot);
}
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

Expand Down
Loading

0 comments on commit 4628962

Please sign in to comment.