From 0f5bcd22fd5275cc06522df666d9a0314db527e3 Mon Sep 17 00:00:00 2001 From: piegames Date: Tue, 28 Jul 2020 22:35:42 +0200 Subject: [PATCH 1/3] Make more use of EasyObservableList If we are returning an object from a class that is under "our" control, we should let the return type be `EasyObservableList` to avoid unnecessary (and ugly) `EasyBind#wrapList` calls. --- src/main/java/com/tobiasdiez/easybind/EasyBind.java | 4 ++-- src/main/java/com/tobiasdiez/easybind/FlattenedList.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/tobiasdiez/easybind/EasyBind.java b/src/main/java/com/tobiasdiez/easybind/EasyBind.java index f17ef27..cf46cad 100644 --- a/src/main/java/com/tobiasdiez/easybind/EasyBind.java +++ b/src/main/java/com/tobiasdiez/easybind/EasyBind.java @@ -295,12 +295,12 @@ public static EasyObservableList map(ObservableList sourc return new MappedList<>(sourceList, f); } - public static ObservableList flatten(ObservableList> sources) { + public static EasyObservableList flatten(ObservableList> sources) { return new FlattenedList<>(sources); } @SafeVarargs - public static ObservableList concat(ObservableList... sources) { + public static EasyObservableList concat(ObservableList... sources) { return new FlattenedList<>(FXCollections.observableArrayList(sources)); } diff --git a/src/main/java/com/tobiasdiez/easybind/FlattenedList.java b/src/main/java/com/tobiasdiez/easybind/FlattenedList.java index fb6ce1f..5a9033b 100644 --- a/src/main/java/com/tobiasdiez/easybind/FlattenedList.java +++ b/src/main/java/com/tobiasdiez/easybind/FlattenedList.java @@ -10,7 +10,7 @@ import javafx.collections.ObservableList; import javafx.collections.ObservableListBase; -class FlattenedList extends ObservableListBase { +class FlattenedList extends ObservableListBase implements EasyObservableList { private final ObservableList> sourceLists; FlattenedList(ObservableList> sourceLists) { From a94c64d5cdc59f3b770fafb66a2e9a21841807ef Mon Sep 17 00:00:00 2001 From: piegames Date: Tue, 28 Jul 2020 22:39:46 +0200 Subject: [PATCH 2/3] Add more methods to EasyObservableList --- .../tobiasdiez/easybind/EasyObservableList.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/com/tobiasdiez/easybind/EasyObservableList.java b/src/main/java/com/tobiasdiez/easybind/EasyObservableList.java index c465b29..f919ce0 100644 --- a/src/main/java/com/tobiasdiez/easybind/EasyObservableList.java +++ b/src/main/java/com/tobiasdiez/easybind/EasyObservableList.java @@ -5,6 +5,7 @@ import java.util.stream.Stream; import javafx.beans.binding.Bindings; import javafx.beans.binding.BooleanBinding; +import javafx.beans.property.Property; import javafx.beans.value.ObservableValue; import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; @@ -68,6 +69,19 @@ default FilteredList filtered(ObservableValue> predica filteredList.predicateProperty().bind(predicate); return filteredList; } + + /* + * Sadly, we cannot easily let {@link FilteredList} implement {@link EasyObservableList}, because that class is final. Neither can we return + * both at once, as intersection types do not exist in Java. Therefore, the easiest way is to simply create two methods, one for each return + * type. + */ + default EasyObservableList filteredWrapped(ObservableValue> predicate) { + return EasyBind.wrapList(filtered(predicate)); + } + + default EasyObservableList mapped(Function f) { + return new MappedList<>(this, f); + } /** * @see EasyBind#valueAt(ObservableList, int) From bd0914044198dba73be3824c9722ec2087d82124 Mon Sep 17 00:00:00 2001 From: piegames Date: Thu, 6 Aug 2020 15:03:05 +0200 Subject: [PATCH 3/3] Improvements from code review --- CHANGELOG.md | 7 +++++++ .../com/tobiasdiez/easybind/EasyObservableList.java | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bfae7f..012934d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file. ## [Unreleased] ### Added - Added support for JDK >= 9. [#21](https://github.com/tobiasdiez/EasyBind/issues/21) +- Added `EasyObservableList#mapped` as fluid alternative to `EasyBind#map` +- Added `EasyObservableList#filteredWrapped` as an alternative to `EasyObservableList#filtered` that returns an `EasyObservableList` instead of a `FilteredList`. + - This allows fluid method chaining without having to wrap the `FilteredList` using `EasyBind#wrapList` again. + - It is a temporary workaround that will be removed once https://github.com/openjdk/jfx/pull/278 is merged. + ### Changed +- `EasyBind#flatten` and `EasyBind#concat` now return an `EasyObservableList` instead of an `ObservableList`. + ### Removed ## [2.0.0] - 2020-07-17 diff --git a/src/main/java/com/tobiasdiez/easybind/EasyObservableList.java b/src/main/java/com/tobiasdiez/easybind/EasyObservableList.java index f919ce0..e3b47cc 100644 --- a/src/main/java/com/tobiasdiez/easybind/EasyObservableList.java +++ b/src/main/java/com/tobiasdiez/easybind/EasyObservableList.java @@ -74,13 +74,22 @@ default FilteredList filtered(ObservableValue> predica * Sadly, we cannot easily let {@link FilteredList} implement {@link EasyObservableList}, because that class is final. Neither can we return * both at once, as intersection types do not exist in Java. Therefore, the easiest way is to simply create two methods, one for each return * type. + * + * This method will be deleted once https://github.com/openjdk/jfx/pull/278 is merged */ default EasyObservableList filteredWrapped(ObservableValue> predicate) { return EasyBind.wrapList(filtered(predicate)); } + /** + * Creates a {@link MappedList} wrapper of this list using the specified mapping function. + * + * @param f the mapping function to transform the items + * @return new {@code MappedList} + * @see EasyBind#map(ObservableList, Function) + */ default EasyObservableList mapped(Function f) { - return new MappedList<>(this, f); + return EasyBind.map(this, f); } /**