Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableMergeWithMaybe.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 java.util.concurrent.atomic.*;
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.util.AtomicThrowable;
22 import io.reactivex.rxjava3.operators.SimplePlainQueue;
23 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
24
25 /**
26 * Merges an Observable and a Maybe by emitting the items of the Observable and the success
27 * value of the Maybe and waiting until both the Observable and Maybe terminate normally.
28 * <p>History: 2.1.10 - experimental
29 * @param <T> the element type of the Observable
30 * @since 2.2
31 */
32 public final class ObservableMergeWithMaybe<T> extends AbstractObservableWithUpstream<T, T> {
33
34 final MaybeSource<? extends T> other;
35
36 public ObservableMergeWithMaybe(Observable<T> source, MaybeSource<? extends T> other) {
37 super(source);
38 this.other = other;
39 }
40
41 @Override
42 protected void subscribeActual(Observer<? super T> observer) {
43 MergeWithObserver<T> parent = new MergeWithObserver<>(observer);
44 observer.onSubscribe(parent);
45 source.subscribe(parent);
46 other.subscribe(parent.otherObserver);
47 }
48
49 static final class MergeWithObserver<T> extends AtomicInteger
50 implements Observer<T>, Disposable {
51
52 private static final long serialVersionUID = -4592979584110982903L;
53
54 final Observer<? super T> downstream;
55
56 final AtomicReference<Disposable> mainDisposable;
57
58 final OtherObserver<T> otherObserver;
59
60 final AtomicThrowable errors;
61
62 volatile SimplePlainQueue<T> queue;
63
64 T singleItem;
65
66 volatile boolean disposed;
67
68 volatile boolean mainDone;
69
70 volatile int otherState;
71
72 static final int OTHER_STATE_HAS_VALUE = 1;
73
74 static final int OTHER_STATE_CONSUMED_OR_EMPTY = 2;
75
76 MergeWithObserver(Observer<? super T> downstream) {
77 this.downstream = downstream;
78 this.mainDisposable = new AtomicReference<>();
79 this.otherObserver = new OtherObserver<>(this);
80 this.errors = new AtomicThrowable();
81 }
82
83 @Override
84 public void onSubscribe(Disposable d) {
85 DisposableHelper.setOnce(mainDisposable, d);
86 }
87
88 @Override
89 public void onNext(T t) {
90 if (compareAndSet(0, 1)) {
91 downstream.onNext(t);
92 if (decrementAndGet() == 0) {
93 return;
94 }
95 } else {
96 SimplePlainQueue<T> q = getOrCreateQueue();
97 q.offer(t);
98 if (getAndIncrement() != 0) {
99 return;
100 }
101 }
102 drainLoop();
103 }
104
105 @Override
106 public void onError(Throwable ex) {
107 if (errors.tryAddThrowableOrReport(ex)) {
108 DisposableHelper.dispose(otherObserver);
109 drain();
110 }
111 }
112
113 @Override
114 public void onComplete() {
115 mainDone = true;
116 drain();
117 }
118
119 @Override
120 public boolean isDisposed() {
121 return DisposableHelper.isDisposed(mainDisposable.get());
122 }
123
124 @Override
125 public void dispose() {
126 disposed = true;
127 DisposableHelper.dispose(mainDisposable);
128 DisposableHelper.dispose(otherObserver);
129 errors.tryTerminateAndReport();
130 if (getAndIncrement() == 0) {
131 queue = null;
132 singleItem = null;
133 }
134 }
135
136 void otherSuccess(T value) {
137 if (compareAndSet(0, 1)) {
138 downstream.onNext(value);
139 otherState = OTHER_STATE_CONSUMED_OR_EMPTY;
140 } else {
141 singleItem = value;
142 otherState = OTHER_STATE_HAS_VALUE;
143 if (getAndIncrement() != 0) {
144 return;
145 }
146 }
147 drainLoop();
148 }
149
150 void otherError(Throwable ex) {
151 if (errors.tryAddThrowableOrReport(ex)) {
152 DisposableHelper.dispose(mainDisposable);
153 drain();
154 }
155 }
156
157 void otherComplete() {
158 otherState = OTHER_STATE_CONSUMED_OR_EMPTY;
159 drain();
160 }
161
162 SimplePlainQueue<T> getOrCreateQueue() {
163 SimplePlainQueue<T> q = queue;
164 if (q == null) {
165 q = new SpscLinkedArrayQueue<>(bufferSize());
166 queue = q;
167 }
168 return q;
169 }
170
171 void drain() {
172 if (getAndIncrement() == 0) {
173 drainLoop();
174 }
175 }
176
177 void drainLoop() {
178 Observer<? super T> actual = this.downstream;
179 int missed = 1;
180 for (;;) {
181
182 for (;;) {
183 if (disposed) {
184 singleItem = null;
185 queue = null;
186 return;
187 }
188
189 if (errors.get() != null) {
190 singleItem = null;
191 queue = null;
192 errors.tryTerminateConsumer(actual);
193 return;
194 }
195
196 int os = otherState;
197 if (os == OTHER_STATE_HAS_VALUE) {
198 T v = singleItem;
199 singleItem = null;
200 otherState = OTHER_STATE_CONSUMED_OR_EMPTY;
201 os = OTHER_STATE_CONSUMED_OR_EMPTY;
202 actual.onNext(v);
203 }
204
205 boolean d = mainDone;
206 SimplePlainQueue<T> q = queue;
207 T v = q != null ? q.poll() : null;
208 boolean empty = v == null;
209
210 if (d && empty && os == OTHER_STATE_CONSUMED_OR_EMPTY) {
211 queue = null;
212 actual.onComplete();
213 return;
214 }
215
216 if (empty) {
217 break;
218 }
219
220 actual.onNext(v);
221 }
222
223 missed = addAndGet(-missed);
224 if (missed == 0) {
225 break;
226 }
227 }
228 }
229
230 static final class OtherObserver<T> extends AtomicReference<Disposable>
231 implements MaybeObserver<T> {
232
233 private static final long serialVersionUID = -2935427570954647017L;
234
235 final MergeWithObserver<T> parent;
236
237 OtherObserver(MergeWithObserver<T> parent) {
238 this.parent = parent;
239 }
240
241 @Override
242 public void onSubscribe(Disposable d) {
243 DisposableHelper.setOnce(this, d);
244 }
245
246 @Override
247 public void onSuccess(T t) {
248 parent.otherSuccess(t);
249 }
250
251 @Override
252 public void onError(Throwable e) {
253 parent.otherError(e);
254 }
255
256 @Override
257 public void onComplete() {
258 parent.otherComplete();
259 }
260 }
261 }
262 }