package be.iminds.ilabt.jfed.highlevel.jobs;

import be.iminds.ilabt.jfed.experiment.Experiment;
import be.iminds.ilabt.jfed.highlevel.controller.Task;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
import be.iminds.ilabt.jfed.highlevel.controller.TaskThread;
import be.iminds.ilabt.jfed.highlevel.tasks.HighLevelTaskFactory;

/**
 * User: twalcari
 * Date: 12/1/2015
 * Time: 16:58
 */
public abstract class SingleTaskJob<T extends Task, V> extends AbstractJob<V> {



    public SingleTaskJob(String name, Experiment experiment, HighLevelTaskFactory hltf, TaskThread tt) {
        super(name, experiment, hltf, tt);
    }

    @Override
    public V execute() throws Exception {
        final ExecuteTaskState executeTaskState = new ExecuteTaskState();
        setAndRunState(executeTaskState);

        onFinished(executeTaskState.getTask(), executeTaskState.getTaskExecution());
        return getResult();
    }

    protected V getResult(){
        return null;
    }

    protected void onFinished(T task, TaskExecution<T> taskExecution){}

    public abstract T createTask();

    public class ExecuteTaskState extends SingleCallState<T> {
        protected ExecuteTaskState() {
            super(SingleTaskJob.this.getName(), createTask());
        }
    }
}
