/
TransformingOperatorsTest.java
302 lines (256 loc) · 10.2 KB
/
TransformingOperatorsTest.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.geniusmart.rxjava;
import com.geniusmart.rxjava.utils.OperatorUtils;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.schedulers.TestScheduler;
import static junit.framework.Assert.assertEquals;
/**
* Created by geniusmart on 2016/11/2.
* <p>
* Operators that transform items that are emitted by an Observable.
*/
public class TransformingOperatorsTest {
private TestScheduler mTestScheduler;
private List<Object> mList;
@Before
public void setUp() {
mTestScheduler = new TestScheduler();
mList = new ArrayList<>();
}
/**
* periodically gather items emitted by an Observable into bundles and emit these bundles
* rather than emitting the items one at a time
*
* @see <a href="http://reactivex.io/documentation/operators/buffer.html">ReactiveX
* operators documentation: Buffer</a>
*/
@Test
public void buffer() {
Observable.just(1, 2, 3, 4, 5, 6)
.buffer(3)
.subscribe(mList::add);
System.out.println(mList);
List<List<Integer>> exceptList = Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6));
assertEquals(mList, exceptList);
}
/**
* buffer(long,TimeUnit)
*
* @see <a href="http://reactivex.io/documentation/operators/images/buffer5.png">
* buffer diagram</a>
*/
@Test
public void bufferWithTimeUnit() {
Observable.interval(0, 1, TimeUnit.SECONDS, mTestScheduler)
.take(6)
.buffer(2, TimeUnit.SECONDS, mTestScheduler)
.subscribe(mList::add);
mTestScheduler.advanceTimeBy(10, TimeUnit.SECONDS);
System.out.println(mList);
List<List<Long>> expectedList = Arrays.asList(
Arrays.asList(0L, 1L),
Arrays.asList(2L, 3L),
Arrays.asList(4L, 5L));
assertEquals(mList, expectedList);
}
//TODO-bufferClosingSelector(是否准确待定)
@Test
public void bufferClosingSelector() {
Observable<Integer> observable = Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
subscriber.onNext(1);
subscriber.onNext(2);
subscriber.onNext(3);
OperatorUtils.sleep(2000);
subscriber.onNext(4);
subscriber.onNext(5);
subscriber.onNext(6);
OperatorUtils.sleep(500);
}
})
.subscribeOn(mTestScheduler)
.doOnNext(System.out::println)
.doOnCompleted(()->System.out.println("slkdfsd"));
Observable bufferClosingSelector = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
OperatorUtils.sleep(1000);
subscriber.onNext("close");
OperatorUtils.sleep(1500);
subscriber.onNext("close");
subscriber.onCompleted();
}
})
.subscribeOn(Schedulers.newThread())
.doOnNext(System.out::println);
observable.buffer((Func0<Observable<?>>) () -> bufferClosingSelector)
.subscribe(mList::add);
mTestScheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
System.out.println(mList);
}
/**
* transform the items emitted by an Observable into Observables, then flatten the
* emissions from those into a single Observable
* <p/>根据官方 marble diagrams 进行实现
*
* @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators
* documentation: FlatMap</a>
* @see <a href="http://reactivex.io/documentation/operators/images/flatMap.c.png">faltMap.png</a>
*/
@Test
public void flatMap() {
Observable.just(1, 2, 3)
.flatMap((Func1<Integer, Observable<?>>) num -> Observable.interval(num - 1,
TimeUnit.SECONDS, mTestScheduler)
.take(2)
.map(value -> num + "◇"))
.subscribe(mList::add);
mTestScheduler.advanceTimeBy(100, TimeUnit.SECONDS);
assertEquals(mList, Arrays.asList("1◇", "1◇", "2◇", "3◇", "2◇", "3◇"));
System.out.println(mList);
}
/**
* There is also a concatMap operator, which is like the simpler version of the flatMap
* operator, but it concatenates rather than merges the resulting Observables in order to
* generate its own sequence.
* <p/>根据官方 marble diagrams 进行实现
* <p/>
* concatMap与flatMap的区别在于,concatMap可以保持Observable发送时的顺序,而flatMap可能会交错的
* 发送数据流
*
* @see <a href="http://reactivex.io/documentation/operators/images/concatMap.png">concatMap.png</a>
*/
@Test
public void concatMap() {
Observable.just(1, 2, 3)
.concatMap((Func1<Integer, Observable<?>>) num -> Observable.interval(num - 1,
TimeUnit.SECONDS, mTestScheduler)
.take(2)
.map(value -> num + "◇"))
.subscribe(mList::add);
mTestScheduler.advanceTimeBy(100, TimeUnit.SECONDS);
assertEquals(mList, Arrays.asList("1◇", "1◇", "2◇", "2◇", "3◇", "3◇"));
System.out.println(mList);
}
/**
* It behaves much like flatMap, except that whenever a new item is emitted by the source
* Observable, it will unsubscribe to and stop mirroring the Observable that was generated
* from the previously-emitted item, and begin only mirroring the current one.
* <p/>根据官方 marble diagrams 进行实现
*
* @see <a href="http://reactivex.io/documentation/operators/images/switchMap.png">switchMap.png</a>
*/
@Test
public void switchMap() {
Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
subscriber.onNext(1);
OperatorUtils.sleep(1500);
subscriber.onNext(2);
OperatorUtils.sleep(500);
subscriber.onNext(3);
OperatorUtils.sleep(1500);
subscriber.onCompleted();
}
})
.subscribeOn(mTestScheduler)
.switchMap((Func1<Integer, Observable<?>>) num ->
Observable.interval(0, 1, TimeUnit.SECONDS)
.take(2)
.map(value -> num + "◇")
)
.subscribe(mList::add);
mTestScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
assertEquals(mList, Arrays.asList("1◇", "1◇", "2◇", "3◇", "3◇"));
System.out.println(mList);
}
/**
* divide an Observable into a set of Observables that each emit a different group of items
* from the original Observable, organized by key
*
* @see <a href="http://reactivex.io/documentation/operators/groupby.html">ReactiveX operators
* documentation: GroupBy</a>
*/
@Test
public void groupBy() {
Observable.just(1, 2, 130, 3, 150, 999)
.groupBy(num -> {
// 根据规则产生键
if (num > 100) {
return "big";
}
return "small";
})
.subscribe(groupedObservable -> {
groupedObservable.subscribe(value -> {
//通过getKey()可以获取键值
String key = groupedObservable.getKey();
String result = key + "->" + value;
System.out.println(result);
mList.add(result);
});
});
assertEquals(mList, Arrays.asList("small->1", "small->2", "big->130", "small->3",
"big->150", "big->999"));
}
/**
* transform the items emitted by an Observable by applying a function to each item
*
* @see <a href = "http://rxmarbles.com/#map">RxMarbles map diagrams</a>
* @see <a href="http://reactivex.io/documentation/operators/map.html">ReactiveX operators
* documentation: Map</a>
*/
@Test
public void map() {
Observable.just(1, 2, 3)
.map(integer -> integer * 10)
.subscribe(mList::add);
assertEquals(mList, Arrays.asList(10, 20, 30));
}
/**
* apply a function to each item emitted by an Observable, sequentially, and emit each
* successive value
*
* @see <a href = "http://rxmarbles.com/#scan">RxMarbles scan diagrams</a>
* @see <a href="http://reactivex.io/documentation/operators/scan.html">ReactiveX operators
* documentation: Scan</a>
*/
@Test
public void scan() {
Observable.just(1, 2, 3, 4, 5)
.scan((num1, num2) -> num1 + num2)
.subscribe(mList::add);
assertEquals(mList, Arrays.asList(1, 3, 6, 10, 15));
}
/**
* periodically subdivide items from an Observable into Observable windows and emit
* these windows rather than emitting the items one at a time
*
* @see <a href="http://reactivex.io/documentation/operators/window.html">ReactiveX
* operators documentation: Window</a>
*/
@Test
public void window() {
List<Observable> list = new ArrayList<>();
Observable.just(1, 2, 3, 4, 5, 6)
.window(3)
.toBlocking()
.forEach(list::add);
for (int i = 0; i < 2; i++) {
Observable observable = list.get(i);
System.out.println(i + "->" + observable);
observable.subscribe(System.out::println);
}
}
}