forked from Froussios/Intro-To-RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample2_2.java
More file actions
250 lines (211 loc) · 7.59 KB
/
Sample2_2.java
File metadata and controls
250 lines (211 loc) · 7.59 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
public class Sample2_2 {
//Reducing a sequence
public void filter() {
// If the decision is false, the item is ommited from the filtered sequence.
Observable<Integer> values = Observable.range(0, 10);
Disposable oddNumbers = values
.filter(v -> v % 2 == 0)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
}
public void distinct() {
Observable<Integer> values = Observable.create(o -> {
o.onNext(1);
o.onNext(1);
o.onNext(2);
o.onNext(3);
o.onNext(2);
o.onComplete();
});
Disposable disposable = values
.distinct()
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
}
public void distinct_keySelector() {
//An overload of distinct takes a key selector. For each item, the function generates a key and the key is then used to determine distinctiveness
Observable<String> values = Observable.create(o -> {
o.onNext("First");
o.onNext("Second");
o.onNext("Third");
o.onNext("Fourth");
o.onNext("Fifth");
o.onComplete();
});
Disposable disposable = values
.distinct(v -> v.charAt(0))
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
}
public void distinct_untilChanged() {
// The difference is that only consecutive non-distinct values are filtered out
Observable<Integer> values = Observable.create(o -> {
o.onNext(1);
o.onNext(1);
o.onNext(2);
o.onNext(3);
o.onNext(2);
o.onComplete();
});
Disposable disposable = values
.distinctUntilChanged()
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
}
public void distinct_untilChanged_keySelector() {
Observable<String> values = Observable.create(o -> {
o.onNext("First");
o.onNext("Second");
o.onNext("Third");
o.onNext("Fourth");
o.onNext("Fifth");
});
Disposable disposable = values
.distinctUntilChanged(v -> v.charAt(0))
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
}
public void ignoreElements() {
//ignoreElement will ignore every value, but lets pass through onCompleted and onError
Observable<Integer> values = Observable.range(0, 10);
Disposable disposable = values
.ignoreElements()
.subscribe(
() -> System.out.println("Completed"),
e -> System.out.println("Error: " + e)
);
//ignoreElements() produces the same results as `filter(v -> false)`
}
public void take() {
// The next group of methods serve to cut the sequence at a specific point based on the item's index,
// and either take the first part or the second part
// `take` takes the first n elements, while `skip` skips them.
// Note that neither function considers it an error if there are fewer items in the sequence than the specified index.
Observable<Integer> values = Observable.range(0, 5);
Disposable take2 = values
.take(2)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
}
public void take_with_error() {
Observable<Integer> values = Observable.create(o -> {
o.onNext(1);
o.onError(new Exception("Oops"));
});
Disposable disposable = values
.take(1)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
}
public void skip() {
// `skip` returns the other half of a take
Observable<Integer> values = Observable.range(0, 5);
Disposable disposable = values
.skip(2)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
}
public void take_moment() {
// There are overloads where the cutoff is a moment in time ather than place in the sequence
Observable<Long> values = Observable.interval(100, TimeUnit.MILLISECONDS);
Disposable disposable = values
.take(250, TimeUnit.MILLISECONDS)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
try{System.in.read();} catch(Exception ignore){}
}
public void takeWhile() {
//`take` and `skip` work with predefined indices. If you want to "discover" the cutoff point as the values come,
//`takeWhile` and `skipWhile` will use a predicate instead. `takeWhile` takes items while a predicate function returns `true`
Observable<Long> values = Observable.interval(100, TimeUnit.MILLISECONDS);
Disposable disposable = values
.takeWhile(v -> v < 2)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
try{System.in.read();} catch(Exception ignore){}
}
public void skipWhile() {
Observable<Long> values = Observable.interval(100, TimeUnit.MILLISECONDS);
Disposable disposable = values
.skipWhile(v -> v < 2)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
try{System.in.read();} catch(Exception ignore){}
}
public void skipLast() {
//`skipLast` and `takeLast` work just like `take` and `skip`, with the difference that the point of reference is from the end
Observable<Integer> values = Observable.range(0, 5);
Disposable disposable = values
.skipLast(2)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
}
public void takeUntil() {
// The cutoff point is defined as the moment when another obervable emits an item
Observable<Long> values = Observable.interval(100, TimeUnit.MILLISECONDS);
Observable<Long> cutoff = Observable.timer(250, TimeUnit.MILLISECONDS);
Disposable disposable = values
.takeUntil(cutoff)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
try{System.in.read();} catch(Exception ignore){}
}
public void skipUntil() {
Observable<Long> values = Observable.interval(100, TimeUnit.MILLISECONDS);
Observable<Long> cutoff = Observable.timer(250, TimeUnit.MILLISECONDS);
Disposable disposable = values
.skipUntil(cutoff)
.subscribe(
v -> System.out.println(v),
e -> System.out.println("Error: " + e),
() -> System.out.println("Completed")
);
try{System.in.read();} catch(Exception ignore){}
}
public static void main(String[] args) {
Sample2_2 sample = new Sample2_2();
sample.skipUntil();
}
}