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

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

Go to download

TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.

The newest version!
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