package be.iminds.ilabt.jfed.experimenter_gui.util.ui;

import javafx.beans.binding.ObjectBinding;
import javafx.beans.binding.ObjectExpression;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.function.Function;

/**
 * User: twalcari
 * Date: 10/13/2015
 * Time: 16:23
 */
public class TransformingObjectBinding<S,T> extends ObjectBinding<T>{

    private final ObjectExpression<S> source;
    private final Function<S,T> transformationFunction;

    public TransformingObjectBinding(ObjectExpression<S> source, Function<S, T> transformationFunction) {
        this.source = source;
        this.transformationFunction = transformationFunction;

        super.bind(source);
    }


    @Override
    protected T computeValue() {
        return transformationFunction.apply(source.get());
    }

    @Override
    public void dispose() {
        super.unbind(source);
    }


    @Override
    public ObservableList<ObservableValue<?>> getDependencies() {
        return FXCollections.singletonObservableList(source);
    }
}
