-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollapsingWIthRxJava2Example.java
64 lines (44 loc) · 1.4 KB
/
CollapsingWIthRxJava2Example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package challenge4.rxjava2;
import externalLegacyCodeNotUnderOurControl.PriceService;
import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static externalLegacyCodeNotUnderOurControl.PrintlnWithThreadname.println;
import static java.lang.Thread.sleep;
/**
* This example uses RxJava2.
*
* @author Jarek Ratajski
*
*/
public class CollapsingWIthRxJava2Example {
public static void main(final String... args) throws InterruptedException {
new CollapsingWIthRxJava2Example().run();
sleep(10_000);
}
/**
* Create the price services.
*/
private CollapsingWIthRxJava2Example() {
}
/**
* Use RxJava2 to call the price services.
*/
private void run() {
final PriceService priceService = new PriceService();
final Flowable<Integer> priceCall = Flowable.fromCallable(priceService::getPrice);
final Flowable<Integer> calls = Flowable.range(1,10);
Flowable.combineLatest(priceCall, calls, (price, ignored) -> price).subscribe(this::collector);
}
/**
* Collect the answers and calculate the average price.
*/
private synchronized void collector(final int price) {
System.out.println(price);
}
}