All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fitnesse.util.SerialExecutorService Maven / Gradle / Ivy

There is a newer version: 20181217
Show newest version
package fitnesse.util;

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

import org.apache.commons.lang.NotImplementedException;

/**
 * This implementation of {@link java.util.concurrent.ExecutorService} is a dummy/debug version of an execution
 * service. The tasks are executed instantly, in the current execution thread.
 */
public class SerialExecutorService implements ExecutorService {

  @Override
  public  Future submit(Callable task) {
    try {
      return new FutureIsNow<>(task.call());
    } catch (Exception e) {
      return new FutureIsNow<>(e);
    }
  }

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

  @Override
  public Future submit(Runnable task) {
    task.run();
    return new FutureIsNow(null);
  }

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

  @Override
  public void shutdown() {
  }

  @Override
  public List shutdownNow() {
    return Collections.emptyList();
  }

  @Override
  public boolean isShutdown() {
    return true;
  }

  @Override
  public boolean isTerminated() {
    return true;
  }

  @Override
  public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
    return true;
  }

  @Override
  public  List> invokeAll(Collection> tasks) throws InterruptedException {
    throw new NotImplementedException();
  }

  @Override
  public  List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException {
    throw new NotImplementedException();
  }

  @Override
  public  T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException {
    throw new NotImplementedException();
  }

  @Override
  public  T invokeAny(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    throw new NotImplementedException();
  }

}

class FutureIsNow implements Future {

  private final T result;
  private final Exception exc;

  public FutureIsNow(T result) {
    this.result = result;
    this.exc = null;
  }

  public FutureIsNow(Exception exc) {
    this.result = null;
    this.exc = exc;
  }

  @Override
  public boolean cancel(boolean mayInterruptIfRunning) {
    return false;
  }

  @Override
  public boolean isCancelled() {
    return false;
  }

  @Override
  public boolean isDone() {
    return true;
  }

  @Override
  public T get() throws InterruptedException, ExecutionException {
    if (exc != null) {
      throw new ExecutionException(exc);
    }
    return result;
  }

  @Override
  public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    return get();
  }
}