-
Notifications
You must be signed in to change notification settings - Fork 544
PoC: Fluent bindings for ObservableValue #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
hjohn
wants to merge
1
commit into
openjdk:master
from
hjohn:feature/observable-value-fluent-bindings
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
modules/javafx.base/src/main/java/javafx/beans/value/Bindings.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| }; | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
modules/javafx.base/src/main/java/javafx/beans/value/ConditionalBinding.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
modules/javafx.base/src/main/java/javafx/beans/value/FlatMapBinding.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }; | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
modules/javafx.base/src/main/java/javafx/beans/value/LazyObjectBinding.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.