package com.splicemachine.concurrent;

import com.google.common.collect.Lists;
import com.sun.xml.internal.ws.util.CompletedFuture;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Scott Fines
 *         Date: 4/11/14
 */
public class SameThreadExecutorService implements ExecutorService{
		private static final ExecutorService INSTANCE = new SameThreadExecutorService();

		public static ExecutorService instance(){
				return INSTANCE;
		}

		private SameThreadExecutorService(){}

		@Override public void shutdown() {  }
		@Override public List<Runnable> shutdownNow() { return null; }
		@Override public boolean isShutdown() { return false; }
		@Override public boolean isTerminated() { return false; }
		@Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { return false; }

		@Override
		public <T> Future<T> submit(Callable<T> task) {
				try {
						T call = task.call();
						return value(call);
				} catch (Exception e) {
					return error(e);
				}
		}

		private <T> Future<T> error(Exception e) {
				return new CompletedFuture<T>(null,e);
		}

		private <T> Future<T> value(T call) {
				return new CompletedFuture<T>(call,null);
		}

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

		@Override
		public Future<?> submit(Runnable task) {
				task.run();
				return value(null);
		}

		@Override
		public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
				List<Future<T>> completedFutures = Lists.newArrayListWithExpectedSize(tasks.size());
				for(Callable<T> task:tasks){
						completedFutures.add(submit(task));
				}
				return completedFutures;
		}

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

		@Override
		public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
				for(Callable<T> task:tasks){
						if(task!=null)
								return submit(task).get();
				}
				throw new IllegalArgumentException("No tasks to execute!");
		}

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

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

@@ -75,61 +75,8 @@
 sts;
-%0Aimport com.sun.xml.internal.ws.util.CompletedFuture;
 %0A%0Aim
@@ -2494,10 +2494,963 @@
 ();%0A%09%09%7D%0A
+    private static class CompletedFuture %3CT%3E implements Future%3CT%3E %7B%0A        private final T element;%0A%0A        private final Throwable error;%0A%0A        private CompletedFuture(T element, Throwable error) %7B%0A            this.element = element;%0A            this.error = error;%0A        %7D%0A%0A        @Override%0A        public boolean cancel(boolean b) %7B%0A            return false;%0A        %7D%0A%0A        @Override%0A        public boolean isCancelled() %7B%0A            return false;%0A        %7D%0A%0A        @Override%0A        public boolean isDone() %7B%0A            return true;%0A        %7D%0A%0A        @Override%0A        public T get() throws InterruptedException, ExecutionException %7B%0A            if (error != null)%0A                throw new ExecutionException(error);%0A            return element;%0A        %7D%0A%0A        @Override%0A        public T get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException %7B%0A            return get();%0A        %7D%0A    %7D%0A
 %7D%0A

correct compile error with sfine's help, this commit does produce a unit test failure