-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathObservableInternalHelper.java
315 lines (254 loc) · 11.5 KB
/
ObservableInternalHelper.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
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* 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 io.reactivex.internal.operators.observable;
import java.util.List;
import java.util.concurrent.*;
import io.reactivex.*;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.Functions;
import io.reactivex.observables.ConnectableObservable;
/**
* Helper utility class to support Flowable with inner classes.
*/
public enum ObservableInternalHelper {
;
static final class SimpleGenerator<T, S> implements BiFunction<S, Emitter<T>, S> {
final Consumer<Emitter<T>> consumer;
public SimpleGenerator(Consumer<Emitter<T>> consumer) {
this.consumer = consumer;
}
@Override
public S apply(S t1, Emitter<T> t2) throws Exception {
consumer.accept(t2);
return t1;
}
}
public static <T, S> BiFunction<S, Emitter<T>, S> simpleGenerator(Consumer<Emitter<T>> consumer) {
return new SimpleGenerator<T, S>(consumer);
}
static final class SimpleBiGenerator<T, S> implements BiFunction<S, Emitter<T>, S> {
final BiConsumer<S, Emitter<T>> consumer;
public SimpleBiGenerator(BiConsumer<S, Emitter<T>> consumer) {
this.consumer = consumer;
}
@Override
public S apply(S t1, Emitter<T> t2) throws Exception {
consumer.accept(t1, t2);
return t1;
}
}
public static <T, S> BiFunction<S, Emitter<T>, S> simpleBiGenerator(BiConsumer<S, Emitter<T>> consumer) {
return new SimpleBiGenerator<T, S>(consumer);
}
static final class ItemDelayFunction<T, U> implements Function<T, ObservableSource<T>> {
final Function<? super T, ? extends ObservableSource<U>> itemDelay;
public ItemDelayFunction(Function<? super T, ? extends ObservableSource<U>> itemDelay) {
this.itemDelay = itemDelay;
}
@Override
public ObservableSource<T> apply(final T v) throws Exception {
return new ObservableTake<U>(itemDelay.apply(v), 1).map(Functions.justFunction(v)).defaultIfEmpty(v);
}
}
public static <T, U> Function<T, ObservableSource<T>> itemDelay(final Function<? super T, ? extends ObservableSource<U>> itemDelay) {
return new ItemDelayFunction<T, U>(itemDelay);
}
static final class ObserverOnNext<T> implements Consumer<T> {
final Observer<T> observer;
public ObserverOnNext(Observer<T> observer) {
this.observer = observer;
}
@Override
public void accept(T v) throws Exception {
observer.onNext(v);
}
}
static final class ObserverOnError<T> implements Consumer<Throwable> {
final Observer<T> observer;
public ObserverOnError(Observer<T> observer) {
this.observer = observer;
}
@Override
public void accept(Throwable v) throws Exception {
observer.onError(v);
}
}
static final class ObserverOnComplete<T> implements Action {
final Observer<T> observer;
public ObserverOnComplete(Observer<T> observer) {
this.observer = observer;
}
@Override
public void run() throws Exception {
observer.onComplete();
}
}
public static <T> Consumer<T> observerOnNext(Observer<T> observer) {
return new ObserverOnNext<T>(observer);
}
public static <T> Consumer<Throwable> observerOnError(Observer<T> observer) {
return new ObserverOnError<T>(observer);
}
public static <T> Action observerOnComplete(Observer<T> observer) {
return new ObserverOnComplete<T>(observer);
}
static final class FlatMapWithCombinerInner<U, R, T> implements Function<U, R> {
private final BiFunction<? super T, ? super U, ? extends R> combiner;
private final T t;
FlatMapWithCombinerInner(BiFunction<? super T, ? super U, ? extends R> combiner, T t) {
this.combiner = combiner;
this.t = t;
}
@Override
public R apply(U w) throws Exception {
return combiner.apply(t, w);
}
}
static final class FlatMapWithCombinerOuter<T, R, U> implements Function<T, ObservableSource<R>> {
private final BiFunction<? super T, ? super U, ? extends R> combiner;
private final Function<? super T, ? extends ObservableSource<? extends U>> mapper;
FlatMapWithCombinerOuter(BiFunction<? super T, ? super U, ? extends R> combiner,
Function<? super T, ? extends ObservableSource<? extends U>> mapper) {
this.combiner = combiner;
this.mapper = mapper;
}
@Override
public ObservableSource<R> apply(final T t) throws Exception {
@SuppressWarnings("unchecked")
ObservableSource<U> u = (ObservableSource<U>)mapper.apply(t);
return new ObservableMap<U, R>(u, new FlatMapWithCombinerInner<U, R, T>(combiner, t));
}
}
public static <T, U, R> Function<T, ObservableSource<R>> flatMapWithCombiner(
final Function<? super T, ? extends ObservableSource<? extends U>> mapper,
final BiFunction<? super T, ? super U, ? extends R> combiner) {
return new FlatMapWithCombinerOuter<T, R, U>(combiner, mapper);
}
static final class FlatMapIntoIterable<T, U> implements Function<T, ObservableSource<U>> {
private final Function<? super T, ? extends Iterable<? extends U>> mapper;
FlatMapIntoIterable(Function<? super T, ? extends Iterable<? extends U>> mapper) {
this.mapper = mapper;
}
@Override
public ObservableSource<U> apply(T t) throws Exception {
return new ObservableFromIterable<U>(mapper.apply(t));
}
}
public static <T, U> Function<T, ObservableSource<U>> flatMapIntoIterable(final Function<? super T, ? extends Iterable<? extends U>> mapper) {
return new FlatMapIntoIterable<T, U>(mapper);
}
enum MapToInt implements Function<Object, Object>{
INSTANCE;
@Override
public Object apply(Object t) throws Exception {
return 0;
}
}
static final class RepeatWhenOuterHandler
implements Function<Observable<Notification<Object>>, ObservableSource<?>> {
private final Function<? super Observable<Object>, ? extends ObservableSource<?>> handler;
RepeatWhenOuterHandler(Function<? super Observable<Object>, ? extends ObservableSource<?>> handler) {
this.handler = handler;
}
@Override
public ObservableSource<?> apply(Observable<Notification<Object>> no) throws Exception {
return handler.apply(no.map(MapToInt.INSTANCE));
}
}
public static Function<Observable<Notification<Object>>, ObservableSource<?>> repeatWhenHandler(final Function<? super Observable<Object>, ? extends ObservableSource<?>> handler) {
return new RepeatWhenOuterHandler(handler);
}
public static <T> Callable<ConnectableObservable<T>> replayCallable(final Observable<T> parent) {
return new Callable<ConnectableObservable<T>>() {
@Override
public ConnectableObservable<T> call() {
return parent.replay();
}
};
}
public static <T> Callable<ConnectableObservable<T>> replayCallable(final Observable<T> parent, final int bufferSize) {
return new Callable<ConnectableObservable<T>>() {
@Override
public ConnectableObservable<T> call() {
return parent.replay(bufferSize);
}
};
}
public static <T> Callable<ConnectableObservable<T>> replayCallable(final Observable<T> parent, final int bufferSize, final long time, final TimeUnit unit, final Scheduler scheduler) {
return new Callable<ConnectableObservable<T>>() {
@Override
public ConnectableObservable<T> call() {
return parent.replay(bufferSize, time, unit, scheduler);
}
};
}
public static <T> Callable<ConnectableObservable<T>> replayCallable(final Observable<T> parent, final long time, final TimeUnit unit, final Scheduler scheduler) {
return new Callable<ConnectableObservable<T>>() {
@Override
public ConnectableObservable<T> call() {
return parent.replay(time, unit, scheduler);
}
};
}
public static <T, R> Function<Observable<T>, ObservableSource<R>> replayFunction(final Function<? super Observable<T>, ? extends ObservableSource<R>> selector, final Scheduler scheduler) {
return new Function<Observable<T>, ObservableSource<R>>() {
@Override
public ObservableSource<R> apply(Observable<T> t) throws Exception {
return Observable.wrap(selector.apply(t)).observeOn(scheduler);
}
};
}
enum ErrorMapperFilter implements Function<Notification<Object>, Throwable>, Predicate<Notification<Object>> {
INSTANCE;
@Override
public Throwable apply(Notification<Object> t) throws Exception {
return t.getError();
}
@Override
public boolean test(Notification<Object> t) throws Exception {
return t.isOnError();
}
}
static final class RetryWhenInner
implements Function<Observable<Notification<Object>>, ObservableSource<?>> {
private final Function<? super Observable<? extends Throwable>, ? extends ObservableSource<?>> handler;
RetryWhenInner(
Function<? super Observable<? extends Throwable>, ? extends ObservableSource<?>> handler) {
this.handler = handler;
}
@Override
public ObservableSource<?> apply(Observable<Notification<Object>> no) throws Exception {
Observable<Throwable> map = no
.takeWhile(ErrorMapperFilter.INSTANCE)
.map(ErrorMapperFilter.INSTANCE);
return handler.apply(map);
}
}
public static <T> Function<Observable<Notification<Object>>, ObservableSource<?>> retryWhenHandler(final Function<? super Observable<? extends Throwable>, ? extends ObservableSource<?>> handler) {
return new RetryWhenInner(handler);
}
static final class ZipIterableFunction<T, R>
implements Function<List<ObservableSource<? extends T>>, ObservableSource<? extends R>> {
private final Function<? super T[], ? extends R> zipper;
ZipIterableFunction(Function<? super T[], ? extends R> zipper) {
this.zipper = zipper;
}
@Override
public ObservableSource<? extends R> apply(List<ObservableSource<? extends T>> list) {
return Observable.zipIterable(list, zipper, false, Flowable.bufferSize());
}
}
public static <T, R> Function<List<ObservableSource<? extends T>>, ObservableSource<? extends R>> zipIterable(final Function<? super T[], ? extends R> zipper) {
return new ZipIterableFunction<T, R>(zipper);
}
}