Skip to content
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

EasyObservableList improvements #22

Merged
merged 3 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/tobiasdiez/easybind/EasyBind.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@ public static <T, U> EasyObservableList<U> map(ObservableList<? extends T> sourc
return new MappedList<>(sourceList, f);
}

public static <T> ObservableList<T> flatten(ObservableList<ObservableList<? extends T>> sources) {
public static <T> EasyObservableList<T> flatten(ObservableList<ObservableList<? extends T>> sources) {
return new FlattenedList<>(sources);
}

@SafeVarargs
public static <T> ObservableList<T> concat(ObservableList<? extends T>... sources) {
public static <T> EasyObservableList<T> concat(ObservableList<? extends T>... sources) {
return new FlattenedList<>(FXCollections.observableArrayList(sources));
}

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/tobiasdiez/easybind/EasyObservableList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,28 @@ default FilteredList<E> filtered(ObservableValue<? extends Predicate<E>> 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
piegamesde marked this conversation as resolved.
Show resolved Hide resolved
* type.
*
* This method will be deleted once https://github.com/openjdk/jfx/pull/278 is merged
*/
default EasyObservableList<E> filteredWrapped(ObservableValue<? extends Predicate<E>> 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 <U> EasyObservableList<U> mapped(Function<? super E, ? extends U> f) {
piegamesde marked this conversation as resolved.
Show resolved Hide resolved
return EasyBind.map(this, f);
}

/**
* @see EasyBind#valueAt(ObservableList, int)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tobiasdiez/easybind/FlattenedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import javafx.collections.ObservableList;
import javafx.collections.ObservableListBase;

class FlattenedList<E> extends ObservableListBase<E> {
class FlattenedList<E> extends ObservableListBase<E> implements EasyObservableList<E> {
private final ObservableList<ObservableList<? extends E>> sourceLists;

FlattenedList(ObservableList<ObservableList<? extends E>> sourceLists) {
Expand Down