Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit c76728c

Browse files
committed
Added method to map the values of an ObservableList prior to linking them to a ListProperty from JavaFX. This does not work with sets as there's no guarantee the mapper will consistently return the same object for a given input object.
1 parent 0273825 commit c76728c

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ subprojects {
44
apply plugin: 'signing'
55

66
group 'net.fhannes.rx'
7-
version '0.2.0'
7+
version '0.2.1'
88
archivesBaseName = "rxjavacollections"
99

1010
sourceCompatibility = 1.8

core/src/main/java/net/fhannes/rx/collections/ObservableList.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package net.fhannes.rx.collections;
2020

21-
import com.sun.javafx.scene.transform.TransformUtils;
2221
import io.reactivex.Observable;
2322
import io.reactivex.subjects.BehaviorSubject;
2423
import io.reactivex.subjects.PublishSubject;

core/src/main/java/net/fhannes/rx/collections/util/Pair.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.Objects;
2222

2323
/**
24-
* A utility class representing a pair of objects.
24+
* An immutable utility class representing a pair of objects.
2525
*
2626
* @param <A> The type of the first object.
2727
* @param <B> The type of the second object.
@@ -48,10 +48,20 @@ public static <A, B> Pair<A, B> of(A a, Pair<?, B> p) {
4848
this.b = b;
4949
}
5050

51+
/**
52+
* Returns the first value of the value pair.
53+
*
54+
* @return The stored object.
55+
*/
5156
public A getA() {
5257
return a;
5358
}
5459

60+
/**
61+
* Returns the second value of the value pair.
62+
*
63+
* @return The stored object.
64+
*/
5565
public B getB() {
5666
return b;
5767
}

javafx/src/main/java/net/fhannes/rx/collections/fx/JavaFXAdaptor.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package net.fhannes.rx.collections.fx;
2020

21+
import io.reactivex.functions.Function;
2122
import net.fhannes.rx.collections.ObservableList;
2223
import 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

Comments
 (0)