Skip to content

Commit

Permalink
Added ObservableLists.map() utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
salmonb committed May 8, 2024
1 parent d7791f6 commit 4cacce2
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.webfx.platform.util.function.Converter;
import javafx.beans.binding.BooleanExpression;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;

Expand Down Expand Up @@ -46,7 +47,7 @@ public static <A, B> void setAllNonNullsConverted(List<A> aList, Converter<A, B>
setAllNonNulls(bList, Collections.map(aList, aToBConverter));
}

public static <T> void bind(ObservableList<T> list1, ObservableList<T> list2) {
public static <A, B extends A> void bind(ObservableList<A> list1, ObservableList<B> list2) {
runNowAndOnListChange(c -> list1.setAll(list2), list2);
}

Expand All @@ -58,6 +59,12 @@ public static <A, B> void bindConverted(ObservableList<A> aList, ObservableList<
runNowAndOnListChange(c -> setAllConverted(bList, bToAConverter, aList), bList);
}

public static <A, B> ObservableList<A> map(ObservableList<B> bList, Converter<B, A> bToAConverter) {
ObservableList<A> aList = FXCollections.observableArrayList();
bindConverted(aList, bList, bToAConverter);
return aList;
}

public static <T> void runNowAndOnListChange(ListChangeListener<T> listener, ObservableList<T> list) {
listener.onChanged(null);
runOnListChange(listener, list);
Expand Down

0 comments on commit 4cacce2

Please sign in to comment.