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

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

There is a newer version: 2.0-M5
Show newest version
package org.reactfx.value;

import javafx.beans.value.ObservableValue;

import org.reactfx.Subscription;

class OrElse extends ValBase {
    private final ObservableValue src;
    private final ObservableValue other;

    private boolean trySrc; // irrelevant when not isConnected()


    OrElse(
            ObservableValue src,
            ObservableValue other) {
        this.src = src;
        this.other = other;
    }

    @Override
    protected Subscription connect() {
        trySrc = true;
        Subscription sub1 = Val.observeInvalidations(src, obs -> {
            trySrc = true;
            invalidate();
        });
        Subscription sub2 = Val.observeInvalidations(other, obs -> {
            if(!trySrc) {
                invalidate();
            }
        });
        return sub1.and(sub2);
    }

    @Override
    protected T computeValue() {
        if(!isObservingInputs()) {
            T val = src.getValue();
            return val != null ? val : other.getValue();
        } else {
            if(trySrc) {
                T val = src.getValue();
                if(val != null) {
                    return val;
                } else {
                    trySrc = false;
                }
            }
            return other.getValue();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy