forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservableConversionTest.java
More file actions
250 lines (216 loc) · 9.41 KB
/
ObservableConversionTest.java
File metadata and controls
250 lines (216 loc) · 9.41 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
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package rx;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.*;
import org.junit.Test;
import rx.Observable.OnSubscribe;
import rx.Observable.Operator;
import rx.exceptions.OnErrorNotImplementedException;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.internal.operators.OnSubscribeFilter;
import rx.internal.operators.OnSubscribeMap;
import rx.observers.TestSubscriber;
import rx.schedulers.Schedulers;
public class ObservableConversionTest {
public static class Cylon {}
public static class Jail {
Object cylon;
Jail(Object cylon) {
this.cylon = cylon;
}
}
public static class CylonDetectorObservable<T> {
protected OnSubscribe<T> onSubscribe;
public static <T> CylonDetectorObservable<T> create(OnSubscribe<T> onSubscribe) {
return new CylonDetectorObservable<T>(onSubscribe);
}
protected CylonDetectorObservable(OnSubscribe<T> onSubscribe) {
this.onSubscribe = onSubscribe;
}
public void subscribe(Subscriber<T> subscriber) {
onSubscribe.call(subscriber);
}
public <R> CylonDetectorObservable<R> lift(Operator<? extends R, ? super T> operator) {
return x(new RobotConversionFunc<T, R>(operator));
}
public <R, O> O x(Func1<OnSubscribe<T>, O> operator) {
return operator.call(onSubscribe);
}
public <R> CylonDetectorObservable<? extends R> compose(Func1<CylonDetectorObservable<? super T>, CylonDetectorObservable<? extends R>> transformer) {
return transformer.call(this);
}
public final CylonDetectorObservable<T> beep(Func1<? super T, Boolean> predicate) {
return create(new OnSubscribeFilter<T>(Observable.create(onSubscribe), predicate));
}
public final <R> CylonDetectorObservable<R> boop(Func1<? super T, ? extends R> func) {
return create(new OnSubscribeMap<T, R>(Observable.create(onSubscribe), func));
}
public CylonDetectorObservable<String> DESTROY() {
return boop(new Func1<T, String>() {
@Override
public String call(T t) {
Object cylon = ((Jail) t).cylon;
throwOutTheAirlock(cylon);
if (t instanceof Jail) {
String name = cylon.toString();
return "Cylon '" + name + "' has been destroyed";
}
else {
return "Cylon 'anonymous' has been destroyed";
}
}});
}
private static void throwOutTheAirlock(Object cylon) {
// ...
}
}
public static class RobotConversionFunc<T, R> implements Func1<OnSubscribe<T>, CylonDetectorObservable<R>> {
private Operator<? extends R, ? super T> operator;
public RobotConversionFunc(Operator<? extends R, ? super T> operator) {
this.operator = operator;
}
@Override
public CylonDetectorObservable<R> call(final OnSubscribe<T> onSubscribe) {
return CylonDetectorObservable.create(new OnSubscribe<R>() {
@Override
public void call(Subscriber<? super R> o) {
try {
Subscriber<? super T> st = operator.call(o);
try {
st.onStart();
onSubscribe.call(st);
} catch (OnErrorNotImplementedException e) {
throw e;
} catch (Throwable e) {
st.onError(e);
}
} catch (OnErrorNotImplementedException e) {
throw e;
} catch (Throwable e) {
o.onError(e);
}
}});
}
}
public static class ConvertToCylonDetector<T> implements Func1<OnSubscribe<T>, CylonDetectorObservable<T>> {
@Override
public CylonDetectorObservable<T> call(final OnSubscribe<T> onSubscribe) {
return CylonDetectorObservable.create(onSubscribe);
}
}
public static class ConvertToObservable<T> implements Func1<OnSubscribe<T>, Observable<T>> {
@Override
public Observable<T> call(final OnSubscribe<T> onSubscribe) {
return Observable.create(onSubscribe);
}
}
@Test
public void testConversionBetweenObservableClasses() {
final TestSubscriber<String> subscriber = new TestSubscriber<String>(new Subscriber<String>(){
@Override
public void onCompleted() {
System.out.println("Complete");
}
@Override
public void onError(Throwable e) {
System.out.println("error: " + e.getMessage());
e.printStackTrace();
}
@Override
public void onNext(String t) {
System.out.println(t);
}});
List<Object> crewOfBattlestarGalactica = Arrays.asList(new Object[] {"William Adama", "Laura Roslin", "Lee Adama", new Cylon()});
Observable.from(crewOfBattlestarGalactica)
.extend(new ConvertToCylonDetector<Object>())
.beep(new Func1<Object, Boolean>(){
@Override
public Boolean call(Object t) {
return t instanceof Cylon;
}})
.boop(new Func1<Object, Object>() {
@Override
public Jail call(Object cylon) {
return new Jail(cylon);
}})
.DESTROY()
.x(new ConvertToObservable<String>())
.reduce("Cylon Detector finished. Report:\n", new Func2<String, String, String>() {
@Override
public String call(String a, String n) {
return a + n + "\n";
}})
.subscribe(subscriber);
subscriber.assertNoErrors();
subscriber.assertCompleted();
}
@Test
public void testConvertToConcurrentQueue() {
final AtomicReference<Throwable> thrown = new AtomicReference<Throwable>(null);
final AtomicBoolean isFinished = new AtomicBoolean(false);
ConcurrentLinkedQueue<? extends Integer> queue = Observable.range(0,5)
.flatMap(new Func1<Integer, Observable<Integer>>(){
@Override
public Observable<Integer> call(final Integer i) {
return Observable.range(0, 5)
.observeOn(Schedulers.io())
.map(new Func1<Integer, Integer>(){
@Override
public Integer call(Integer k) {
try {
Thread.sleep(System.currentTimeMillis() % 100);
} catch (InterruptedException e) {
e.printStackTrace();
}
return i + k;
}});
}})
.extend(new Func1<OnSubscribe<Integer>, ConcurrentLinkedQueue<Integer>>() {
@Override
public ConcurrentLinkedQueue<Integer> call(OnSubscribe<Integer> onSubscribe) {
final ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>();
onSubscribe.call(new Subscriber<Integer>(){
@Override
public void onCompleted() {
isFinished.set(true);
}
@Override
public void onError(Throwable e) {
thrown.set(e);
}
@Override
public void onNext(Integer t) {
q.add(t);
}});
return q;
}});
int x = 0;
while(!isFinished.get()) {
Integer i = queue.poll();
if (i != null) {
x++;
System.out.println(x + " item: " + i);
}
}
assertEquals(null, thrown.get());
}
}