001package org.intellimate.izou.threadpool;
002
003import org.intellimate.izou.identification.Identifiable;
004import org.intellimate.izou.identification.IllegalIDException;
005
006import java.util.ArrayList;
007import java.util.Collection;
008import java.util.List;
009import java.util.concurrent.*;
010
011/**
012 * This is a ExecutorService where the Source of every submitted Thread is easily identifiable.
013 * <p>
014 * The future purpose of this class is to track which addOn started which task, which makes it possible to hotswap
015 * AddOns.
016 * </p>
017 * @author Leander Kurscheidt
018 * @version 1.0
019 */
020//at the moment this is essentially a dummy class, but it is needed to advance the Identification-system (hotswap, kill
021// AddOn etc)
022@SuppressWarnings("NullableProblems")
023public class TrackingExecutorService implements ExecutorService {
024    private final ExecutorService executorService;
025    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
026    private final Identifiable identifiable;
027
028    private TrackingExecutorService(ExecutorService executorService, Identifiable identifiable) {
029        this.executorService = executorService;
030        this.identifiable = identifiable;
031    }
032
033    /**
034     * creates a new ExecutorService
035     * @param executorService the ExecutorService to use under the hood
036     * @param identifiable the identifiable to associate each submitted thread with
037     * @return an instance of TrackingExecutorService
038     * @throws IllegalIDException not yet implemented
039     */
040    public static TrackingExecutorService createTrackingExecutorService(ExecutorService executorService,
041                                                                        Identifiable identifiable) throws IllegalIDException {
042        return new TrackingExecutorService(executorService, identifiable);
043    }
044
045    /**
046     * method not implemented
047     */
048    @Override
049    //(maybe set executorService to null?)
050    public void shutdown() {}
051
052    /**
053     * method not fully implemented.
054     * It will not return the List of waiting Runnables and after calling it is still possible to submit Tasks
055     * @return empty list
056     */
057    @Override
058    public List<Runnable> shutdownNow() {
059        return new ArrayList<>();
060    }
061
062    /**
063     * method not implemented, will always return false
064     */
065    @Override
066    public boolean isShutdown() {
067        return executorService.isShutdown();
068    }
069
070    /**
071     * method not implemented, will always return false
072     */
073    @Override
074    public boolean isTerminated() {
075        return executorService.isTerminated();
076    }
077
078    /**
079     * method not implemented, will do nothing
080     */
081    @Override
082    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
083        return false;
084    }
085
086    /**
087     * submits a Task to the ThreadPool
088     * @param task the Task to submit
089     * @param <T> the type of the return
090     * @return null if unable to obtain Identification
091     */
092    @Override
093    public <T> Future<T> submit(Callable<T> task) {
094        return executorService.submit(task);
095    }
096
097    @Override
098    public <T> Future<T> submit(Runnable task, T result) {
099        return executorService.submit(task, result);
100    }
101
102    @Override
103    public Future<?> submit(Runnable task) {
104        return executorService.submit(task);
105    }
106
107    @Override
108    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
109        return executorService.invokeAll(tasks);
110    }
111
112    @Override
113    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
114        return executorService.invokeAll(tasks, timeout, unit);
115    }
116
117    @Override
118    public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
119        return executorService.invokeAny(tasks);
120    }
121
122    @Override
123    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
124        return executorService.invokeAny(tasks, timeout, unit);
125    }
126
127    @Override
128    public void execute(Runnable command) {
129        executorService.execute(command);
130    }
131}