Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeSwitchIfEmpty.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.maybe;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21
22 /**
23 * Subscribes to the other source if the main source is empty.
24 *
25 * @param <T> the value type
26 */
27 public final class MaybeSwitchIfEmpty<T> extends AbstractMaybeWithUpstream<T, T> {
28
29 final MaybeSource<? extends T> other;
30
31 public MaybeSwitchIfEmpty(MaybeSource<T> source, MaybeSource<? extends T> other) {
32 super(source);
33 this.other = other;
34 }
35
36 @Override
37 protected void subscribeActual(MaybeObserver<? super T> observer) {
38 source.subscribe(new SwitchIfEmptyMaybeObserver<>(observer, other));
39 }
40
41 static final class SwitchIfEmptyMaybeObserver<T>
42 extends AtomicReference<Disposable>
43 implements MaybeObserver<T>, Disposable {
44
45 private static final long serialVersionUID = -2223459372976438024L;
46
47 final MaybeObserver<? super T> downstream;
48
49 final MaybeSource<? extends T> other;
50
51 SwitchIfEmptyMaybeObserver(MaybeObserver<? super T> actual, MaybeSource<? extends T> other) {
52 this.downstream = actual;
53 this.other = other;
54 }
55
56 @Override
57 public void dispose() {
58 DisposableHelper.dispose(this);
59 }
60
61 @Override
62 public boolean isDisposed() {
63 return DisposableHelper.isDisposed(get());
64 }
65
66 @Override
67 public void onSubscribe(Disposable d) {
68 if (DisposableHelper.setOnce(this, d)) {
69 downstream.onSubscribe(this);
70 }
71 }
72
73 @Override
74 public void onSuccess(T value) {
75 downstream.onSuccess(value);
76 }
77
78 @Override
79 public void onError(Throwable e) {
80 downstream.onError(e);
81 }
82
83 @Override
84 public void onComplete() {
85 Disposable d = get();
86 if (d != DisposableHelper.DISPOSED) {
87 if (compareAndSet(d, null)) {
88 other.subscribe(new OtherMaybeObserver<T>(downstream, this));
89 }
90 }
91 }
92
93 static final class OtherMaybeObserver<T> implements MaybeObserver<T> {
94
95 final MaybeObserver<? super T> downstream;
96
97 final AtomicReference<Disposable> parent;
98 OtherMaybeObserver(MaybeObserver<? super T> actual, AtomicReference<Disposable> parent) {
99 this.downstream = actual;
100 this.parent = parent;
101 }
102
103 @Override
104 public void onSubscribe(Disposable d) {
105 DisposableHelper.setOnce(parent, d);
106 }
107
108 @Override
109 public void onSuccess(T value) {
110 downstream.onSuccess(value);
111 }
112
113 @Override
114 public void onError(Throwable e) {
115 downstream.onError(e);
116 }
117
118 @Override
119 public void onComplete() {
120 downstream.onComplete();
121 }
122 }
123
124 }
125 }