Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ public ObservableList<?> getDependencies() {
public final T get() {
if (!valid) {
value = computeValue();
valid = true;

if (allowValidation()) {
valid = true;
}
}
return value;
}
Expand All @@ -182,6 +185,28 @@ public final boolean isValid() {
return valid;
}

/**
* Returns {@code true} when this binding currently has one or more
* listeners, otherwise {@code false}.
*
* @return {@code true} when this binding currently has one or more
* listeners, otherwise {@code false}
*/
protected final boolean isObserved() {
return helper != null;
}

/**
* Can be overriden in extending classes to prevent a binding from becoming
* valid. The default implementation always allows bindings to become valid.
*
* @return {@code true} if this binding is allowed to become valid, otherwise
* {@code false}
*/
protected boolean allowValidation() {
return true;
}

/**
* Calculates the current value of this binding.
* <p>
Expand Down
35 changes: 35 additions & 0 deletions modules/javafx.base/src/main/java/javafx/beans/value/Bindings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package javafx.beans.value;

import java.util.function.Function;

/**
* Support class which supplies varies types of bindings.
*/
class Bindings {

public static <T, U> ObservableValue<U> mapping(ObservableValue<T> source, Function<? super T, ? extends U> mapper) {
return nullableMapping(source, v -> v == null ? null : mapper.apply(v));
}

public static <T> ObservableValue<T> conditional(ObservableValue<T> source, ObservableValue<Boolean> condition) {
return new ConditionalBinding<>(condition, source);
}

public static <T, U> ObservableValue<U> flatMapping(ObservableValue<T> source, Function<? super T, ? extends ObservableValue<? extends U>> mapper) {
return new FlatMapBinding<>(source, mapper);
}

public static <T, U> ObservableValue<U> nullableMapping(ObservableValue<T> source, Function<? super T, ? extends U> mapper) {
return new LazyObjectBinding<>() {
@Override
protected Subscription observeInputs() {
return source.subscribeInvalidations(() -> invalidate()); // start observing source
}

@Override
protected U computeValue() {
return mapper.apply(source.getValue());
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package javafx.beans.value;

import java.util.Objects;

public class ConditionalBinding<T> extends LazyObjectBinding<T> {
private final ObservableValue<Boolean> condition;
private final ObservableValue<T> source;

private Subscription subscription;

public ConditionalBinding(ObservableValue<Boolean> condition, ObservableValue<T> source) {
this.source = Objects.requireNonNull(source);
this.condition = Objects.requireNonNull(condition);

condition.subscribeInvalidations(() -> {
invalidate();

if(!isActive()) {
getValue(); // make valid so last value is cached while conditional is false
}
});
}

@Override
protected boolean allowValidation() {
// This binding is valid when it is itself observed, or is currently inactive.
// When inactive, the binding has the value of its source at the time it became
// inactive.
return super.allowValidation() || !isActive();
}

@Override
protected T computeValue() {
unsubscribe();

if(isObserved() && isActive()) {
subscription = source.subscribeInvalidations(this::invalidate);
}

return source.getValue();
}

@Override
protected Subscription observeInputs() {
return this::unsubscribe; // condition is always observed and never unsubscribed
}

private boolean isActive() {
return Boolean.TRUE.equals(condition.getValue());
}

private void unsubscribe() {
if(subscription != null) {
subscription.unsubscribe();
subscription = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package javafx.beans.value;

import java.util.Objects;
import java.util.function.Function;

public class FlatMapBinding<S, T> extends LazyObjectBinding<T> {
private final ObservableValue<S> source;
private final Function<? super S, ? extends ObservableValue<? extends T>> mapper;

private Subscription mappedSubscription = Subscription.EMPTY;

public FlatMapBinding(
ObservableValue<S> source,
Function<? super S, ? extends ObservableValue<? extends T>> mapper
) {
this.source = Objects.requireNonNull(source);
this.mapper = Objects.requireNonNull(mapper);
}

@Override
protected T computeValue() {
S value = source.getValue();
ObservableValue<? extends T> mapped = value == null ? null : mapper.apply(value);

if(isObserved()) {
mappedSubscription.unsubscribe();
mappedSubscription = mapped == null ? Subscription.EMPTY : mapped.subscribeInvalidations(this::invalidate);
}

return mapped == null ? null : mapped.getValue();
}

@Override
protected Subscription observeInputs() {
Subscription subscription = source.subscribeInvalidations(this::invalidate);

return () -> {
subscription.unsubscribe();
mappedSubscription.unsubscribe();
mappedSubscription = Subscription.EMPTY;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package javafx.beans.value;

import javafx.beans.InvalidationListener;
import javafx.beans.binding.ObjectBinding;

/**
* Extends {@link ObjectBinding} with the ability to lazily register
* and eagerly unregister listeners on its dependencies.
*
* @param <T> the type of the wrapped {@code Object}
*/
public abstract class LazyObjectBinding<T> extends ObjectBinding<T> {
private Subscription subscription;
private boolean wasObserved;

@Override
public void addListener(ChangeListener<? super T> listener) {
super.addListener(listener);

updateSubcription();
}

@Override
public void removeListener(ChangeListener<? super T> listener) {
super.removeListener(listener);

updateSubcription();
}

@Override
public void addListener(InvalidationListener listener) {
super.addListener(listener);

updateSubcription();
}

@Override
public void removeListener(InvalidationListener listener) {
super.removeListener(listener);

updateSubcription();
}

@Override
protected boolean allowValidation() {
return isObserved();
}

private void updateSubcription() {
boolean isObserved = isObserved();

if(!wasObserved && isObserved) { // was first observer registered?
subscription = observeInputs(); // start observing source

/*
* Although the act of registering a listener already attempts to make
* this binding valid, allowValidation won't allow it as the binding is
* not observed yet. This is because isObserved will not yet return true
* when the process of registering the listener hasn't completed yet.
*
* As the binding must be valid after it becomes observed the first time
* 'get' is called again.
*/

get(); // make binding valid as source wasn't tracked until now
}
else if(wasObserved && !isObserved) { // was last observer unregistered?
subscription.unsubscribe();
subscription = null;
invalidate(); // make binding invalid as source is no longer tracked
}

wasObserved = isObserved;
}

/**
* Called when this binding was previously not observed and a new observer was added. Implementors
* must return a {@link Subscription} which will be cancelled when this binding no longer has any
* observers.
*
* @return a {@link Subscription} which will be cancelled when this binding no longer has any observers, never null
*/
protected abstract Subscription observeInputs();
}
Loading