package org.warp.commonutils.concurrency.executor; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.jetbrains.annotations.NotNull; public abstract class ExecutorServiceDecorator implements ExecutorService { private ExecutorService executorService; public ExecutorServiceDecorator(ExecutorService executorService) { this.executorService = Objects.requireNonNull(executorService); } protected static boolean hasDecorator(ExecutorService executor, Class decoratorClass) { if (executor instanceof ExecutorServiceDecorator) { var executorServiceDecoratorImpl = (ExecutorServiceDecorator) executor; var executorServiceDecorators = executorServiceDecoratorImpl.getExecutorServiceDecorators(); return executorServiceDecorators.contains(decoratorClass); } return false; } public final Set> getExecutorServiceDecorators() { if (executorService instanceof ExecutorServiceDecorator) { var decorators = ((ExecutorServiceDecorator) executorService).getExecutorServiceDecorators(); decorators.add(this.getClass()); return decorators; } else { return new HashSet<>(); } } @Override public void shutdown() { executorService.shutdown(); } @NotNull @Override public List shutdownNow() { return executorService.shutdownNow(); } @Override public boolean isShutdown() { return executorService.isShutdown(); } @Override public boolean isTerminated() { return executorService.isTerminated(); } @Override public boolean awaitTermination(long l, @NotNull TimeUnit timeUnit) throws InterruptedException { return executorService.awaitTermination(l, timeUnit); } @NotNull @Override public Future submit(@NotNull Callable callable) { return executorService.submit(callable); } @NotNull @Override public Future submit(@NotNull Runnable runnable, T t) { return executorService.submit(runnable, t); } @NotNull @Override public Future submit(@NotNull Runnable runnable) { return executorService.submit(runnable); } @NotNull @Override public List> invokeAll(@NotNull Collection> collection) throws InterruptedException { return executorService.invokeAll(collection); } @NotNull @Override public List> invokeAll(@NotNull Collection> collection, long l, @NotNull TimeUnit timeUnit) throws InterruptedException { return executorService.invokeAll(collection, l, timeUnit); } @NotNull @Override public T invokeAny(@NotNull Collection> collection) throws InterruptedException, ExecutionException { return executorService.invokeAny(collection); } @Override public T invokeAny(@NotNull Collection> collection, long l, @NotNull TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException { return executorService.invokeAny(collection, l, timeUnit); } @Override public void execute(@NotNull Runnable runnable) { executorService.execute(runnable); } }