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/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/EasyObservableList.java b/src/main/java/com/tobiasdiez/easybind/EasyObservableList.java index c465b29..e3b47cc 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,28 @@ 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. + * + * 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 EasyBind.map(this, f); + } /** * @see EasyBind#valueAt(ObservableList, int) 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) {