|
27 | 27 |
|
28 | 28 | import java.util.function.Function;
|
29 | 29 |
|
| 30 | +import com.sun.javafx.binding.ConditionalBinding; |
30 | 31 | import com.sun.javafx.binding.FlatMappedBinding;
|
31 | 32 | import com.sun.javafx.binding.MappedBinding;
|
32 | 33 | import com.sun.javafx.binding.OrElseBinding;
|
@@ -251,4 +252,49 @@ default ObservableValue<T> orElse(T constant) {
|
251 | 252 | default <U> ObservableValue<U> flatMap(Function<? super T, ? extends ObservableValue<? extends U>> mapper) {
|
252 | 253 | return new FlatMappedBinding<>(this, mapper);
|
253 | 254 | }
|
| 255 | + |
| 256 | + /** |
| 257 | + * Returns an {@code ObservableValue} that holds this value and is updated only |
| 258 | + * when {@code condition} holds {@code true}. |
| 259 | + * <p> |
| 260 | + * The returned {@code ObservableValue} only observes this value when |
| 261 | + * {@code condition} holds {@code true}. This allows this {@code ObservableValue} |
| 262 | + * and the conditional {@code ObservableValue} to be garbage collected if neither is |
| 263 | + * otherwise strongly referenced when {@code condition} holds {@code false}. |
| 264 | + * This is in contrast to the general behavior of bindings, where the binding is |
| 265 | + * only eligible for garbage collection when not observed itself. |
| 266 | + * <p> |
| 267 | + * A {@code condition} holding {@code null} is treated as holding {@code false}. |
| 268 | + * <p> |
| 269 | + * For example: |
| 270 | + * <pre>{@code |
| 271 | + * ObservableValue<Boolean> condition = new SimpleBooleanProperty(true); |
| 272 | + * ObservableValue<String> longLivedProperty = new SimpleStringProperty("A"); |
| 273 | + * ObservableValue<String> whenProperty = longLivedProperty.when(condition); |
| 274 | + * |
| 275 | + * // observe whenProperty, which will in turn observe longLivedProperty |
| 276 | + * whenProperty.addListener((ov, old, current) -> System.out.println(current)); |
| 277 | + * |
| 278 | + * longLivedProperty.setValue("B"); // "B" is printed |
| 279 | + * |
| 280 | + * condition.setValue(false); |
| 281 | + * |
| 282 | + * // After condition becomes false, whenProperty stops observing longLivedProperty; condition |
| 283 | + * // and whenProperty may now be eligible for GC despite being observed by the ChangeListener |
| 284 | + * |
| 285 | + * longLivedProperty.setValue("C"); // nothing is printed |
| 286 | + * longLivedProperty.setValue("D"); // nothing is printed |
| 287 | + * |
| 288 | + * condition.setValue(true); // longLivedProperty is observed again, and "D" is printed |
| 289 | + * }</pre> |
| 290 | + * |
| 291 | + * @param condition a boolean {@code ObservableValue}, cannot be {@code null} |
| 292 | + * @return an {@code ObservableValue} that holds this value whenever the given |
| 293 | + * condition evaluates to {@code true}, otherwise holds the last seen value; |
| 294 | + * never returns {@code null} |
| 295 | + * @since 20 |
| 296 | + */ |
| 297 | + default ObservableValue<T> when(ObservableValue<Boolean> condition) { |
| 298 | + return new ConditionalBinding<>(this, condition); |
| 299 | + } |
254 | 300 | }
|
0 commit comments