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