package jenkins.util;

import com.google.common.util.concurrent.ForwardingExecutorService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
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;

/**
 * {@link ExecutorService} that wraps all the tasks that run inside.
 *
 * @author Kohsuke Kawaguchi
 * @since 1.557
 */
public abstract class InterceptingExecutorService extends ForwardingExecutorService {
    private final ExecutorService base;

    public InterceptingExecutorService(ExecutorService base) {
        this.base = base;
    }

    protected abstract Runnable wrap(Runnable r);

    protected abstract <V> Callable<V> wrap(Callable<V> r);

    @Override
    protected ExecutorService delegate() {
        return base;
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return super.submit(wrap(task));
    }

    @Override
    public <T> Future<T> submit(Runnable task, T result) {
        return super.submit(wrap(task), result);
    }

    @Override
    public Future<?> submit(Runnable task) {
        return super.submit(wrap(task));
    }

    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
        return super.invokeAll(wrap(tasks));
    }

    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
        return super.invokeAll(wrap(tasks), timeout, unit);
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
        return super.invokeAny(wrap(tasks));
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return super.invokeAny(wrap(tasks), timeout, unit);
    }

    @Override
    public void execute(Runnable command) {
        super.execute(wrap(command));
    }

    private <T> Collection<Callable<T>> wrap(Collection<? extends Callable<T>> callables) {
        List<Callable<T>> r = new ArrayList<>();
        for (Callable<T> c : callables) {
            r.add(wrap(c));
        }
        return r;
    }
}

@@ -20,77 +20,8 @@
 l;%0A%0A
-import com.google.common.util.concurrent.ForwardingExecutorService;%0A%0A
 impo
@@ -527,26 +527,19 @@
 ice 
-extend
+implement
 s 
-Forwarding
 Exec
@@ -801,30 +801,16 @@
 V%3E r);%0A%0A
-    @Override%0A
     prot
@@ -868,38 +868,24 @@
 ase;%0A    %7D%0A%0A
-    @Override%0A
     public %3C
@@ -931,37 +931,42 @@
 %0A        return 
-super
+delegate()
 .submit(wrap(tas
@@ -1057,37 +1057,42 @@
 %0A        return 
-super
+delegate()
 .submit(wrap(tas
@@ -1185,21 +1185,26 @@
  return 
-super
+delegate()
 .submit(
@@ -1356,37 +1356,42 @@
 %0A        return 
-super
+delegate()
 .invokeAll(wrap(
@@ -1568,37 +1568,42 @@
 %0A        return 
-super
+delegate()
 .invokeAll(wrap(
@@ -1772,37 +1772,42 @@
 %0A        return 
-super
+delegate()
 .invokeAny(wrap(
@@ -2016,21 +2016,26 @@
  return 
-super
+delegate()
 .invokeA
@@ -2143,35 +2143,685 @@
     
-super.execute(wrap(command)
+delegate().execute(wrap(command));%0A    %7D%0A%0A%0A    @Override%0A    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException %7B%0A        return delegate().awaitTermination(timeout, unit);%0A    %7D%0A%0A    @Override%0A    public boolean isShutdown() %7B%0A        return delegate().isShutdown();%0A    %7D%0A%0A    @Override%0A    public boolean isTerminated() %7B%0A        return delegate().isTerminated();%0A    %7D%0A%0A    @Override%0A    public void shutdown() %7B%0A        delegate().shutdown();%0A    %7D%0A%0A    @Override%0A    public List%3CRunnable%3E shutdownNow() %7B%0A        return delegate().shutdownNow();%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return delegate().toString(
 );%0A 

Implement `InterceptingExecutorService` without Guava (#5565)