120 lines
3.4 KiB
Java
120 lines
3.4 KiB
Java
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<? extends ExecutorServiceDecorator> decoratorClass) {
|
|
if (executor instanceof ExecutorServiceDecorator) {
|
|
var executorServiceDecoratorImpl = (ExecutorServiceDecorator) executor;
|
|
var executorServiceDecorators = executorServiceDecoratorImpl.getExecutorServiceDecorators();
|
|
return executorServiceDecorators.contains(decoratorClass);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public final Set<Class<? extends ExecutorServiceDecorator>> 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<Runnable> 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 <T> Future<T> submit(@NotNull Callable<T> callable) {
|
|
return executorService.submit(callable);
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public <T> Future<T> 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 <T> List<Future<T>> invokeAll(@NotNull Collection<? extends Callable<T>> collection)
|
|
throws InterruptedException {
|
|
return executorService.invokeAll(collection);
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public <T> List<Future<T>> invokeAll(@NotNull Collection<? extends Callable<T>> collection,
|
|
long l,
|
|
@NotNull TimeUnit timeUnit) throws InterruptedException {
|
|
return executorService.invokeAll(collection, l, timeUnit);
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public <T> T invokeAny(@NotNull Collection<? extends Callable<T>> collection)
|
|
throws InterruptedException, ExecutionException {
|
|
return executorService.invokeAny(collection);
|
|
}
|
|
|
|
@Override
|
|
public <T> T invokeAny(@NotNull Collection<? extends Callable<T>> 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);
|
|
}
|
|
}
|