1818 */
1919package net .fhannes .rx .collections .fx ;
2020
21+ import io .reactivex .functions .Function ;
2122import net .fhannes .rx .collections .ObservableList ;
2223import net .fhannes .rx .collections .ObservableSet ;
2324
@@ -35,6 +36,27 @@ private JavaFXAdaptor() {
3536
3637 }
3738
39+ /**
40+ * Creates an adaptor which maps an {@link ObservableList} object from RxJavaCollections onto a new
41+ * {@link javafx.collections.ObservableList} from JavaFX. The method takes a mapper argument to map the elements in
42+ * the given observable to a different value.
43+ *
44+ * @param list The given reactive list.
45+ * @param mapper Maps the elements of the given list to a different value.
46+ * @param <E> The type of the elements stored in the list.
47+ * @param <R> The type of the mapped elements stored in the {@link javafx.collections.ObservableList} object.
48+ * @return The newly constructed list which updates automatically when the given list is updated.
49+ */
50+ public static <E , R > javafx .collections .ObservableList <R > adapt (ObservableList <E > list ,
51+ Function <? super E , ? extends R > mapper ) {
52+ javafx .collections .ObservableList <R > newList = javafx .collections .FXCollections .observableArrayList ();
53+ list .observable ().map (mapper ).subscribe (newList ::add );
54+ list .onAdded ().subscribe (c -> newList .add (c .getIndex (), mapper .apply (c .getValue ())));
55+ list .onRemoved ().subscribe (c -> newList .remove (c .getIndex ()));
56+ list .onUpdatedChanged ().subscribe (c -> newList .set (c .getIndex (), mapper .apply (c .getValue ())));
57+ return newList ;
58+ }
59+
3860 /**
3961 * Creates an adaptor which maps an {@link ObservableList} object from RxJavaCollections onto a new
4062 * {@link javafx.collections.ObservableList} from JavaFX.
@@ -56,15 +78,15 @@ public static <E> javafx.collections.ObservableList<E> adapt(ObservableList<E> l
5678 * Creates an adaptor which maps an {@link ObservableSet} object from RxJavaCollections onto a new
5779 * {@link javafx.collections.ObservableSet} from JavaFX.
5880 *
59- * @param list The given reactive set.
81+ * @param set The given reactive set.
6082 * @param <E> The type of the elements stored in the set.
6183 * @return The newly constructed set which updates automatically when the given set is updated.
6284 */
63- public static <E > javafx .collections .ObservableSet <E > adapt (ObservableSet <E > list ) {
85+ public static <E > javafx .collections .ObservableSet <E > adapt (ObservableSet <E > set ) {
6486 javafx .collections .ObservableSet <E > newSet = javafx .collections .FXCollections .observableSet (new HashSet <>());
65- list .observable ().subscribe (newSet ::add );
66- list .onAdded ().subscribe (newSet ::add );
67- list .onRemoved ().subscribe (newSet ::remove );
87+ set .observable ().subscribe (newSet ::add );
88+ set .onAdded ().subscribe (newSet ::add );
89+ set .onRemoved ().subscribe (newSet ::remove );
6890 return newSet ;
6991 }
7092
0 commit comments