| 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.Objects; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.annotations.*; |
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 24 |
import io.reactivex.rxjava3.functions.Function; |
| 25 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableMap.MapSubscriber; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 27 |
import io.reactivex.rxjava3.internal.util.*; |
| 28 |
import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
|
| 31 |
/** |
| 32 |
* Combines the latest values from multiple sources through a function. |
| 33 |
* |
| 34 |
* @param <T> the value type of the sources |
| 35 |
* @param <R> the result type |
| 36 |
*/ |
| 37 |
public final class FlowableCombineLatest<T, R> |
| 38 |
extends Flowable<R> { |
| 39 |
|
| 40 |
@Nullable |
| 41 |
final Publisher<? extends T>[] array; |
| 42 |
|
| 43 |
@Nullable |
| 44 |
final Iterable<? extends Publisher<? extends T>> iterable; |
| 45 |
|
| 46 |
final Function<? super Object[], ? extends R> combiner; |
| 47 |
|
| 48 |
final int bufferSize; |
| 49 |
|
| 50 |
final boolean delayErrors; |
| 51 |
|
| 52 |
public FlowableCombineLatest(@NonNull Publisher<? extends T>[] array, |
| 53 |
@NonNull Function<? super Object[], ? extends R> combiner, |
| 54 |
int bufferSize, boolean delayErrors) { |
| 55 |
this.array = array; |
| 56 |
this.iterable = null; |
| 57 |
this.combiner = combiner; |
| 58 |
this.bufferSize = bufferSize; |
| 59 |
this.delayErrors = delayErrors; |
| 60 |
} |
| 61 |
|
| 62 |
public FlowableCombineLatest(@NonNull Iterable<? extends Publisher<? extends T>> iterable, |
| 63 |
@NonNull Function<? super Object[], ? extends R> combiner, |
| 64 |
int bufferSize, boolean delayErrors) { |
| 65 |
this.array = null; |
| 66 |
this.iterable = iterable; |
| 67 |
this.combiner = combiner; |
| 68 |
this.bufferSize = bufferSize; |
| 69 |
this.delayErrors = delayErrors; |
| 70 |
} |
| 71 |
|
| 72 |
@SuppressWarnings("unchecked") |
| 73 |
@Override |
| 74 |
public void subscribeActual(Subscriber<? super R> s) { |
| 75 |
Publisher<? extends T>[] sources = array; |
| 76 |
int count; |
| 77 |
if (sources == null) { |
| 78 |
count = 0; |
| 79 |
sources = new Publisher[8]; |
| 80 |
|
| 81 |
try { |
| 82 |
for (Publisher<? extends T> p : iterable) { |
| 83 |
if (count == sources.length) { |
| 84 |
Publisher<? extends T>[] b = new Publisher[count + (count >> 2)]; |
| 85 |
System.arraycopy(sources, 0, b, 0, count); |
| 86 |
sources = b; |
| 87 |
} |
| 88 |
sources[count++] = Objects.requireNonNull(p, "The Iterator returned a null Publisher"); |
| 89 |
} |
| 90 |
} catch (Throwable ex) { |
| 91 |
Exceptions.throwIfFatal(ex); |
| 92 |
EmptySubscription.error(ex, s); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
} else { |
| 97 |
count = sources.length; |
| 98 |
} |
| 99 |
|
| 100 |
if (count == 0) { |
| 101 |
EmptySubscription.complete(s); |
| 102 |
return; |
| 103 |
} |
| 104 |
if (count == 1) { |
| 105 |
sources[0].subscribe(new MapSubscriber<>(s, new SingletonArrayFunc())); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
CombineLatestCoordinator<T, R> coordinator = |
| 110 |
new CombineLatestCoordinator<>(s, combiner, count, bufferSize, delayErrors); |
| 111 |
|
| 112 |
s.onSubscribe(coordinator); |
| 113 |
|
| 114 |
coordinator.subscribe(sources, count); |
| 115 |
} |
| 116 |
|
| 117 |
static final class CombineLatestCoordinator<T, R> |
| 118 |
extends BasicIntQueueSubscription<R> { |
| 119 |
|
| 120 |
private static final long serialVersionUID = -5082275438355852221L; |
| 121 |
|
| 122 |
final Subscriber<? super R> downstream; |
| 123 |
|
| 124 |
final Function<? super Object[], ? extends R> combiner; |
| 125 |
|
| 126 |
final CombineLatestInnerSubscriber<T>[] subscribers; |
| 127 |
|
| 128 |
final SpscLinkedArrayQueue<Object> queue; |
| 129 |
|
| 130 |
final Object[] latest; |
| 131 |
|
| 132 |
final boolean delayErrors; |
| 133 |
|
| 134 |
boolean outputFused; |
| 135 |
|
| 136 |
int nonEmptySources; |
| 137 |
|
| 138 |
int completedSources; |
| 139 |
|
| 140 |
volatile boolean cancelled; |
| 141 |
|
| 142 |
final AtomicLong requested; |
| 143 |
|
| 144 |
volatile boolean done; |
| 145 |
|
| 146 |
final AtomicThrowable error; |
| 147 |
|
| 148 |
CombineLatestCoordinator(Subscriber<? super R> actual, |
| 149 |
Function<? super Object[], ? extends R> combiner, int n, |
| 150 |
int bufferSize, boolean delayErrors) { |
| 151 |
this.downstream = actual; |
| 152 |
this.combiner = combiner; |
| 153 |
@SuppressWarnings("unchecked") |
| 154 |
CombineLatestInnerSubscriber<T>[] a = new CombineLatestInnerSubscriber[n]; |
| 155 |
for (int i = 0; i < n; i++) { |
| 156 |
a[i] = new CombineLatestInnerSubscriber<>(this, i, bufferSize); |
| 157 |
} |
| 158 |
this.subscribers = a; |
| 159 |
this.latest = new Object[n]; |
| 160 |
this.queue = new SpscLinkedArrayQueue<>(bufferSize); |
| 161 |
this.requested = new AtomicLong(); |
| 162 |
this.error = new AtomicThrowable(); |
| 163 |
this.delayErrors = delayErrors; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void request(long n) { |
| 168 |
if (SubscriptionHelper.validate(n)) { |
| 169 |
BackpressureHelper.add(requested, n); |
| 170 |
drain(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public void cancel() { |
| 176 |
cancelled = true; |
| 177 |
cancelAll(); |
| 178 |
drain(); |
| 179 |
} |
| 180 |
|
| 181 |
void subscribe(Publisher<? extends T>[] sources, int n) { |
| 182 |
CombineLatestInnerSubscriber<T>[] a = subscribers; |
| 183 |
|
| 184 |
for (int i = 0; i < n; i++) { |
| 185 |
if (done || cancelled) { |
| 186 |
return; |
| 187 |
} |
| 188 |
sources[i].subscribe(a[i]); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void innerValue(int index, T value) { |
| 193 |
|
| 194 |
boolean replenishInsteadOfDrain; |
| 195 |
|
| 196 |
synchronized (this) { |
| 197 |
Object[] os = latest; |
| 198 |
|
| 199 |
int localNonEmptySources = nonEmptySources; |
| 200 |
|
| 201 |
if (os[index] == null) { |
| 202 |
localNonEmptySources++; |
| 203 |
nonEmptySources = localNonEmptySources; |
| 204 |
} |
| 205 |
|
| 206 |
os[index] = value; |
| 207 |
|
| 208 |
if (os.length == localNonEmptySources) { |
| 209 |
|
| 210 |
queue.offer(subscribers[index], os.clone()); |
| 211 |
|
| 212 |
replenishInsteadOfDrain = false; |
| 213 |
} else { |
| 214 |
replenishInsteadOfDrain = true; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if (replenishInsteadOfDrain) { |
| 219 |
subscribers[index].requestOne(); |
| 220 |
} else { |
| 221 |
drain(); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void innerComplete(int index) { |
| 226 |
synchronized (this) { |
| 227 |
Object[] os = latest; |
| 228 |
|
| 229 |
if (os[index] != null) { |
| 230 |
int localCompletedSources = completedSources + 1; |
| 231 |
|
| 232 |
if (localCompletedSources == os.length) { |
| 233 |
done = true; |
| 234 |
} else { |
| 235 |
completedSources = localCompletedSources; |
| 236 |
return; |
| 237 |
} |
| 238 |
} else { |
| 239 |
done = true; |
| 240 |
} |
| 241 |
} |
| 242 |
drain(); |
| 243 |
} |
| 244 |
|
| 245 |
void innerError(int index, Throwable e) { |
| 246 |
|
| 247 |
if (ExceptionHelper.addThrowable(error, e)) { |
| 248 |
if (!delayErrors) { |
| 249 |
cancelAll(); |
| 250 |
done = true; |
| 251 |
drain(); |
| 252 |
} else { |
| 253 |
innerComplete(index); |
| 254 |
} |
| 255 |
} else { |
| 256 |
RxJavaPlugins.onError(e); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
void drainOutput() { |
| 261 |
final Subscriber<? super R> a = downstream; |
| 262 |
final SpscLinkedArrayQueue<Object> q = queue; |
| 263 |
|
| 264 |
int missed = 1; |
| 265 |
|
| 266 |
for (;;) { |
| 267 |
|
| 268 |
if (cancelled) { |
| 269 |
q.clear(); |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
Throwable ex = error.get(); |
| 274 |
if (ex != null) { |
| 275 |
q.clear(); |
| 276 |
|
| 277 |
a.onError(ex); |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
boolean d = done; |
| 282 |
|
| 283 |
boolean empty = q.isEmpty(); |
| 284 |
|
| 285 |
if (!empty) { |
| 286 |
a.onNext(null); |
| 287 |
} |
| 288 |
|
| 289 |
if (d && empty) { |
| 290 |
a.onComplete(); |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
missed = addAndGet(-missed); |
| 295 |
if (missed == 0) { |
| 296 |
break; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
@SuppressWarnings("unchecked") |
| 302 |
void drainAsync() { |
| 303 |
final Subscriber<? super R> a = downstream; |
| 304 |
final SpscLinkedArrayQueue<Object> q = queue; |
| 305 |
|
| 306 |
int missed = 1; |
| 307 |
|
| 308 |
for (;;) { |
| 309 |
|
| 310 |
long r = requested.get(); |
| 311 |
long e = 0L; |
| 312 |
|
| 313 |
while (e != r) { |
| 314 |
boolean d = done; |
| 315 |
|
| 316 |
Object v = q.poll(); |
| 317 |
|
| 318 |
boolean empty = v == null; |
| 319 |
|
| 320 |
if (checkTerminated(d, empty, a, q)) { |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
if (empty) { |
| 325 |
break; |
| 326 |
} |
| 327 |
|
| 328 |
T[] va = (T[])q.poll(); |
| 329 |
|
| 330 |
R w; |
| 331 |
|
| 332 |
try { |
| 333 |
w = Objects.requireNonNull(combiner.apply(va), "The combiner returned a null value"); |
| 334 |
} catch (Throwable ex) { |
| 335 |
Exceptions.throwIfFatal(ex); |
| 336 |
|
| 337 |
cancelAll(); |
| 338 |
ExceptionHelper.addThrowable(error, ex); |
| 339 |
ex = ExceptionHelper.terminate(error); |
| 340 |
|
| 341 |
a.onError(ex); |
| 342 |
return; |
| 343 |
} |
| 344 |
|
| 345 |
a.onNext(w); |
| 346 |
|
| 347 |
((CombineLatestInnerSubscriber<T>)v).requestOne(); |
| 348 |
|
| 349 |
e++; |
| 350 |
} |
| 351 |
|
| 352 |
if (e == r) { |
| 353 |
if (checkTerminated(done, q.isEmpty(), a, q)) { |
| 354 |
return; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
if (e != 0L && r != Long.MAX_VALUE) { |
| 359 |
requested.addAndGet(-e); |
| 360 |
} |
| 361 |
|
| 362 |
missed = addAndGet(-missed); |
| 363 |
if (missed == 0) { |
| 364 |
break; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
void drain() { |
| 370 |
if (getAndIncrement() != 0) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
if (outputFused) { |
| 375 |
drainOutput(); |
| 376 |
} else { |
| 377 |
drainAsync(); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
boolean checkTerminated(boolean d, boolean empty, Subscriber<?> a, SpscLinkedArrayQueue<?> q) { |
| 382 |
if (cancelled) { |
| 383 |
cancelAll(); |
| 384 |
q.clear(); |
| 385 |
error.tryTerminateAndReport(); |
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
if (d) { |
| 390 |
if (delayErrors) { |
| 391 |
if (empty) { |
| 392 |
cancelAll(); |
| 393 |
error.tryTerminateConsumer(a); |
| 394 |
return true; |
| 395 |
} |
| 396 |
} else { |
| 397 |
Throwable e = ExceptionHelper.terminate(error); |
| 398 |
|
| 399 |
if (e != null && e != ExceptionHelper.TERMINATED) { |
| 400 |
cancelAll(); |
| 401 |
q.clear(); |
| 402 |
a.onError(e); |
| 403 |
return true; |
| 404 |
} else |
| 405 |
if (empty) { |
| 406 |
cancelAll(); |
| 407 |
|
| 408 |
a.onComplete(); |
| 409 |
return true; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
void cancelAll() { |
| 417 |
for (CombineLatestInnerSubscriber<T> inner : subscribers) { |
| 418 |
inner.cancel(); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
@Override |
| 423 |
public int requestFusion(int requestedMode) { |
| 424 |
if ((requestedMode & BOUNDARY) != 0) { |
| 425 |
return NONE; |
| 426 |
} |
| 427 |
int m = requestedMode & ASYNC; |
| 428 |
outputFused = m != 0; |
| 429 |
return m; |
| 430 |
} |
| 431 |
|
| 432 |
@Nullable |
| 433 |
@SuppressWarnings("unchecked") |
| 434 |
@Override |
| 435 |
public R poll() throws Throwable { |
| 436 |
Object e = queue.poll(); |
| 437 |
if (e == null) { |
| 438 |
return null; |
| 439 |
} |
| 440 |
T[] a = (T[])queue.poll(); |
| 441 |
R r = Objects.requireNonNull(combiner.apply(a), "The combiner returned a null value"); |
| 442 |
((CombineLatestInnerSubscriber<T>)e).requestOne(); |
| 443 |
return r; |
| 444 |
} |
| 445 |
|
| 446 |
@Override |
| 447 |
public void clear() { |
| 448 |
queue.clear(); |
| 449 |
} |
| 450 |
|
| 451 |
@Override |
| 452 |
public boolean isEmpty() { |
| 453 |
return queue.isEmpty(); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
static final class CombineLatestInnerSubscriber<T> |
| 458 |
extends AtomicReference<Subscription> |
| 459 |
implements FlowableSubscriber<T> { |
| 460 |
|
| 461 |
private static final long serialVersionUID = -8730235182291002949L; |
| 462 |
|
| 463 |
final CombineLatestCoordinator<T, ?> parent; |
| 464 |
|
| 465 |
final int index; |
| 466 |
|
| 467 |
final int prefetch; |
| 468 |
|
| 469 |
final int limit; |
| 470 |
|
| 471 |
int produced; |
| 472 |
|
| 473 |
CombineLatestInnerSubscriber(CombineLatestCoordinator<T, ?> parent, int index, int prefetch) { |
| 474 |
this.parent = parent; |
| 475 |
this.index = index; |
| 476 |
this.prefetch = prefetch; |
| 477 |
this.limit = prefetch - (prefetch >> 2); |
| 478 |
} |
| 479 |
|
| 480 |
@Override |
| 481 |
public void onSubscribe(Subscription s) { |
| 482 |
SubscriptionHelper.setOnce(this, s, prefetch); |
| 483 |
} |
| 484 |
|
| 485 |
@Override |
| 486 |
public void onNext(T t) { |
| 487 |
parent.innerValue(index, t); |
| 488 |
} |
| 489 |
|
| 490 |
@Override |
| 491 |
public void onError(Throwable t) { |
| 492 |
parent.innerError(index, t); |
| 493 |
} |
| 494 |
|
| 495 |
@Override |
| 496 |
public void onComplete() { |
| 497 |
parent.innerComplete(index); |
| 498 |
} |
| 499 |
|
| 500 |
public void cancel() { |
| 501 |
SubscriptionHelper.cancel(this); |
| 502 |
} |
| 503 |
|
| 504 |
public void requestOne() { |
| 505 |
|
| 506 |
int p = produced + 1; |
| 507 |
if (p == limit) { |
| 508 |
produced = 0; |
| 509 |
get().request(p); |
| 510 |
} else { |
| 511 |
produced = p; |
| 512 |
} |
| 513 |
|
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
final class SingletonArrayFunc implements Function<T, R> { |
| 518 |
@Override |
| 519 |
public R apply(T t) throws Throwable { |
| 520 |
return combiner.apply(new Object[] { t }); |
| 521 |
} |
| 522 |
} |
| 523 |
} |