-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathCh9_09.java
More file actions
54 lines (49 loc) · 1.91 KB
/
Copy pathCh9_09.java
File metadata and controls
54 lines (49 loc) · 1.91 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
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableOperator;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.subscribers.DisposableSubscriber;
import org.reactivestreams.Subscriber;
public class Ch9_09 {
public static void main(String[] args) {
Flowable.range(1, 5)
.lift(doOnEmpty(() ->
System.out.println("Operation 1 Empty!")))
.subscribe(v -> System.out.println("Operation 1: " + v));
Flowable.<Integer>empty()
.lift(doOnEmpty(() ->
System.out.println("Operation 2 Empty!")))
.subscribe(v -> System.out.println("Operation 2: " + v));
}
private static <T> FlowableOperator<T, T> doOnEmpty(Action action) {
return new FlowableOperator<T, T>() {
@Override
public Subscriber<? super T> apply(Subscriber<? super
T> subscriber) throws Exception {
return new DisposableSubscriber<T>() {
boolean isEmpty = true;
@Override
public void onNext(T value) {
isEmpty = false;
subscriber.onNext(value);
}
@Override
public void onError(Throwable t) {
subscriber.onError(t);
}
@Override
public void onComplete() {
if (isEmpty) {
try {
action.run();
} catch (Throwable e) {
onError(e);
return;
}
}
subscriber.onComplete();
}
};
}
};
}
}