rx.internal.operators.OperatorZip Maven / Gradle / Ivy
Show all versions of rxjava-core Show documentation
/**
* Copyright 2014 Netflix, Inc.
*
* 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 rx.internal.operators;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import rx.Observable;
import rx.Observable.Operator;
import rx.Observer;
import rx.Subscriber;
import rx.exceptions.OnErrorThrowable;
import rx.functions.Func2;
import rx.functions.Func3;
import rx.functions.Func4;
import rx.functions.Func5;
import rx.functions.Func6;
import rx.functions.Func7;
import rx.functions.Func8;
import rx.functions.Func9;
import rx.functions.FuncN;
import rx.functions.Functions;
import rx.subscriptions.CompositeSubscription;
/**
* Returns an Observable that emits the results of a function applied to sets of items emitted, in
* sequence, by two or more other Observables.
*
*
*
* The zip operation applies this function in strict sequence, so the first item emitted by the new
* Observable will be the result of the function applied to the first item emitted by each zipped
* Observable; the second item emitted by the new Observable will be the result of the function
* applied to the second item emitted by each zipped Observable; and so forth.
*
* The resulting Observable returned from zip will invoke onNext
as many times as the
* number of onNext
invocations of the source Observable that emits the fewest items.
* @param the result type
*/
public final class OperatorZip implements Operator[]> {
/*
* Raw types are used so we can use a single implementation for all arities such as zip(t1, t2) and zip(t1, t2, t3) etc.
* The types will be cast on the edges so usage will be the type-safe but the internals are not.
*/
final FuncN extends R> zipFunction;
public OperatorZip(FuncN extends R> f) {
this.zipFunction = f;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func2 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func3 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func4 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func5 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func6 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func7 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func8 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func9 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings("rawtypes")
@Override
public Subscriber super Observable[]> call(final Subscriber super R> observer) {
return new Subscriber(observer) {
boolean started = false;
@Override
public void onCompleted() {
if (!started) {
// this means we have not received a valid onNext before termination so we emit the onCompleted
observer.onCompleted();
}
}
@Override
public void onError(Throwable e) {
observer.onError(e);
}
@Override
public void onNext(Observable[] observables) {
if (observables == null || observables.length == 0) {
observer.onCompleted();
} else {
started = true;
new Zip(observables, observer, zipFunction).zip();
}
}
};
}
static final NotificationLite