package com.hammurapi.common.concurrent;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Executes all tasks in the current thread. This executor service can be used in situations when
 * inherently asynchronous class shall operate in synchronous mode. 
 * @author Pavel Vlasov
 *
 */
public class CallerThreadExecutorService extends AbstractExecutorService {
	
	private CallerThreadExecutorService() {
		
	}
	
	public final static ExecutorService INSTANCE = new CallerThreadExecutorService();

	@Override
	public void shutdown() {
		// Nothing to do
	}

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

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

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

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

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

}
