-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMappingExamples.java
121 lines (94 loc) · 3.2 KB
/
MappingExamples.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.packtpub.reactive.chapter04;
import static com.packtpub.reactive.common.Helpers.subscribePrint;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscription;
import rx.functions.Func0;
import rx.functions.Func1;
import com.packtpub.reactive.common.CreateObservable;
import com.packtpub.reactive.common.Program;
/**
* Demonstration of using map, flatMap, flatMapIterable and switchMap.
*
* @author meddle
*/
public class MappingExamples implements Program {
@Override
public String name() {
return "Examples of using Observable transformations";
}
@Override
public int chapter() {
return 4;
}
@Override
public void run() {
Observable<String> mapped = Observable.just(2, 3, 5, 8).map(v -> v * 3)
.map(v -> (v % 2 == 0) ? "even" : "odd");
subscribePrint(mapped, "map");
System.out.println("-----------------");
Observable<Integer> flatMapped = Observable.just(3, 5, 8).flatMap(
v -> Observable.range(v, v));
subscribePrint(flatMapped, "flatMap");
System.out.println("-----------------");
flatMapped = Observable
.<Integer>just(-1, 0, 1)
.map(v -> 2 / v)
.flatMap(
new Func1<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> call(Integer v) {
return Observable.<Integer>just(v);
}
},
new Func1<Throwable, Observable<Integer>>() {
@Override
public Observable<Integer> call(Throwable e) {
return Observable.just(0);
}
},
new Func0<Observable<Integer>>() {
@Override
public Observable<Integer> call() {
return Observable.just(42);
}
}
);
subscribePrint(flatMapped, "flatMap");
System.out.println("-----------------");
flatMapped = Observable.just(5, 432).flatMap(v -> Observable.range(v, 2),
(x, y) -> x + y);
subscribePrint(flatMapped, "flatMap");
System.out.println("-----------------");
Observable<String> fsObs = CreateObservable.listFolder(
Paths.get("src", "main", "resources"),
"{lorem.txt,letters.txt}")
.flatMap(path -> CreateObservable.from(path), (path, line) -> path.getFileName() + " : " + line);
subscribePrint(fsObs, "FS");
Observable<?> fMapped = Observable.just(Arrays.asList(2, 4),
Arrays.asList("two", "four"), Arrays.asList('t', 'f'),
Arrays.asList(true, false)).flatMap(l -> Observable.from(l));
subscribePrint(fMapped, "flatMap");
Observable<?> fIterableMapped = Observable.just(Arrays.asList(2, 4),
Arrays.asList("two", "four"), Arrays.asList('t', 'f'),
Arrays.asList(true, false)).flatMapIterable(l -> l);
subscribePrint(fIterableMapped, "flatMapIterable");
System.out.println("-----------------");
Observable<Object> obs = Observable
.interval(40L, TimeUnit.MILLISECONDS).switchMap(
v -> Observable.timer(0L, 10L, TimeUnit.MILLISECONDS)
.map(u -> "Observable <" + (v + 1) + "> : " + (v + u)));
Subscription sub = subscribePrint(obs, "switchMap");
try {
Thread.sleep(400L);
} catch (InterruptedException e) {
}
sub.unsubscribe();
System.out.println("-----------------");
}
public static void main(String[] args) {
new MappingExamples().run();
}
}