Debian Codegraph @

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