Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableSwitchIfEmpty.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.observable;
15
16 import io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.internal.disposables.SequentialDisposable;
19
20 public final class ObservableSwitchIfEmpty<T> extends AbstractObservableWithUpstream<T, T> {
21 final ObservableSource<? extends T> other;
22 public ObservableSwitchIfEmpty(ObservableSource<T> source, ObservableSource<? extends T> other) {
23 super(source);
24 this.other = other;
25 }
26
27 @Override
28 public void subscribeActual(Observer<? super T> t) {
29 SwitchIfEmptyObserver<T> parent = new SwitchIfEmptyObserver<>(t, other);
30 t.onSubscribe(parent.arbiter);
31 source.subscribe(parent);
32 }
33
34 static final class SwitchIfEmptyObserver<T> implements Observer<T> {
35 final Observer<? super T> downstream;
36 final ObservableSource<? extends T> other;
37 final SequentialDisposable arbiter;
38
39 boolean empty;
40
41 SwitchIfEmptyObserver(Observer<? super T> actual, ObservableSource<? extends T> other) {
42 this.downstream = actual;
43 this.other = other;
44 this.empty = true;
45 this.arbiter = new SequentialDisposable();
46 }
47
48 @Override
49 public void onSubscribe(Disposable d) {
50 arbiter.update(d);
51 }
52
53 @Override
54 public void onNext(T t) {
55 if (empty) {
56 empty = false;
57 }
58 downstream.onNext(t);
59 }
60
61 @Override
62 public void onError(Throwable t) {
63 downstream.onError(t);
64 }
65
66 @Override
67 public void onComplete() {
68 if (empty) {
69 empty = false;
70 other.subscribe(this);
71 } else {
72 downstream.onComplete();
73 }
74 }
75 }
76 }