io.reactivex.internal.operators.observable.ObservableWithLatestFromMany 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.observable;
import java.util.Arrays;
import java.util.concurrent.atomic.*;
import io.reactivex.*;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;
import io.reactivex.internal.disposables.*;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.util.*;
import io.reactivex.plugins.RxJavaPlugins;
/**
* Combines a main sequence of values with the latest from multiple other sequences via
* a selector function.
*
* @param the main sequence's type
* @param the output type
*/
public final class ObservableWithLatestFromMany extends AbstractObservableWithUpstream {
@Nullable
final ObservableSource>[] otherArray;
@Nullable
final Iterable extends ObservableSource>> otherIterable;
@NonNull
final Function super Object[], R> combiner;
public ObservableWithLatestFromMany(@NonNull ObservableSource source, @NonNull ObservableSource>[] otherArray, @NonNull Function super Object[], R> combiner) {
super(source);
this.otherArray = otherArray;
this.otherIterable = null;
this.combiner = combiner;
}
public ObservableWithLatestFromMany(@NonNull ObservableSource source, @NonNull Iterable extends ObservableSource>> otherIterable, @NonNull Function super Object[], R> combiner) {
super(source);
this.otherArray = null;
this.otherIterable = otherIterable;
this.combiner = combiner;
}
@Override
protected void subscribeActual(Observer super R> observer) {
ObservableSource>[] others = otherArray;
int n = 0;
if (others == null) {
others = new ObservableSource[8];
try {
for (ObservableSource> p : otherIterable) {
if (n == others.length) {
others = Arrays.copyOf(others, n + (n >> 1));
}
others[n++] = p;
}
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
EmptyDisposable.error(ex, observer);
return;
}
} else {
n = others.length;
}
if (n == 0) {
new ObservableMap(source, new SingletonArrayFunc()).subscribeActual(observer);
return;
}
WithLatestFromObserver parent = new WithLatestFromObserver(observer, combiner, n);
observer.onSubscribe(parent);
parent.subscribe(others, n);
source.subscribe(parent);
}
static final class WithLatestFromObserver
extends AtomicInteger
implements Observer, Disposable {
private static final long serialVersionUID = 1577321883966341961L;
final Observer super R> downstream;
final Function super Object[], R> combiner;
final WithLatestInnerObserver[] observers;
final AtomicReferenceArray
© 2015 - 2024 Weber Informatics LLC | Privacy Policy