io.reactivex.flowable.internal.operators.FlowableCombineLatest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava3-flowable Show documentation
Show all versions of rxjava3-flowable Show documentation
rxjava3-flowable developed by David Karnok
The newest version!
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.flowable.internal.operators;
import java.util.Iterator;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import hu.akarnokd.reactivestreams.extensions.RelaxedSubscriber;
import io.reactivex.common.RxJavaCommonPlugins;
import io.reactivex.common.annotations.*;
import io.reactivex.common.exceptions.Exceptions;
import io.reactivex.common.functions.Function;
import io.reactivex.common.internal.functions.ObjectHelper;
import io.reactivex.common.internal.utils.ExceptionHelper;
import io.reactivex.flowable.Flowable;
import io.reactivex.flowable.internal.operators.FlowableMap.MapSubscriber;
import io.reactivex.flowable.internal.queues.SpscLinkedArrayQueue;
import io.reactivex.flowable.internal.subscriptions.*;
import io.reactivex.flowable.internal.utils.BackpressureHelper;
/**
* Combines the latest values from multiple sources through a function.
*
* @param the value type of the sources
* @param the result type
*/
public final class FlowableCombineLatest
extends Flowable {
@Nullable
final Publisher extends T>[] array;
@Nullable
final Iterable extends Publisher extends T>> iterable;
final Function super Object[], ? extends R> combiner;
final int bufferSize;
final boolean delayErrors;
public FlowableCombineLatest(@NonNull Publisher extends T>[] array,
@NonNull Function super Object[], ? extends R> combiner,
int bufferSize, boolean delayErrors) {
this.array = array;
this.iterable = null;
this.combiner = combiner;
this.bufferSize = bufferSize;
this.delayErrors = delayErrors;
}
public FlowableCombineLatest(@NonNull Iterable extends Publisher extends T>> iterable,
@NonNull Function super Object[], ? extends R> combiner,
int bufferSize, boolean delayErrors) {
this.array = null;
this.iterable = iterable;
this.combiner = combiner;
this.bufferSize = bufferSize;
this.delayErrors = delayErrors;
}
@SuppressWarnings("unchecked")
@Override
public void subscribeActual(Subscriber super R> s) {
Publisher extends T>[] a = array;
int n;
if (a == null) {
n = 0;
a = new Publisher[8];
Iterator extends Publisher extends T>> it;
try {
it = ObjectHelper.requireNonNull(iterable.iterator(), "The iterator returned is null");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
EmptySubscription.error(e, s);
return;
}
for (;;) {
boolean b;
try {
b = it.hasNext();
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
EmptySubscription.error(e, s);
return;
}
if (!b) {
break;
}
Publisher extends T> p;
try {
p = ObjectHelper.requireNonNull(it.next(), "The publisher returned by the iterator is null");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
EmptySubscription.error(e, s);
return;
}
if (n == a.length) {
Publisher extends T>[] c = new Publisher[n + (n >> 2)];
System.arraycopy(a, 0, c, 0, n);
a = c;
}
a[n++] = p;
}
} else {
n = a.length;
}
if (n == 0) {
EmptySubscription.complete(s);
return;
}
if (n == 1) {
((Publisher)a[0]).subscribe(new MapSubscriber(s, new SingletonArrayFunc()));
return;
}
CombineLatestCoordinator coordinator =
new CombineLatestCoordinator(s, combiner, n, bufferSize, delayErrors);
s.onSubscribe(coordinator);
coordinator.subscribe(a, n);
}
static final class CombineLatestCoordinator
extends BasicIntFusedQueueSubscription {
private static final long serialVersionUID = -5082275438355852221L;
final Subscriber super R> actual;
final Function super Object[], ? extends R> combiner;
final CombineLatestInnerSubscriber[] subscribers;
final SpscLinkedArrayQueue