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

org.reactfx.value.VarFromVal Maven / Gradle / Ivy

package org.reactfx.value;

import java.util.function.Consumer;

import javafx.beans.value.ObservableValue;

import org.reactfx.Subscription;

class VarFromVal extends ProxyVal implements Var {
    private final Consumer setter;

    private Subscription binding = null;

    VarFromVal(Val underlying, Consumer setter) {
        super(underlying);
        this.setter = setter;
    }

    @Override
    public T getValue() {
        return getUnderlyingObservable().getValue();
    }

    @Override
    protected Consumer adaptObserver(Consumer observer) {
        return observer; // no adaptation needed
    }

    @Override
    public void bind(ObservableValue observable) {
        unbind();
        binding = Val.observeChanges(
                observable,
                (obs, oldVal, newVal) -> setValue(newVal));
        setValue(observable.getValue());
    }

    @Override
    public void unbind() {
        if(binding != null) {
            binding.unsubscribe();
            binding = null;
        }
    }

    @Override
    public boolean isBound() {
        return binding != null;
    }

    @Override
    public void setValue(T value) {
        setter.accept(value);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy