Skip to content

Commit

Permalink
Clean up and (add some more documentation to) core package.
Browse files Browse the repository at this point in the history
Signed-off-by: Sjoerd Talsma <sjoerdtalsma@users.noreply.github.com>
  • Loading branch information
sjoerdtalsma committed Nov 15, 2024
1 parent 76dd949 commit 7b17dea
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 501 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
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.CallMappingExecutorService;
import nl.talsmasoftware.context.core.delegation.DelegatingExecutorService;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;

/**
* {@code ExecutorService} propagating a {@linkplain nl.talsmasoftware.context.api.ContextSnapshot ContextSnapshot}
* to submitted tasks. Any existing {@linkplain java.util.concurrent.ExecutorService ExecutorService} can be used
* * as a delegate, including those from the {@linkplain java.util.concurrent.Executors Executors} utility class.
* <p>
* Executor service that wraps another executor service, making sure background tasks operates 'within'
* a context snapshot taken from the submitting thread.
*
Expand All @@ -36,26 +40,28 @@
*
* @author Sjoerd Talsma
*/
public class ContextAwareExecutorService extends CallMappingExecutorService {
public class ContextAwareExecutorService extends DelegatingExecutorService {
public ContextAwareExecutorService(ExecutorService delegate) {
super(delegate);
}

/**
* This method maps any callable (before scheduling it) by taking a snapshot of the context in the scheduling thread
* and propagating this context into the executed callable by snapshot reactivation.
*
* @param callable The callable to be mapped.
* @param <V> the actual return type of the callable object being scheduled.
* @return A callable that will reactivate the scheduling thread context snapshot before executing.
*/
@Override
protected <V> Callable<V> map(final Callable<V> callable) {
protected <T> Callable<T> wrap(final Callable<T> callable) {
final ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
return () -> {
try (Reactivation reactivation = snapshot.reactivate()) {
return callable.call();
}
};
}

@Override
protected Runnable wrap(final Runnable runnable) {
final ContextSnapshot snapshot = ContextManagers.createContextSnapshot();
return () -> {
try (Reactivation reactivation = snapshot.reactivate()) {
runnable.run();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/
/**
* Classes adding context support to concurrent usage.
*
* <p>
*
* <h2>{@linkplain nl.talsmasoftware.context.core.concurrent.ContextAwareExecutorService ContextAwareExecutorService}</h2>
* <p>
* An {@code ExecutorService} that propagates a {@linkplain nl.talsmasoftware.context.api.ContextSnapshot ContextSnapshot}
* to submitted tasks. Any existing {@linkplain java.util.concurrent.ExecutorService ExecutorService} can be used
* as a delegate, including those from the {@linkplain java.util.concurrent.Executors Executors} utility class.
*
* <h2>{@linkplain nl.talsmasoftware.context.core.concurrent.ContextAwareCompletableFuture ContextAwareCompletableFuture}</h2>
* <p>
*
*/
package nl.talsmasoftware.context.core.concurrent;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* The class also provides overridable <code>wrapper</code> methods for all complex input (e.g. {@link Callable}, {@link Runnable})
* and result types (e.g. {@link Future}).
* <p>
* Although this class does implements <em>all</em> required methods of {@link ExecutorService} it is still declared
* Although this class does implement <em>all</em> required methods of {@link ExecutorService} it is still declared
* as an <em>abstract</em> class.<br>
* This is because it does not provide any value in itself.
*
Expand Down Expand Up @@ -73,14 +73,10 @@ protected <T> Future<T> wrap(Future<T> source) {
* @see #wrap(Callable)
*/
protected <T> Collection<? extends Callable<T>> wrapTasks(Collection<? extends Callable<T>> tasks) {
if (tasks != null && !tasks.isEmpty()) {
final List<Callable<T>> wrappedTasks = new ArrayList<>(tasks.size());
for (Callable<T> task : tasks) {
wrappedTasks.add(wrap(task));
}
tasks = wrappedTasks;
}
return tasks;
if (tasks == null) return null;
final List<Callable<T>> wrappedTasks = new ArrayList<>(tasks.size());
for (Callable<T> task : tasks) wrappedTasks.add(wrap(task));
return wrappedTasks;
}

/**
Expand All @@ -94,7 +90,7 @@ protected <T> Collection<? extends Callable<T>> wrapTasks(Collection<? extends C
*/
protected <T> List<Future<T>> wrapFutures(Collection<? extends Future<T>> futures) {
if (futures == null) return null;
final List<Future<T>> wrappedFutures = new ArrayList<Future<T>>(futures.size());
final List<Future<T>> wrappedFutures = new ArrayList<>(futures.size());
for (Future<T> future : futures) wrappedFutures.add(wrap(future));
return wrappedFutures;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
/**
* Base classes for delegation.
*
* <p>
* These delegators classes are layered, providing increasing level of functionality.<br>
* {@linkplain nl.talsmasoftware.context.core.delegation.Wrapper Wrapper} is the base class for all other delegators.<br>
* The {@linkplain nl.talsmasoftware.context.core.delegation.CallMappingExecutorService CallMappingExecutorService}
* has the highest implementation level in this package.
*
* <h2>{@linkplain nl.talsmasoftware.context.core.delegation.Wrapper Wrapper}</h2>
* <p>
* Base class for any delegation in this package.<br>
Expand All @@ -38,12 +32,5 @@
* <p>
* Future that passes all operations to a delegate future.<br>
* It allows the result of the to future to be wrapped using an overridable {@code wrapResult} method.
*
* <h2>{@linkplain nl.talsmasoftware.context.core.delegation.CallMappingExecutorService}</h2>
* <p>
* A {@linkplain nl.talsmasoftware.context.core.delegation.DelegatingExecutorService} that maps
* all {@linkplain java.lang.Runnable} tasks in {@linkplain java.util.concurrent.Callable}
* objects, providing a base executor service that can wrap any background task
* by implementing a single {@code map(Callable)} method.
*/
package nl.talsmasoftware.context.core.delegation;
Loading

0 comments on commit 7b17dea

Please sign in to comment.