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

de.invation.code.toval.thread.AbstractCallable Maven / Gradle / Ivy

package de.invation.code.toval.thread;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;

public abstract class AbstractCallable implements Callable{

	private Set> listeners = new HashSet>();
	private V callableResult;
	
	public void addCallableListener(CallableListener listener){
		listeners.add(listener);
	}

	public void removeCallableListener(CallableListener listener){
		listeners.remove(listener);
	}

	@Override
	public V call() throws Exception {
		notifyExecutionStarted();
		try{
			callableResult = callRoutine();
		}catch(Exception e){
			notifyExecutionStopped();
			notifyException(e);
			throw e;
		}
		notifyExecutionFinished(callableResult);
		return callableResult;
	}
	
	protected void notifyExecutionStarted(){
		for(CallableListener listener: listeners)
			listener.callableStarted();
	}
	
	protected void notifyExecutionStopped(){
		for(CallableListener listener: listeners)
			listener.callableStopped();
	}
	
	protected void notifyExecutionFinished(V result){
		for(CallableListener listener: listeners)
			listener.callableFinished(result);
	}
	
	protected void notifyException(Exception exception){
		for(CallableListener listener: listeners)
			listener.callableException(exception);
	}
	
	public V getCallableResult(){
		return callableResult;
	}

	protected abstract V callRoutine() throws Exception;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy