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

import be.iminds.ilabt.jfed.highlevel.controller.Task;
import javafx.util.Pair;

import java.util.HashMap;

/**
 * User: twalcari
 * Date: 6/16/2015
 * Time: 16:09
 */
public abstract class CacheablePairTaskFactory<R,S, T extends Task> {

    private final HashMap<Pair<R,S>, T> cachedTasks = new HashMap<>();

    public synchronized T getTask(R r, S s) {
        final Pair<R, S> key = new Pair<>(r, s);
        return cachedTasks.computeIfAbsent(key, rsPair -> createNewTask(rsPair.getKey(), rsPair.getValue()));
    }

    protected abstract T createNewTask(R r, S s);


}
