| 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.flowable; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 23 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 24 |
import io.reactivex.rxjava3.internal.util.*; |
| 25 |
import io.reactivex.rxjava3.operators.SimplePlainQueue; |
| 26 |
import io.reactivex.rxjava3.operators.SpscArrayQueue; |
| 27 |
|
| 28 |
/** |
| 29 |
* Merges an Observable and a Maybe by emitting the items of the Observable and the success |
| 30 |
* value of the Maybe and waiting until both the Observable and Maybe terminate normally. |
| 31 |
* <p>History: 2.1.10 - experimental |
| 32 |
* @param <T> the element type of the Observable |
| 33 |
* @since 2.2 |
| 34 |
*/ |
| 35 |
public final class FlowableMergeWithMaybe<T> extends AbstractFlowableWithUpstream<T, T> { |
| 36 |
|
| 37 |
final MaybeSource<? extends T> other; |
| 38 |
|
| 39 |
public FlowableMergeWithMaybe(Flowable<T> source, MaybeSource<? extends T> other) { |
| 40 |
super(source); |
| 41 |
this.other = other; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
protected void subscribeActual(Subscriber<? super T> subscriber) { |
| 46 |
MergeWithObserver<T> parent = new MergeWithObserver<>(subscriber); |
| 47 |
subscriber.onSubscribe(parent); |
| 48 |
source.subscribe(parent); |
| 49 |
other.subscribe(parent.otherObserver); |
| 50 |
} |
| 51 |
|
| 52 |
static final class MergeWithObserver<T> extends AtomicInteger |
| 53 |
implements FlowableSubscriber<T>, Subscription { |
| 54 |
|
| 55 |
private static final long serialVersionUID = -4592979584110982903L; |
| 56 |
|
| 57 |
final Subscriber<? super T> downstream; |
| 58 |
|
| 59 |
final AtomicReference<Subscription> mainSubscription; |
| 60 |
|
| 61 |
final OtherObserver<T> otherObserver; |
| 62 |
|
| 63 |
final AtomicThrowable errors; |
| 64 |
|
| 65 |
final AtomicLong requested; |
| 66 |
|
| 67 |
final int prefetch; |
| 68 |
|
| 69 |
final int limit; |
| 70 |
|
| 71 |
volatile SimplePlainQueue<T> queue; |
| 72 |
|
| 73 |
T singleItem; |
| 74 |
|
| 75 |
volatile boolean cancelled; |
| 76 |
|
| 77 |
volatile boolean mainDone; |
| 78 |
|
| 79 |
volatile int otherState; |
| 80 |
|
| 81 |
long emitted; |
| 82 |
|
| 83 |
int consumed; |
| 84 |
|
| 85 |
static final int OTHER_STATE_HAS_VALUE = 1; |
| 86 |
|
| 87 |
static final int OTHER_STATE_CONSUMED_OR_EMPTY = 2; |
| 88 |
|
| 89 |
MergeWithObserver(Subscriber<? super T> downstream) { |
| 90 |
this.downstream = downstream; |
| 91 |
this.mainSubscription = new AtomicReference<>(); |
| 92 |
this.otherObserver = new OtherObserver<>(this); |
| 93 |
this.errors = new AtomicThrowable(); |
| 94 |
this.requested = new AtomicLong(); |
| 95 |
this.prefetch = bufferSize(); |
| 96 |
this.limit = prefetch - (prefetch >> 2); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void onSubscribe(Subscription s) { |
| 101 |
SubscriptionHelper.setOnce(mainSubscription, s, prefetch); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public void onNext(T t) { |
| 106 |
if (compareAndSet(0, 1)) { |
| 107 |
long e = emitted; |
| 108 |
if (requested.get() != e) { |
| 109 |
SimplePlainQueue<T> q = queue; |
| 110 |
if (q == null || q.isEmpty()) { |
| 111 |
|
| 112 |
emitted = e + 1; |
| 113 |
downstream.onNext(t); |
| 114 |
|
| 115 |
int c = consumed + 1; |
| 116 |
if (c == limit) { |
| 117 |
consumed = 0; |
| 118 |
mainSubscription.get().request(c); |
| 119 |
} else { |
| 120 |
consumed = c; |
| 121 |
} |
| 122 |
} else { |
| 123 |
q.offer(t); |
| 124 |
} |
| 125 |
} else { |
| 126 |
SimplePlainQueue<T> q = getOrCreateQueue(); |
| 127 |
q.offer(t); |
| 128 |
} |
| 129 |
if (decrementAndGet() == 0) { |
| 130 |
return; |
| 131 |
} |
| 132 |
} else { |
| 133 |
SimplePlainQueue<T> q = getOrCreateQueue(); |
| 134 |
q.offer(t); |
| 135 |
if (getAndIncrement() != 0) { |
| 136 |
return; |
| 137 |
} |
| 138 |
} |
| 139 |
drainLoop(); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void onError(Throwable ex) { |
| 144 |
if (errors.tryAddThrowableOrReport(ex)) { |
| 145 |
DisposableHelper.dispose(otherObserver); |
| 146 |
drain(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void onComplete() { |
| 152 |
mainDone = true; |
| 153 |
drain(); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void request(long n) { |
| 158 |
BackpressureHelper.add(requested, n); |
| 159 |
drain(); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public void cancel() { |
| 164 |
cancelled = true; |
| 165 |
SubscriptionHelper.cancel(mainSubscription); |
| 166 |
DisposableHelper.dispose(otherObserver); |
| 167 |
errors.tryTerminateAndReport(); |
| 168 |
if (getAndIncrement() == 0) { |
| 169 |
queue = null; |
| 170 |
singleItem = null; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void otherSuccess(T value) { |
| 175 |
if (compareAndSet(0, 1)) { |
| 176 |
long e = emitted; |
| 177 |
if (requested.get() != e) { |
| 178 |
|
| 179 |
emitted = e + 1; |
| 180 |
downstream.onNext(value); |
| 181 |
otherState = OTHER_STATE_CONSUMED_OR_EMPTY; |
| 182 |
} else { |
| 183 |
singleItem = value; |
| 184 |
otherState = OTHER_STATE_HAS_VALUE; |
| 185 |
if (decrementAndGet() == 0) { |
| 186 |
return; |
| 187 |
} |
| 188 |
} |
| 189 |
} else { |
| 190 |
singleItem = value; |
| 191 |
otherState = OTHER_STATE_HAS_VALUE; |
| 192 |
if (getAndIncrement() != 0) { |
| 193 |
return; |
| 194 |
} |
| 195 |
} |
| 196 |
drainLoop(); |
| 197 |
} |
| 198 |
|
| 199 |
void otherError(Throwable ex) { |
| 200 |
if (errors.tryAddThrowableOrReport(ex)) { |
| 201 |
SubscriptionHelper.cancel(mainSubscription); |
| 202 |
drain(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void otherComplete() { |
| 207 |
otherState = OTHER_STATE_CONSUMED_OR_EMPTY; |
| 208 |
drain(); |
| 209 |
} |
| 210 |
|
| 211 |
SimplePlainQueue<T> getOrCreateQueue() { |
| 212 |
SimplePlainQueue<T> q = queue; |
| 213 |
if (q == null) { |
| 214 |
q = new SpscArrayQueue<>(bufferSize()); |
| 215 |
queue = q; |
| 216 |
} |
| 217 |
return q; |
| 218 |
} |
| 219 |
|
| 220 |
void drain() { |
| 221 |
if (getAndIncrement() == 0) { |
| 222 |
drainLoop(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
void drainLoop() { |
| 227 |
Subscriber<? super T> actual = this.downstream; |
| 228 |
int missed = 1; |
| 229 |
long e = emitted; |
| 230 |
int c = consumed; |
| 231 |
int lim = limit; |
| 232 |
for (;;) { |
| 233 |
|
| 234 |
long r = requested.get(); |
| 235 |
|
| 236 |
while (e != r) { |
| 237 |
if (cancelled) { |
| 238 |
singleItem = null; |
| 239 |
queue = null; |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
if (errors.get() != null) { |
| 244 |
singleItem = null; |
| 245 |
queue = null; |
| 246 |
errors.tryTerminateConsumer(downstream); |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
int os = otherState; |
| 251 |
if (os == OTHER_STATE_HAS_VALUE) { |
| 252 |
T v = singleItem; |
| 253 |
singleItem = null; |
| 254 |
otherState = OTHER_STATE_CONSUMED_OR_EMPTY; |
| 255 |
os = OTHER_STATE_CONSUMED_OR_EMPTY; |
| 256 |
actual.onNext(v); |
| 257 |
|
| 258 |
e++; |
| 259 |
continue; |
| 260 |
} |
| 261 |
|
| 262 |
boolean d = mainDone; |
| 263 |
SimplePlainQueue<T> q = queue; |
| 264 |
T v = q != null ? q.poll() : null; |
| 265 |
boolean empty = v == null; |
| 266 |
|
| 267 |
if (d && empty && os == OTHER_STATE_CONSUMED_OR_EMPTY) { |
| 268 |
queue = null; |
| 269 |
actual.onComplete(); |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
if (empty) { |
| 274 |
break; |
| 275 |
} |
| 276 |
|
| 277 |
actual.onNext(v); |
| 278 |
|
| 279 |
e++; |
| 280 |
|
| 281 |
if (++c == lim) { |
| 282 |
c = 0; |
| 283 |
mainSubscription.get().request(lim); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if (e == r) { |
| 288 |
if (cancelled) { |
| 289 |
singleItem = null; |
| 290 |
queue = null; |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
if (errors.get() != null) { |
| 295 |
singleItem = null; |
| 296 |
queue = null; |
| 297 |
errors.tryTerminateConsumer(downstream); |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
boolean d = mainDone; |
| 302 |
SimplePlainQueue<T> q = queue; |
| 303 |
boolean empty = q == null || q.isEmpty(); |
| 304 |
|
| 305 |
if (d && empty && otherState == 2) { |
| 306 |
queue = null; |
| 307 |
actual.onComplete(); |
| 308 |
return; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
emitted = e; |
| 313 |
consumed = c; |
| 314 |
missed = addAndGet(-missed); |
| 315 |
if (missed == 0) { |
| 316 |
break; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
static final class OtherObserver<T> extends AtomicReference<Disposable> |
| 322 |
implements MaybeObserver<T> { |
| 323 |
|
| 324 |
private static final long serialVersionUID = -2935427570954647017L; |
| 325 |
|
| 326 |
final MergeWithObserver<T> parent; |
| 327 |
|
| 328 |
OtherObserver(MergeWithObserver<T> parent) { |
| 329 |
this.parent = parent; |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public void onSubscribe(Disposable d) { |
| 334 |
DisposableHelper.setOnce(this, d); |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
public void onSuccess(T t) { |
| 339 |
parent.otherSuccess(t); |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
public void onError(Throwable e) { |
| 344 |
parent.otherError(e); |
| 345 |
} |
| 346 |
|
| 347 |
@Override |
| 348 |
public void onComplete() { |
| 349 |
parent.otherComplete(); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
} |