io.reactivex.internal.operators.flowable.FlowableCombineLatest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava Show documentation
Show all versions of rxjava Show documentation
Reactive Extensions for Java
/**
* 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.internal.operators.flowable;
import java.util.Iterator;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.annotations.*;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.operators.flowable.FlowableMap.MapSubscriber;
import io.reactivex.internal.queue.SpscLinkedArrayQueue;
import io.reactivex.internal.subscriptions.*;
import io.reactivex.internal.util.*;
import io.reactivex.plugins.RxJavaPlugins;
/**
* 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 BasicIntQueueSubscription {
private static final long serialVersionUID = -5082275438355852221L;
final Subscriber super R> downstream;
final Function super Object[], ? extends R> combiner;
final CombineLatestInnerSubscriber[] subscribers;
final SpscLinkedArrayQueue
© 2015 - 2024 Weber Informatics LLC | Privacy Policy