org.reactfx.value.OrElse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reactfx Show documentation
Show all versions of reactfx Show documentation
Reactive event streams for JavaFX
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();
}
}
}