Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableMergeWithSingle.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 Single by emitting the items of the Observable and the success
27 * value of the Single and waiting until both the Observable and Single 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 ObservableMergeWithSingle<T> extends AbstractObservableWithUpstream<T, T> {
33
34 final SingleSource<? extends T> other;
35
36 public ObservableMergeWithSingle(Observable<T> source, SingleSource<? 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 SimplePlainQueue<T> getOrCreateQueue() {
158 SimplePlainQueue<T> q = queue;
159 if (q == null) {
160 q = new SpscLinkedArrayQueue<>(bufferSize());
161 queue = q;
162 }
163 return q;
164 }
165
166 void drain() {
167 if (getAndIncrement() == 0) {
168 drainLoop();
169 }
170 }
171
172 void drainLoop() {
173 Observer<? super T> actual = this.downstream;
174 int missed = 1;
175 for (;;) {
176
177 for (;;) {
178 if (disposed) {
179 singleItem = null;
180 queue = null;
181 return;
182 }
183
184 if (errors.get() != null) {
185 singleItem = null;
186 queue = null;
187 errors.tryTerminateConsumer(actual);
188 return;
189 }
190
191 int os = otherState;
192 if (os == OTHER_STATE_HAS_VALUE) {
193 T v = singleItem;
194 singleItem = null;
195 otherState = OTHER_STATE_CONSUMED_OR_EMPTY;
196 os = OTHER_STATE_CONSUMED_OR_EMPTY;
197 actual.onNext(v);
198 }
199
200 boolean d = mainDone;
201 SimplePlainQueue<T> q = queue;
202 T v = q != null ? q.poll() : null;
203 boolean empty = v == null;
204
205 if (d && empty && os == OTHER_STATE_CONSUMED_OR_EMPTY) {
206 queue = null;
207 actual.onComplete();
208 return;
209 }
210
211 if (empty) {
212 break;
213 }
214
215 actual.onNext(v);
216 }
217
218 missed = addAndGet(-missed);
219 if (missed == 0) {
220 break;
221 }
222 }
223 }
224
225 static final class OtherObserver<T> extends AtomicReference<Disposable>
226 implements SingleObserver<T> {
227
228 private static final long serialVersionUID = -2935427570954647017L;
229
230 final MergeWithObserver<T> parent;
231
232 OtherObserver(MergeWithObserver<T> parent) {
233 this.parent = parent;
234 }
235
236 @Override
237 public void onSubscribe(Disposable d) {
238 DisposableHelper.setOnce(this, d);
239 }
240
241 @Override
242 public void onSuccess(T t) {
243 parent.otherSuccess(t);
244 }
245
246 @Override
247 public void onError(Throwable e) {
248 parent.otherError(e);
249 }
250
251 }
252 }
253 }