|
1 | 1 | /**
|
2 | 2 | * Copyright 2017 Netflix, Inc.
|
3 |
| - * |
| 3 | + * <p> |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
6 | 6 | * You may obtain a copy of the License at
|
7 |
| - * |
| 7 | + * <p> |
8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 |
| - * |
| 9 | + * <p> |
10 | 10 | * Unless required by applicable law or agreed to in writing, software
|
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18 | 18 | import io.reactivex.Observable;
|
19 | 19 | import io.reactivex.functions.Consumer;
|
20 | 20 | import io.reactivex.observables.ConnectableObservable;
|
| 21 | +import io.reactivex.rxjavafx.observables.JavaFxObservable; |
21 | 22 | import javafx.beans.binding.Binding;
|
| 23 | +import javafx.beans.value.ObservableValue; |
| 24 | + |
| 25 | +import java.util.Optional; |
22 | 26 |
|
23 | 27 | public enum JavaFxObserver {
|
24 | 28 | ;//no instances
|
| 29 | + |
25 | 30 | /**
|
26 | 31 | * Turns an Observable into an eager JavaFX Binding that subscribes immediately to the Observable. Calling the Binding's dispose() method will handle the unsubscription.
|
27 | 32 | */
|
28 | 33 | public static <T> Binding<T> toBinding(Observable<T> obs) {
|
29 |
| - BindingObserver<T> bindingObserver = new BindingObserver<>(e -> {}); |
| 34 | + return toBinding(obs, JavaFxObserver::onError); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Turns an Observable into an eager JavaFX Binding that subscribes immediately to the Observable. Calling the Binding's dispose() method will handle the unsubscription. |
| 39 | + */ |
| 40 | + public static <T> Binding<T> toBinding(Observable<T> obs, Consumer<Throwable> onErrorAction) { |
| 41 | + BindingObserver<T, T> bindingObserver = new BindingObserver<>(t -> t, onErrorAction); |
| 42 | + obs.subscribe(bindingObserver); |
| 43 | + return bindingObserver; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Turns an Observable into an eager JavaFX Binding that subscribes immediately to the Observable. Calling the Binding's dispose() method will handle the unsubscription. |
| 48 | + * This variant unmasks a nullable value as in {@link JavaFxObservable#valuesOf(ObservableValue, Object)} and emits null when the sentinel is encountered. |
| 49 | + */ |
| 50 | + public static <T> Binding<T> toNullBinding(Observable<T> obs, T nullSentinel) { |
| 51 | + return toNullBinding(obs, nullSentinel, JavaFxObserver::onError); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Turns an Observable into an eager JavaFX Binding that subscribes immediately to the Observable. Calling the Binding's dispose() method will handle the unsubscription. |
| 56 | + * This variant unmasks a nullable value as in {@link JavaFxObservable#valuesOf(ObservableValue, Object)} and emits null when the sentinel is encountered. |
| 57 | + */ |
| 58 | + public static <T> Binding<T> toNullBinding(Observable<T> obs, T nullSentinel, Consumer<Throwable> onErrorAction) { |
| 59 | + if (nullSentinel == null) { |
| 60 | + throw new NullPointerException("The null value sentinel must not be null."); |
| 61 | + } |
| 62 | + BindingObserver<T, T> bindingObserver = new BindingObserver<>(t -> t == nullSentinel ? null : t, onErrorAction); |
30 | 63 | obs.subscribe(bindingObserver);
|
31 | 64 | return bindingObserver;
|
32 | 65 | }
|
| 66 | + |
33 | 67 | /**
|
34 | 68 | * Turns an Observable into an eager JavaFX Binding that subscribes immediately to the Observable. Calling the Binding's dispose() method will handle the unsubscription.
|
| 69 | + * This variant unmasks a nullable value as in {@link JavaFxObservable#nullableValuesOf(ObservableValue)} and emits null when the value is not present. |
35 | 70 | */
|
36 |
| - public static <T> Binding<T> toBinding(Observable<T> obs, Consumer<Throwable> onErrorAction ) { |
37 |
| - BindingObserver<T> bindingObserver = new BindingObserver<>(onErrorAction); |
| 71 | + public static <T> Binding<T> toNullableBinding(Observable<Optional<T>> obs) { |
| 72 | + return toNullableBinding(obs, JavaFxObserver::onError); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Turns an Observable into an eager JavaFX Binding that subscribes immediately to the Observable. Calling the Binding's dispose() method will handle the unsubscription. |
| 77 | + * This variant unmasks a nullable value as in {@link JavaFxObservable#nullableValuesOf(ObservableValue)} and emits null when the value is not present. |
| 78 | + */ |
| 79 | + public static <T> Binding<T> toNullableBinding(Observable<Optional<T>> obs, Consumer<Throwable> onErrorAction) { |
| 80 | + BindingObserver<Optional<T>, T> bindingObserver = new BindingObserver<>(o -> o.orElse(null), onErrorAction); |
38 | 81 | obs.subscribe(bindingObserver);
|
39 | 82 | return bindingObserver;
|
40 | 83 | }
|
| 84 | + |
41 | 85 | /**
|
42 | 86 | * Turns an Observable into an lazy JavaFX Binding that subscribes to the Observable when its getValue() is called. Calling the Binding's dispose() method will handle the unsubscription.
|
43 | 87 | */
|
44 | 88 | public static <T> Binding<T> toLazyBinding(Observable<T> obs) {
|
| 89 | + return toLazyBinding(obs, JavaFxObserver::onError); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Turns an Observable into an eager JavaFX Binding that subscribes to the Observable when its getValue() is called. Calling the Binding's dispose() method will handle the unsubscription. |
| 94 | + */ |
| 95 | + public static <T> Binding<T> toLazyBinding(Observable<T> obs, Consumer<Throwable> onErrorAction) { |
45 | 96 | ConnectableObservable<T> published = obs.publish();
|
46 |
| - BindingObserver<T> bindingObserver = new BindingObserver<>(published, e -> {}); |
| 97 | + BindingObserver<T, T> bindingObserver = new BindingObserver<>(t -> t, published, onErrorAction); |
47 | 98 | published.subscribe(bindingObserver);
|
48 | 99 | return bindingObserver;
|
49 | 100 | }
|
| 101 | + |
50 | 102 | /**
|
51 | 103 | * Turns an Observable into an eager JavaFX Binding that subscribes to the Observable when its getValue() is called. Calling the Binding's dispose() method will handle the unsubscription.
|
| 104 | + * This variant unmasks a nullable value as in {@link JavaFxObservable#valuesOf(ObservableValue, Object)} and emits null when the sentinel is encountered. |
52 | 105 | */
|
53 |
| - public static <T> Binding<T> toLazyBinding(Observable<T> obs, Consumer<Throwable> onErrorAction ) { |
| 106 | + public static <T> Binding<T> toLazyNullBinding(Observable<T> obs, T nullSentinel) { |
| 107 | + return toLazyNullBinding(obs, nullSentinel, JavaFxObserver::onError); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Turns an Observable into an eager JavaFX Binding that subscribes to the Observable when its getValue() is called. Calling the Binding's dispose() method will handle the unsubscription. |
| 112 | + * This variant unmasks a nullable value as in {@link JavaFxObservable#valuesOf(ObservableValue, Object)} and emits null when the sentinel is encountered. |
| 113 | + */ |
| 114 | + public static <T> Binding<T> toLazyNullBinding(Observable<T> obs, T nullSentinel, Consumer<Throwable> onErrorAction) { |
| 115 | + if (nullSentinel == null) { |
| 116 | + throw new NullPointerException("The null value sentinel must not be null."); |
| 117 | + } |
54 | 118 | ConnectableObservable<T> published = obs.publish();
|
55 |
| - BindingObserver<T> bindingObserver = new BindingObserver<>(published,onErrorAction); |
| 119 | + BindingObserver<T, T> bindingObserver = new BindingObserver<>(t -> t == nullSentinel ? null : t, published, onErrorAction); |
56 | 120 | published.subscribe(bindingObserver);
|
57 | 121 | return bindingObserver;
|
58 | 122 | }
|
| 123 | + |
| 124 | + /** |
| 125 | + * Turns an Observable into an lazy JavaFX Binding that subscribes to the Observable when its getValue() is called. Calling the Binding's dispose() method will handle the unsubscription. |
| 126 | + * This variant unmasks a nullable value as in {@link JavaFxObservable#nullableValuesOf(ObservableValue)} and emits null when the value is not present. |
| 127 | + */ |
| 128 | + public static <T> Binding<T> toLazyNullableBinding(Observable<Optional<T>> obs) { |
| 129 | + return toLazyNullableBinding(obs, JavaFxObserver::onError); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Turns an Observable into an lazy JavaFX Binding that subscribes to the Observable when its getValue() is called. Calling the Binding's dispose() method will handle the unsubscription. |
| 134 | + * This variant unmasks a nullable value as in {@link JavaFxObservable#nullableValuesOf(ObservableValue)} and emits null when the value is not present. |
| 135 | + */ |
| 136 | + public static <T> Binding<T> toLazyNullableBinding(Observable<Optional<T>> obs, Consumer<Throwable> onErrorAction) { |
| 137 | + ConnectableObservable<Optional<T>> published = obs.publish(); |
| 138 | + BindingObserver<Optional<T>, T> bindingObserver = new BindingObserver<>(o -> o.orElse(null), published, onErrorAction); |
| 139 | + published.subscribe(bindingObserver); |
| 140 | + return bindingObserver; |
| 141 | + } |
| 142 | + |
| 143 | + private static void onError(Throwable t) { |
| 144 | + // nothing |
| 145 | + } |
59 | 146 | }
|
0 commit comments