/** * Copyright 2016 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 io.reactivex; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicReference; import org.reactivestreams.*; import io.reactivex.annotations.*; import io.reactivex.disposables.*; import io.reactivex.functions.*; import io.reactivex.internal.disposables.EmptyDisposable; import io.reactivex.internal.functions.Functions; import io.reactivex.internal.functions.Objects; import io.reactivex.internal.operators.observable.*; import io.reactivex.internal.subscribers.observable.*; import io.reactivex.internal.util.Exceptions; import io.reactivex.observables.*; import io.reactivex.observers.*; import io.reactivex.plugins.RxJavaPlugins; import io.reactivex.schedulers.*; /** * Observable for delivering a sequence of values without backpressure. * @param */ public abstract class Observable implements ObservableConsumable { public interface NbpOperator extends Function, Observer> { } public interface NbpTransformer extends Function, ObservableConsumable> { } /** An empty observable instance as there is no need to instantiate this more than once. */ static final Observable EMPTY = create(new ObservableConsumable() { @Override public void subscribe(Observer s) { s.onSubscribe(EmptyDisposable.INSTANCE); s.onComplete(); } }); /** A never NbpObservable instance as there is no need to instantiate this more than once. */ static final Observable NEVER = create(new ObservableConsumable() { @Override public void subscribe(Observer s) { s.onSubscribe(EmptyDisposable.INSTANCE); } }); static final Object OBJECT = new Object(); public static Observable amb(Iterable> sources) { Objects.requireNonNull(sources, "sources is null"); return (new ObservableAmb(null, sources)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable amb(ObservableConsumable... sources) { Objects.requireNonNull(sources, "sources is null"); int len = sources.length; if (len == 0) { return empty(); } else if (len == 1) { return (Observable)sources[0]; // FIXME wrap() } return (new ObservableAmb(sources, null)); } /** * Returns the default 'island' size or capacity-increment hint for unbounded buffers. * @return */ static int bufferSize() { return Flowable.bufferSize(); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest(Function combiner, boolean delayError, int bufferSize, ObservableConsumable... sources) { return combineLatest(sources, combiner, delayError, bufferSize); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest(Iterable> sources, Function combiner) { return combineLatest(sources, combiner, false, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest(Iterable> sources, Function combiner, boolean delayError) { return combineLatest(sources, combiner, delayError, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest(Iterable> sources, Function combiner, boolean delayError, int bufferSize) { Objects.requireNonNull(sources, "sources is null"); Objects.requireNonNull(combiner, "combiner is null"); validateBufferSize(bufferSize); // the queue holds a pair of values so we need to double the capacity int s = bufferSize << 1; return (new ObservableCombineLatest(null, sources, combiner, s, delayError)); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest(ObservableConsumable[] sources, Function combiner) { return combineLatest(sources, combiner, false, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest(ObservableConsumable[] sources, Function combiner, boolean delayError) { return combineLatest(sources, combiner, delayError, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest(ObservableConsumable[] sources, Function combiner, boolean delayError, int bufferSize) { validateBufferSize(bufferSize); Objects.requireNonNull(combiner, "combiner is null"); if (sources.length == 0) { return empty(); } // the queue holds a pair of values so we need to double the capacity int s = bufferSize << 1; return (new ObservableCombineLatest(sources, null, combiner, s, delayError)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest( ObservableConsumable p1, ObservableConsumable p2, BiFunction combiner) { return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, Function3 combiner) { return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, Function4 combiner) { return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, Function5 combiner) { return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, Function6 combiner) { return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5, p6); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, ObservableConsumable p7, Function7 combiner) { return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, ObservableConsumable p7, ObservableConsumable p8, Function8 combiner) { return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7, p8); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable combineLatest( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, ObservableConsumable p7, ObservableConsumable p8, ObservableConsumable p9, Function9 combiner) { return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7, p8, p9); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat(int prefetch, Iterable> sources) { Objects.requireNonNull(sources, "sources is null"); return fromIterable(sources).concatMap((Function)Functions.identity(), prefetch); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat(Iterable> sources) { Objects.requireNonNull(sources, "sources is null"); return fromIterable(sources).concatMap((Function)Functions.identity()); } @SchedulerSupport(SchedulerSupport.NONE) public static final Observable concat(ObservableConsumable> sources) { return concat(sources, bufferSize()); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static final Observable concat(ObservableConsumable> sources, int bufferSize) { Objects.requireNonNull(sources, "sources is null"); return new ObservableLift>(sources, new NbpOperatorConcatMap(Functions.identity(), bufferSize)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat(ObservableConsumable p1, ObservableConsumable p2) { return concatArray(p1, p2); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3) { return concatArray(p1, p2, p3); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4) { return concatArray(p1, p2, p3, p4); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5 ) { return concatArray(p1, p2, p3, p4, p5); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6 ) { return concatArray(p1, p2, p3, p4, p5, p6); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, ObservableConsumable p7 ) { return concatArray(p1, p2, p3, p4, p5, p6, p7); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, ObservableConsumable p7, ObservableConsumable p8 ) { return concatArray(p1, p2, p3, p4, p5, p6, p7, p8); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, ObservableConsumable p7, ObservableConsumable p8, ObservableConsumable p9 ) { return concatArray(p1, p2, p3, p4, p5, p6, p7, p8, p9); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable concatArray(int prefetch, ObservableConsumable... sources) { Objects.requireNonNull(sources, "sources is null"); return fromArray(sources).concatMap((Function)Functions.identity(), prefetch); } /** * Concatenates a variable number of NbpObservable sources. *

* Note: named this way because of overload conflict with concat(NbpObservable<NbpObservable>) * @param sources the array of sources * @param the common base value type * @return the new NbpObservable instance * @throws NullPointerException if sources is null */ @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable concatArray(ObservableConsumable... sources) { if (sources.length == 0) { return empty(); } else if (sources.length == 1) { return wrap((ObservableConsumable)sources[0]); } return fromArray(sources).concatMap((Function)Functions.identity()); } public static Observable create(ObservableConsumable onSubscribe) { Objects.requireNonNull(onSubscribe, "onSubscribe is null"); return new ObservableWrapper(onSubscribe); } public static Observable wrap(ObservableConsumable onSubscribe) { Objects.requireNonNull(onSubscribe, "onSubscribe is null"); // TODO plugin wrapper? if (onSubscribe instanceof Observable) { return (Observable)onSubscribe; } return new ObservableWrapper(onSubscribe); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable defer(Supplier> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return (new ObservableDefer(supplier)); } @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Observable empty() { return (Observable)EMPTY; } @SchedulerSupport(SchedulerSupport.NONE) public static Observable error(Supplier errorSupplier) { Objects.requireNonNull(errorSupplier, "errorSupplier is null"); return (new ObservableError(errorSupplier)); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable error(final Throwable e) { Objects.requireNonNull(e, "e is null"); return error(new Supplier() { @Override public Throwable get() { return e; } }); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable fromArray(T... values) { Objects.requireNonNull(values, "values is null"); if (values.length == 0) { return empty(); } else if (values.length == 1) { return just(values[0]); } return (new ObservableFromArray(values)); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable fromCallable(Callable supplier) { Objects.requireNonNull(supplier, "supplier is null"); return (new ObservableFromCallable(supplier)); } /* * It doesn't add cancellation support by default like 1.x * if necessary, one can use composition to achieve it: * futureObservable.doOnCancel(() -> future.cancel(true)); */ @SchedulerSupport(SchedulerSupport.NONE) public static Observable fromFuture(Future future) { Objects.requireNonNull(future, "future is null"); return (new ObservableFromFuture(future, 0L, null)); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable fromFuture(Future future, long timeout, TimeUnit unit) { Objects.requireNonNull(future, "future is null"); Objects.requireNonNull(unit, "unit is null"); Observable o = create(new ObservableFromFuture(future, timeout, unit)); return o; } @SchedulerSupport(SchedulerSupport.CUSTOM) public static Observable fromFuture(Future future, long timeout, TimeUnit unit, Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); Observable o = fromFuture(future, timeout, unit); return o.subscribeOn(scheduler); } @SchedulerSupport(SchedulerSupport.IO) public static Observable fromFuture(Future future, Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); Observable o = fromFuture(future); return o.subscribeOn(Schedulers.io()); } public static Observable fromIterable(Iterable source) { Objects.requireNonNull(source, "source is null"); return (new ObservableFromIterable(source)); } public static Observable fromPublisher(final Publisher publisher) { Objects.requireNonNull(publisher, "publisher is null"); return create(new ObservableConsumable() { @Override public void subscribe(final Observer s) { publisher.subscribe(new Subscriber() { @Override public void onComplete() { s.onComplete(); } @Override public void onError(Throwable t) { s.onError(t); } @Override public void onNext(T t) { s.onNext(t); } @Override public void onSubscribe(Subscription inner) { s.onSubscribe(Disposables.from(inner)); inner.request(Long.MAX_VALUE); } }); } }); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable generate(final Consumer> generator) { Objects.requireNonNull(generator, "generator is null"); return generate(Functions.nullSupplier(), new BiFunction, Object>() { @Override public Object apply(Object s, Observer o) { generator.accept(o); return s; } }, Functions.emptyConsumer()); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable generate(Supplier initialState, final BiConsumer> generator) { Objects.requireNonNull(generator, "generator is null"); return generate(initialState, new BiFunction, S>() { @Override public S apply(S s, Observer o) { generator.accept(s, o); return s; } }, Functions.emptyConsumer()); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable generate( final Supplier initialState, final BiConsumer> generator, Consumer disposeState) { Objects.requireNonNull(generator, "generator is null"); return generate(initialState, new BiFunction, S>() { @Override public S apply(S s, Observer o) { generator.accept(s, o); return s; } }, disposeState); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable generate(Supplier initialState, BiFunction, S> generator) { return generate(initialState, generator, Functions.emptyConsumer()); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable generate(Supplier initialState, BiFunction, S> generator, Consumer disposeState) { Objects.requireNonNull(initialState, "initialState is null"); Objects.requireNonNull(generator, "generator is null"); Objects.requireNonNull(disposeState, "diposeState is null"); return (new ObservableGenerate(initialState, generator, disposeState)); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public static Observable interval(long initialDelay, long period, TimeUnit unit) { return interval(initialDelay, period, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public static Observable interval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) { if (initialDelay < 0) { initialDelay = 0L; } if (period < 0) { period = 0L; } Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return (new ObservableInterval(initialDelay, period, unit, scheduler)); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public static Observable interval(long period, TimeUnit unit) { return interval(period, period, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public static Observable interval(long period, TimeUnit unit, Scheduler scheduler) { return interval(period, period, unit, scheduler); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public static Observable intervalRange(long start, long count, long initialDelay, long period, TimeUnit unit) { return intervalRange(start, count, initialDelay, period, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public static Observable intervalRange(long start, long count, long initialDelay, long period, TimeUnit unit, Scheduler scheduler) { long end = start + (count - 1); if (end < 0) { throw new IllegalArgumentException("Overflow! start + count is bigger than Long.MAX_VALUE"); } if (initialDelay < 0) { initialDelay = 0L; } if (period < 0) { period = 0L; } Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return (new ObservableIntervalRange(start, end, initialDelay, period, unit, scheduler)); } public static Observable just(T value) { Objects.requireNonNull(value, "The value is null"); return new ObservableJust(value); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static final Observable just(T v1, T v2) { Objects.requireNonNull(v1, "The first value is null"); Objects.requireNonNull(v2, "The second value is null"); return fromArray(v1, v2); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static final Observable just(T v1, T v2, T v3) { Objects.requireNonNull(v1, "The first value is null"); Objects.requireNonNull(v2, "The second value is null"); Objects.requireNonNull(v3, "The third value is null"); return fromArray(v1, v2, v3); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static final Observable just(T v1, T v2, T v3, T v4) { Objects.requireNonNull(v1, "The first value is null"); Objects.requireNonNull(v2, "The second value is null"); Objects.requireNonNull(v3, "The third value is null"); Objects.requireNonNull(v4, "The fourth value is null"); return fromArray(v1, v2, v3, v4); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static final Observable just(T v1, T v2, T v3, T v4, T v5) { Objects.requireNonNull(v1, "The first value is null"); Objects.requireNonNull(v2, "The second value is null"); Objects.requireNonNull(v3, "The third value is null"); Objects.requireNonNull(v4, "The fourth value is null"); Objects.requireNonNull(v5, "The fifth value is null"); return fromArray(v1, v2, v3, v4, v5); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static final Observable just(T v1, T v2, T v3, T v4, T v5, T v6) { Objects.requireNonNull(v1, "The first value is null"); Objects.requireNonNull(v2, "The second value is null"); Objects.requireNonNull(v3, "The third value is null"); Objects.requireNonNull(v4, "The fourth value is null"); Objects.requireNonNull(v5, "The fifth value is null"); Objects.requireNonNull(v6, "The sixth value is null"); return fromArray(v1, v2, v3, v4, v5, v6); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static final Observable just(T v1, T v2, T v3, T v4, T v5, T v6, T v7) { Objects.requireNonNull(v1, "The first value is null"); Objects.requireNonNull(v2, "The second value is null"); Objects.requireNonNull(v3, "The third value is null"); Objects.requireNonNull(v4, "The fourth value is null"); Objects.requireNonNull(v5, "The fifth value is null"); Objects.requireNonNull(v6, "The sixth value is null"); Objects.requireNonNull(v7, "The seventh value is null"); return fromArray(v1, v2, v3, v4, v5, v6, v7); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static final Observable just(T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8) { Objects.requireNonNull(v1, "The first value is null"); Objects.requireNonNull(v2, "The second value is null"); Objects.requireNonNull(v3, "The third value is null"); Objects.requireNonNull(v4, "The fourth value is null"); Objects.requireNonNull(v5, "The fifth value is null"); Objects.requireNonNull(v6, "The sixth value is null"); Objects.requireNonNull(v7, "The seventh value is null"); Objects.requireNonNull(v8, "The eigth value is null"); return fromArray(v1, v2, v3, v4, v5, v6, v7, v8); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static final Observable just(T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9) { Objects.requireNonNull(v1, "The first value is null"); Objects.requireNonNull(v2, "The second value is null"); Objects.requireNonNull(v3, "The third value is null"); Objects.requireNonNull(v4, "The fourth value is null"); Objects.requireNonNull(v5, "The fifth value is null"); Objects.requireNonNull(v6, "The sixth value is null"); Objects.requireNonNull(v7, "The seventh value is null"); Objects.requireNonNull(v8, "The eigth value is null"); Objects.requireNonNull(v9, "The ninth is null"); return fromArray(v1, v2, v3, v4, v5, v6, v7, v8, v9); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(int maxConcurrency, int bufferSize, Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity(), false, maxConcurrency, bufferSize); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(int maxConcurrency, int bufferSize, ObservableConsumable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), false, maxConcurrency, bufferSize); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(int maxConcurrency, ObservableConsumable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity()); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(Iterable> sources, int maxConcurrency) { return fromIterable(sources).flatMap((Function)Functions.identity(), maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static Observable merge(ObservableConsumable> sources) { return new ObservableLift(sources, new NbpOperatorFlatMap(Functions.identity(), false, Integer.MAX_VALUE, bufferSize())); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(ObservableConsumable> sources, int maxConcurrency) { return new ObservableLift(sources, new NbpOperatorFlatMap(Functions.identity(), false, maxConcurrency, bufferSize())); } @SuppressWarnings({ "unchecked", "rawtypes" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(ObservableConsumable p1, ObservableConsumable p2) { Objects.requireNonNull(p1, "p1 is null"); Objects.requireNonNull(p2, "p2 is null"); return fromArray(p1, p2).flatMap((Function)Functions.identity(), false, 2); } @SuppressWarnings({ "unchecked", "rawtypes" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(ObservableConsumable p1, ObservableConsumable p2, Observable p3) { Objects.requireNonNull(p1, "p1 is null"); Objects.requireNonNull(p2, "p2 is null"); Objects.requireNonNull(p3, "p3 is null"); return fromArray(p1, p2, p3).flatMap((Function)Functions.identity(), false, 3); } @SuppressWarnings({ "unchecked", "rawtypes" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4) { Objects.requireNonNull(p1, "p1 is null"); Objects.requireNonNull(p2, "p2 is null"); Objects.requireNonNull(p3, "p3 is null"); Objects.requireNonNull(p4, "p4 is null"); return fromArray(p1, p2, p3, p4).flatMap((Function)Functions.identity(), false, 4); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(ObservableConsumable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), sources.length); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError(boolean delayErrors, Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity(), true); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError(int maxConcurrency, int bufferSize, Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity(), true, maxConcurrency, bufferSize); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError(int maxConcurrency, int bufferSize, ObservableConsumable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), true, maxConcurrency, bufferSize); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError(int maxConcurrency, Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity(), true, maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError(int maxConcurrency, ObservableConsumable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), true, maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static Observable mergeDelayError(ObservableConsumable> sources) { return new ObservableLift(sources, new NbpOperatorFlatMap(Functions.identity(), true, Integer.MAX_VALUE, bufferSize())); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError(ObservableConsumable> sources, int maxConcurrency) { return new ObservableLift(sources, new NbpOperatorFlatMap(Functions.identity(), true, maxConcurrency, bufferSize())); } @SuppressWarnings({ "unchecked", "rawtypes" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError(ObservableConsumable p1, ObservableConsumable p2) { Objects.requireNonNull(p1, "p1 is null"); Objects.requireNonNull(p2, "p2 is null"); return fromArray(p1, p2).flatMap((Function)Functions.identity(), true, 2); } @SuppressWarnings({ "unchecked", "rawtypes" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError(ObservableConsumable p1, ObservableConsumable p2, Observable p3) { Objects.requireNonNull(p1, "p1 is null"); Objects.requireNonNull(p2, "p2 is null"); Objects.requireNonNull(p3, "p3 is null"); return fromArray(p1, p2, p3).flatMap((Function)Functions.identity(), true, 3); } @SuppressWarnings({ "unchecked", "rawtypes" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4) { Objects.requireNonNull(p1, "p1 is null"); Objects.requireNonNull(p2, "p2 is null"); Objects.requireNonNull(p3, "p3 is null"); Objects.requireNonNull(p4, "p4 is null"); return fromArray(p1, p2, p3, p4).flatMap((Function)Functions.identity(), true, 4); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable mergeDelayError(ObservableConsumable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), true, sources.length); } @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Observable never() { return (Observable)NEVER; } @SchedulerSupport(SchedulerSupport.NONE) public static Observable range(final int start, final int count) { if (count < 0) { throw new IllegalArgumentException("count >= required but it was " + count); } else if (count == 0) { return empty(); } else if (count == 1) { return just(start); } else if ((long)start + (count - 1) > Integer.MAX_VALUE) { throw new IllegalArgumentException("Integer overflow"); } return create(new ObservableConsumable() { @Override public void subscribe(Observer s) { Disposable d = Disposables.empty(); s.onSubscribe(d); long end = start - 1L + count; for (long i = start; i <= end && !d.isDisposed(); i++) { s.onNext((int)i); } if (!d.isDisposed()) { s.onComplete(); } } }); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable sequenceEqual(ObservableConsumable p1, ObservableConsumable p2) { return sequenceEqual(p1, p2, Objects.equalsPredicate(), bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable sequenceEqual(ObservableConsumable p1, ObservableConsumable p2, BiPredicate isEqual) { return sequenceEqual(p1, p2, isEqual, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable sequenceEqual(ObservableConsumable p1, ObservableConsumable p2, BiPredicate isEqual, int bufferSize) { Objects.requireNonNull(p1, "p1 is null"); Objects.requireNonNull(p2, "p2 is null"); Objects.requireNonNull(isEqual, "isEqual is null"); validateBufferSize(bufferSize); return (new ObservableSequenceEqual(p1, p2, isEqual, bufferSize)); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable sequenceEqual(ObservableConsumable p1, ObservableConsumable p2, int bufferSize) { return sequenceEqual(p1, p2, Objects.equalsPredicate(), bufferSize); } @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static Observable switchOnNext(int bufferSize, ObservableConsumable> sources) { Objects.requireNonNull(sources, "sources is null"); return new ObservableLift(sources, new NbpOperatorSwitchMap(Functions.identity(), bufferSize)); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable switchOnNext(ObservableConsumable> sources) { return switchOnNext(bufferSize(), sources); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public static Observable timer(long delay, TimeUnit unit) { return timer(delay, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public static Observable timer(long delay, TimeUnit unit, Scheduler scheduler) { if (delay < 0) { delay = 0L; } Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return (new ObservableTimer(delay, unit, scheduler)); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable using(Supplier resourceSupplier, Function> sourceSupplier, Consumer disposer) { return using(resourceSupplier, sourceSupplier, disposer, true); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable using(Supplier resourceSupplier, Function> sourceSupplier, Consumer disposer, boolean eager) { Objects.requireNonNull(resourceSupplier, "resourceSupplier is null"); Objects.requireNonNull(sourceSupplier, "sourceSupplier is null"); Objects.requireNonNull(disposer, "disposer is null"); return (new ObservableUsing(resourceSupplier, sourceSupplier, disposer, eager)); } private static void validateBufferSize(int bufferSize) { if (bufferSize <= 0) { throw new IllegalArgumentException("bufferSize > 0 required but it was " + bufferSize); } } @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip(Iterable> sources, Function zipper) { Objects.requireNonNull(zipper, "zipper is null"); Objects.requireNonNull(sources, "sources is null"); return (new ObservableZip(null, sources, zipper, bufferSize(), false)); } @SuppressWarnings({ "rawtypes", "unchecked" }) @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip(ObservableConsumable> sources, final Function zipper) { Objects.requireNonNull(zipper, "zipper is null"); Objects.requireNonNull(sources, "sources is null"); // FIXME don't want to fiddle with manual type inference, this will be inlined later anyway return new ObservableLift(sources, NbpOperatorToList.defaultInstance()) .flatMap(new Function>, Observable>() { @Override public Observable apply(List> list) { return zipIterable(zipper, false, bufferSize(), list); } }); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, BiFunction zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, BiFunction zipper, boolean delayError) { return zipArray(Functions.toFunction(zipper), delayError, bufferSize(), p1, p2); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, BiFunction zipper, boolean delayError, int bufferSize) { return zipArray(Functions.toFunction(zipper), delayError, bufferSize, p1, p2); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, Function3 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, Function4 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, Function5 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, Function6 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5, p6); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, ObservableConsumable p7, Function7 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, ObservableConsumable p7, ObservableConsumable p8, Function8 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7, p8); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public static Observable zip( ObservableConsumable p1, ObservableConsumable p2, ObservableConsumable p3, ObservableConsumable p4, ObservableConsumable p5, ObservableConsumable p6, ObservableConsumable p7, ObservableConsumable p8, ObservableConsumable p9, Function9 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7, p8, p9); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable zipArray(Function zipper, boolean delayError, int bufferSize, ObservableConsumable... sources) { if (sources.length == 0) { return empty(); } Objects.requireNonNull(zipper, "zipper is null"); validateBufferSize(bufferSize); return (new ObservableZip(sources, null, zipper, bufferSize, delayError)); } @SchedulerSupport(SchedulerSupport.NONE) public static Observable zipIterable(Function zipper, boolean delayError, int bufferSize, Iterable> sources) { Objects.requireNonNull(zipper, "zipper is null"); Objects.requireNonNull(sources, "sources is null"); validateBufferSize(bufferSize); return (new ObservableZip(null, sources, zipper, bufferSize, delayError)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable all(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorAll(predicate)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public final Observable ambWith(ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return amb(this, other); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable any(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorAny(predicate)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable asObservable() { return create(new ObservableConsumable() { @Override public void subscribe(Observer s) { Observable.this.subscribe(s); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> buffer(int count) { return buffer(count, count); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> buffer(int count, int skip) { return buffer(count, skip, new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final > Observable buffer(int count, int skip, Supplier bufferSupplier) { if (count <= 0) { throw new IllegalArgumentException("count > 0 required but it was " + count); } if (skip <= 0) { throw new IllegalArgumentException("skip > 0 required but it was " + count); } Objects.requireNonNull(bufferSupplier, "bufferSupplier is null"); return lift(new NbpOperatorBuffer(count, skip, bufferSupplier)); } @SchedulerSupport(SchedulerSupport.NONE) public final > Observable buffer(int count, Supplier bufferSupplier) { return buffer(count, count, bufferSupplier); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable> buffer(long timespan, long timeskip, TimeUnit unit) { return buffer(timespan, timeskip, unit, Schedulers.computation(), new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> buffer(long timespan, long timeskip, TimeUnit unit, Scheduler scheduler) { return buffer(timespan, timeskip, unit, scheduler, new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final > Observable buffer(long timespan, long timeskip, TimeUnit unit, Scheduler scheduler, Supplier bufferSupplier) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); Objects.requireNonNull(bufferSupplier, "bufferSupplier is null"); return lift(new NbpOperatorBufferTimed(timespan, timeskip, unit, scheduler, bufferSupplier, Integer.MAX_VALUE, false)); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable> buffer(long timespan, TimeUnit unit) { return buffer(timespan, unit, Integer.MAX_VALUE, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable> buffer(long timespan, TimeUnit unit, int count) { return buffer(timespan, unit, count, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> buffer(long timespan, TimeUnit unit, int count, Scheduler scheduler) { return buffer(timespan, unit, count, scheduler, new Supplier>() { @Override public List get() { return new ArrayList(); } }, false); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final > Observable buffer( long timespan, TimeUnit unit, int count, Scheduler scheduler, Supplier bufferSupplier, boolean restartTimerOnMaxSize) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); Objects.requireNonNull(bufferSupplier, "bufferSupplier is null"); if (count <= 0) { throw new IllegalArgumentException("count > 0 required but it was " + count); } return lift(new NbpOperatorBufferTimed(timespan, timespan, unit, scheduler, bufferSupplier, count, restartTimerOnMaxSize)); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> buffer(long timespan, TimeUnit unit, Scheduler scheduler) { return buffer(timespan, unit, Integer.MAX_VALUE, scheduler, new Supplier>() { @Override public List get() { return new ArrayList(); } }, false); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> buffer( ObservableConsumable bufferOpenings, Function> bufferClosingSelector) { return buffer(bufferOpenings, bufferClosingSelector, new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final > Observable buffer( ObservableConsumable bufferOpenings, Function> bufferClosingSelector, Supplier bufferSupplier) { Objects.requireNonNull(bufferOpenings, "bufferOpenings is null"); Objects.requireNonNull(bufferClosingSelector, "bufferClosingSelector is null"); Objects.requireNonNull(bufferSupplier, "bufferSupplier is null"); return lift(new NbpOperatorBufferBoundary(bufferOpenings, bufferClosingSelector, bufferSupplier)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> buffer(ObservableConsumable boundary) { /* * XXX: javac complains if this is not manually cast, Eclipse is fine */ return buffer(boundary, new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> buffer(ObservableConsumable boundary, final int initialCapacity) { return buffer(boundary, new Supplier>() { @Override public List get() { return new ArrayList(initialCapacity); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final > Observable buffer(ObservableConsumable boundary, Supplier bufferSupplier) { Objects.requireNonNull(boundary, "boundary is null"); Objects.requireNonNull(bufferSupplier, "bufferSupplier is null"); return lift(new NbpOperatorBufferExactBoundary(boundary, bufferSupplier)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> buffer(Supplier> boundarySupplier) { return buffer(boundarySupplier, new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final > Observable buffer(Supplier> boundarySupplier, Supplier bufferSupplier) { Objects.requireNonNull(boundarySupplier, "boundarySupplier is null"); Objects.requireNonNull(bufferSupplier, "bufferSupplier is null"); return lift(new NbpOperatorBufferBoundarySupplier(boundarySupplier, bufferSupplier)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable cache() { return ObservableCache.from(this); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable cache(int capacityHint) { return ObservableCache.from(this, capacityHint); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable cast(final Class clazz) { Objects.requireNonNull(clazz, "clazz is null"); return map(new Function() { @Override public U apply(T v) { return clazz.cast(v); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable collect(Supplier initialValueSupplier, BiConsumer collector) { Objects.requireNonNull(initialValueSupplier, "initalValueSupplier is null"); Objects.requireNonNull(collector, "collector is null"); return lift(new NbpOperatorCollect(initialValueSupplier, collector)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable collectInto(final U initialValue, BiConsumer collector) { Objects.requireNonNull(initialValue, "initialValue is null"); return collect(new Supplier() { @Override public U get() { return initialValue; } }, collector); } public final Observable compose(Function, ? extends ObservableConsumable> convert) { return wrap(to(convert)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable concatMap(Function> mapper) { return concatMap(mapper, 2); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable concatMap(Function> mapper, int prefetch) { Objects.requireNonNull(mapper, "mapper is null"); if (prefetch <= 0) { throw new IllegalArgumentException("prefetch > 0 required but it was " + prefetch); } return lift(new NbpOperatorConcatMap(mapper, prefetch)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable concatMapIterable(final Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return concatMap(new Function>() { @Override public Observable apply(T v) { return fromIterable(mapper.apply(v)); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable concatMapIterable(final Function> mapper, int prefetch) { return concatMap(new Function>() { @Override public Observable apply(T v) { return fromIterable(mapper.apply(v)); } }, prefetch); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable concatWith(ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return concat(this, other); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable contains(final Object o) { Objects.requireNonNull(o, "o is null"); return any(new Predicate() { @Override public boolean test(T v) { return Objects.equals(v, o); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable count() { return lift(NbpOperatorCount.instance()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable debounce(Function> debounceSelector) { Objects.requireNonNull(debounceSelector, "debounceSelector is null"); return lift(new NbpOperatorDebounce(debounceSelector)); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable debounce(long timeout, TimeUnit unit) { return debounce(timeout, unit, Schedulers.computation()); } @BackpressureSupport(BackpressureKind.ERROR) @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable debounce(long timeout, TimeUnit unit, Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorDebounceTimed(timeout, unit, scheduler)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable defaultIfEmpty(T value) { Objects.requireNonNull(value, "value is null"); return switchIfEmpty(just(value)); } @SchedulerSupport(SchedulerSupport.NONE) // TODO a more efficient implementation if necessary public final Observable delay(final Function> itemDelay) { Objects.requireNonNull(itemDelay, "itemDelay is null"); return flatMap(new Function>() { @Override public Observable apply(final T v) { return new NbpOperatorTake(itemDelay.apply(v), 1).map(new Function() { @Override public T apply(U u) { return v; } }).defaultIfEmpty(v); } }); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable delay(long delay, TimeUnit unit) { return delay(delay, unit, Schedulers.computation(), false); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable delay(long delay, TimeUnit unit, boolean delayError) { return delay(delay, unit, Schedulers.computation(), delayError); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable delay(long delay, TimeUnit unit, Scheduler scheduler) { return delay(delay, unit, scheduler, false); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable delay(long delay, TimeUnit unit, Scheduler scheduler, boolean delayError) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorDelay(delay, unit, scheduler, delayError)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable delay(Supplier> delaySupplier, Function> itemDelay) { return delaySubscription(delaySupplier).delay(itemDelay); } /** * Returns an Observable that delays the subscription to this Observable * until the other Observable emits an element or completes normally. *

*

*
Backpressure:
*
The operator forwards the backpressure requests to this Observable once * the subscription happens and requests Long.MAX_VALUE from the other Observable
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the other Observable, irrelevant * @param other the other Observable that should trigger the subscription * to this Observable. * @return an Observable that delays the subscription to this Observable * until the other Observable emits an element or completes normally. */ @Experimental public final Observable delaySubscription(ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return (new ObservableDelaySubscriptionOther(this, other)); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable delaySubscription(long delay, TimeUnit unit) { return delaySubscription(delay, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) // TODO a more efficient implementation if necessary public final Observable delaySubscription(long delay, TimeUnit unit, Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return timer(delay, unit, scheduler).flatMap(new Function>() { @Override public Observable apply(Long v) { return Observable.this; } }); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public final Observable delaySubscription(final Supplier> delaySupplier) { Objects.requireNonNull(delaySupplier, "delaySupplier is null"); return fromCallable(new Callable>() { @Override public ObservableConsumable call() throws Exception { return delaySupplier.get(); } }) .flatMap((Function)Functions.identity()) .take(1) .cast(Object.class) .defaultIfEmpty(OBJECT) .flatMap(new Function>() { @Override public Observable apply(Object v) { return Observable.this; } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable dematerialize() { @SuppressWarnings("unchecked") Observable>> m = (Observable>>)this; return m.lift(NbpOperatorDematerialize.instance()); } @SuppressWarnings({ "rawtypes", "unchecked" }) @SchedulerSupport(SchedulerSupport.NONE) public final Observable distinct() { return distinct((Function)Functions.identity(), new Supplier>() { @Override public Collection get() { return new HashSet(); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable distinct(Function keySelector) { return distinct(keySelector, new Supplier>() { @Override public Collection get() { return new HashSet(); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable distinct(Function keySelector, Supplier> collectionSupplier) { Objects.requireNonNull(keySelector, "keySelector is null"); Objects.requireNonNull(collectionSupplier, "collectionSupplier is null"); return lift(NbpOperatorDistinct.withCollection(keySelector, collectionSupplier)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable distinctUntilChanged() { return lift(NbpOperatorDistinct.untilChanged()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable distinctUntilChanged(Function keySelector) { Objects.requireNonNull(keySelector, "keySelector is null"); return lift(NbpOperatorDistinct.untilChanged(keySelector)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable doOnCancel(Runnable onCancel) { return doOnLifecycle(Functions.emptyConsumer(), onCancel); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable doOnComplete(Runnable onComplete) { return doOnEach(Functions.emptyConsumer(), Functions.emptyConsumer(), onComplete, Functions.emptyRunnable()); } @SchedulerSupport(SchedulerSupport.NONE) private Observable doOnEach(Consumer onNext, Consumer onError, Runnable onComplete, Runnable onAfterTerminate) { Objects.requireNonNull(onNext, "onNext is null"); Objects.requireNonNull(onError, "onError is null"); Objects.requireNonNull(onComplete, "onComplete is null"); Objects.requireNonNull(onAfterTerminate, "onAfterTerminate is null"); return lift(new NbpOperatorDoOnEach(onNext, onError, onComplete, onAfterTerminate)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable doOnEach(final Consumer>> consumer) { Objects.requireNonNull(consumer, "consumer is null"); return doOnEach( new Consumer() { @Override public void accept(T v) { consumer.accept(Try.ofValue(Optional.of(v))); } }, new Consumer() { @Override public void accept(Throwable e) { consumer.accept(Try.>ofError(e)); } }, new Runnable() { @Override public void run() { consumer.accept(Try.ofValue(Optional.empty())); } }, Functions.emptyRunnable() ); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable doOnEach(final Observer observer) { Objects.requireNonNull(observer, "observer is null"); return doOnEach(new Consumer() { @Override public void accept(T v) { observer.onNext(v); } }, new Consumer() { @Override public void accept(Throwable e) { observer.onError(e); } }, new Runnable() { @Override public void run() { observer.onComplete(); } }, Functions.emptyRunnable()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable doOnError(Consumer onError) { return doOnEach(Functions.emptyConsumer(), onError, Functions.emptyRunnable(), Functions.emptyRunnable()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable doOnLifecycle(final Consumer onSubscribe, final Runnable onCancel) { Objects.requireNonNull(onSubscribe, "onSubscribe is null"); Objects.requireNonNull(onCancel, "onCancel is null"); return lift(new NbpOperator() { @Override public Observer apply(Observer s) { return new NbpSubscriptionLambdaSubscriber(s, onSubscribe, onCancel); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable doOnNext(Consumer onNext) { return doOnEach(onNext, Functions.emptyConsumer(), Functions.emptyRunnable(), Functions.emptyRunnable()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable doOnSubscribe(Consumer onSubscribe) { return doOnLifecycle(onSubscribe, Functions.emptyRunnable()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable doOnTerminate(final Runnable onTerminate) { return doOnEach(Functions.emptyConsumer(), new Consumer() { @Override public void accept(Throwable e) { onTerminate.run(); } }, onTerminate, Functions.emptyRunnable()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable elementAt(long index) { if (index < 0) { throw new IndexOutOfBoundsException("index >= 0 required but it was " + index); } return lift(new NbpOperatorElementAt(index, null)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable elementAt(long index, T defaultValue) { if (index < 0) { throw new IndexOutOfBoundsException("index >= 0 required but it was " + index); } Objects.requireNonNull(defaultValue, "defaultValue is null"); return lift(new NbpOperatorElementAt(index, defaultValue)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public final Observable endWith(Iterable values) { return concatArray(this, fromIterable(values)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public final Observable endWith(ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return concatArray(this, other); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public final Observable endWith(T value) { Objects.requireNonNull(value, "value is null"); return concatArray(this, just(value)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public final Observable endWithArray(T... values) { Observable fromArray = fromArray(values); if (fromArray == empty()) { return this; } return concatArray(this, fromArray); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable filter(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorFilter(predicate)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable finallyDo(Runnable onFinally) { return doOnEach(Functions.emptyConsumer(), Functions.emptyConsumer(), Functions.emptyRunnable(), onFinally); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable first() { return take(1).single(); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable first(T defaultValue) { return take(1).single(defaultValue); } public final Observable flatMap(Function> mapper) { return flatMap(mapper, false); } public final Observable flatMap(Function> mapper, boolean delayError) { return flatMap(mapper, delayError, Integer.MAX_VALUE); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap(Function> mapper, boolean delayErrors, int maxConcurrency) { return flatMap(mapper, delayErrors, maxConcurrency, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap(Function> mapper, boolean delayErrors, int maxConcurrency, int bufferSize) { Objects.requireNonNull(mapper, "mapper is null"); if (maxConcurrency <= 0) { throw new IllegalArgumentException("maxConcurrency > 0 required but it was " + maxConcurrency); } validateBufferSize(bufferSize); if (this instanceof ObservableJust) { ObservableJust scalar = (ObservableJust) this; return (scalar.scalarFlatMap(mapper)); } return lift(new NbpOperatorFlatMap(mapper, delayErrors, maxConcurrency, bufferSize)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap( Function> onNextMapper, Function> onErrorMapper, Supplier> onCompleteSupplier) { Objects.requireNonNull(onNextMapper, "onNextMapper is null"); Objects.requireNonNull(onErrorMapper, "onErrorMapper is null"); Objects.requireNonNull(onCompleteSupplier, "onCompleteSupplier is null"); return merge(lift(new NbpOperatorMapNotification(onNextMapper, onErrorMapper, onCompleteSupplier))); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap( Function> onNextMapper, Function> onErrorMapper, Supplier> onCompleteSupplier, int maxConcurrency) { Objects.requireNonNull(onNextMapper, "onNextMapper is null"); Objects.requireNonNull(onErrorMapper, "onErrorMapper is null"); Objects.requireNonNull(onCompleteSupplier, "onCompleteSupplier is null"); return merge(lift(new NbpOperatorMapNotification(onNextMapper, onErrorMapper, onCompleteSupplier)), maxConcurrency); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap(Function> mapper, int maxConcurrency) { return flatMap(mapper, false, maxConcurrency, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap(Function> mapper, BiFunction resultSelector) { return flatMap(mapper, resultSelector, false, bufferSize(), bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap(Function> mapper, BiFunction combiner, boolean delayError) { return flatMap(mapper, combiner, delayError, bufferSize(), bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap(Function> mapper, BiFunction combiner, boolean delayError, int maxConcurrency) { return flatMap(mapper, combiner, delayError, maxConcurrency, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap(final Function> mapper, final BiFunction combiner, boolean delayError, int maxConcurrency, int bufferSize) { Objects.requireNonNull(mapper, "mapper is null"); Objects.requireNonNull(combiner, "combiner is null"); return flatMap(new Function>() { @Override public Observable apply(final T t) { @SuppressWarnings("unchecked") Observable u = (Observable)mapper.apply(t); return u.map(new Function() { @Override public R apply(U w) { return combiner.apply(t, w); } }); } }, delayError, maxConcurrency, bufferSize); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMap(Function> mapper, BiFunction combiner, int maxConcurrency) { return flatMap(mapper, combiner, false, maxConcurrency, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMapIterable(final Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return flatMap(new Function>() { @Override public Observable apply(T v) { return fromIterable(mapper.apply(v)); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMapIterable(final Function> mapper, BiFunction resultSelector) { return flatMap(new Function>() { @Override public Observable apply(T t) { return fromIterable(mapper.apply(t)); } }, resultSelector, false, bufferSize(), bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMapIterable(final Function> mapper, int bufferSize) { return flatMap(new Function>() { @Override public Observable apply(T v) { return fromIterable(mapper.apply(v)); } }, false, bufferSize); } @SchedulerSupport(SchedulerSupport.NONE) public final Disposable forEach(Consumer onNext) { return subscribe(onNext); } @SchedulerSupport(SchedulerSupport.NONE) public final Disposable forEachWhile(Predicate onNext) { return forEachWhile(onNext, RxJavaPlugins.errorConsumer(), Functions.emptyRunnable()); } @SchedulerSupport(SchedulerSupport.NONE) public final Disposable forEachWhile(Predicate onNext, Consumer onError) { return forEachWhile(onNext, onError, Functions.emptyRunnable()); } @SchedulerSupport(SchedulerSupport.NONE) public final Disposable forEachWhile(final Predicate onNext, Consumer onError, final Runnable onComplete) { Objects.requireNonNull(onNext, "onNext is null"); Objects.requireNonNull(onError, "onError is null"); Objects.requireNonNull(onComplete, "onComplete is null"); final AtomicReference subscription = new AtomicReference(); return subscribe(new Consumer() { @Override public void accept(T v) { if (!onNext.test(v)) { subscription.get().dispose(); onComplete.run(); } } }, onError, onComplete, new Consumer() { @Override public void accept(Disposable s) { subscription.lazySet(s); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final List getList() { final List result = new ArrayList(); final Throwable[] error = { null }; final CountDownLatch cdl = new CountDownLatch(1); subscribe(new Observer() { @Override public void onComplete() { cdl.countDown(); } @Override public void onError(Throwable e) { error[0] = e; cdl.countDown(); } @Override public void onNext(T value) { result.add(value); } @Override public void onSubscribe(Disposable d) { } }); if (cdl.getCount() != 0) { try { cdl.await(); } catch (InterruptedException ex) { throw Exceptions.propagate(ex); } } Throwable e = error[0]; if (e != null) { throw Exceptions.propagate(e); } return result; } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public final Observable> groupBy(Function keySelector) { return groupBy(keySelector, (Function)Functions.identity(), false, bufferSize()); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerSupport.NONE) public final Observable> groupBy(Function keySelector, boolean delayError) { return groupBy(keySelector, (Function)Functions.identity(), delayError, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> groupBy(Function keySelector, Function valueSelector) { return groupBy(keySelector, valueSelector, false, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> groupBy(Function keySelector, Function valueSelector, boolean delayError) { return groupBy(keySelector, valueSelector, false, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> groupBy(Function keySelector, Function valueSelector, boolean delayError, int bufferSize) { Objects.requireNonNull(keySelector, "keySelector is null"); Objects.requireNonNull(valueSelector, "valueSelector is null"); validateBufferSize(bufferSize); return lift(new NbpOperatorGroupBy(keySelector, valueSelector, bufferSize, delayError)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable ignoreElements() { return lift(NbpOperatorIgnoreElements.instance()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable isEmpty() { return all(new Predicate() { @Override public boolean test(T v) { return false; } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable last() { return takeLast(1).single(); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable last(T defaultValue) { return takeLast(1).single(defaultValue); } public final Observable lift(NbpOperator onLift) { Objects.requireNonNull(onLift, "onLift is null"); return (new ObservableLift(this, onLift)); } public final Observable map(Function mapper) { Objects.requireNonNull(mapper, "mapper is null"); return lift(new NbpOperatorMap(mapper)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable>> materialize() { return lift(NbpOperatorMaterialize.instance()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable mergeWith(ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return merge(this, other); } @SchedulerSupport(SchedulerSupport.NONE) @Deprecated public final Observable> nest() { return just(this); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable observeOn(Scheduler scheduler) { return observeOn(scheduler, false, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable observeOn(Scheduler scheduler, boolean delayError) { return observeOn(scheduler, delayError, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable observeOn(Scheduler scheduler, boolean delayError, int bufferSize) { Objects.requireNonNull(scheduler, "scheduler is null"); validateBufferSize(bufferSize); return new NbpOperatorObserveOn(this, scheduler, delayError, bufferSize); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable ofType(final Class clazz) { Objects.requireNonNull(clazz, "clazz is null"); return filter(new Predicate() { @Override public boolean test(T v) { return clazz.isInstance(v); } }).cast(clazz); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable onErrorResumeNext(Function> resumeFunction) { Objects.requireNonNull(resumeFunction, "resumeFunction is null"); return lift(new NbpOperatorOnErrorNext(resumeFunction, false)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable onErrorResumeNext(final ObservableConsumable next) { Objects.requireNonNull(next, "next is null"); return onErrorResumeNext(new Function>() { @Override public ObservableConsumable apply(Throwable e) { return next; } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable onErrorReturn(Function valueSupplier) { Objects.requireNonNull(valueSupplier, "valueSupplier is null"); return lift(new NbpOperatorOnErrorReturn(valueSupplier)); } // TODO would result in ambiguity with onErrorReturn(Function) @SchedulerSupport(SchedulerSupport.NONE) public final Observable onErrorReturnValue(final T value) { Objects.requireNonNull(value, "value is null"); return onErrorReturn(new Function() { @Override public T apply(Throwable e) { return value; } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable onExceptionResumeNext(final ObservableConsumable next) { Objects.requireNonNull(next, "next is null"); return lift(new NbpOperatorOnErrorNext(new Function>() { @Override public ObservableConsumable apply(Throwable e) { return next; } }, true)); } @SchedulerSupport(SchedulerSupport.NONE) public final ConnectableObservable publish() { return publish(bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable publish(Function, ? extends ObservableConsumable> selector) { return publish(selector, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable publish(Function, ? extends ObservableConsumable> selector, int bufferSize) { validateBufferSize(bufferSize); Objects.requireNonNull(selector, "selector is null"); return NbpOperatorPublish.create(this, selector, bufferSize); } @SchedulerSupport(SchedulerSupport.NONE) public final ConnectableObservable publish(int bufferSize) { validateBufferSize(bufferSize); return NbpOperatorPublish.create(this, bufferSize); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable reduce(BiFunction reducer) { return scan(reducer).last(); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable reduce(R seed, BiFunction reducer) { return scan(seed, reducer).last(); } // Naming note, a plain scan would cause ambiguity with the value-seeded version @SchedulerSupport(SchedulerSupport.NONE) public final Observable reduceWith(Supplier seedSupplier, BiFunction reducer) { return scanWith(seedSupplier, reducer).last(); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable repeat() { return repeat(Long.MAX_VALUE); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable repeat(long times) { if (times < 0) { throw new IllegalArgumentException("times >= 0 required but it was " + times); } if (times == 0) { return empty(); } return (new ObservableRepeat(this, times)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable repeatUntil(BooleanSupplier stop) { Objects.requireNonNull(stop, "stop is null"); return (new ObservableRepeatUntil(this, stop)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable repeatWhen(final Function, ? extends ObservableConsumable> handler) { Objects.requireNonNull(handler, "handler is null"); Function>>, ObservableConsumable> f = new Function>>, ObservableConsumable>() { @Override public ObservableConsumable apply(Observable>> no) { return handler.apply(no.map(new Function>, Object>() { @Override public Object apply(Try> v) { return 0; } })); } } ; return (new ObservableRedo(this, f)); } @SchedulerSupport(SchedulerSupport.NONE) public final ConnectableObservable replay() { return NbpOperatorReplay.createFrom(this); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable replay(Function, ? extends ObservableConsumable> selector) { Objects.requireNonNull(selector, "selector is null"); return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public ConnectableObservable get() { return replay(); } }, selector); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable replay(Function, ? extends ObservableConsumable> selector, final int bufferSize) { Objects.requireNonNull(selector, "selector is null"); return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public ConnectableObservable get() { return replay(bufferSize); } }, selector); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable replay(Function, ? extends ObservableConsumable> selector, int bufferSize, long time, TimeUnit unit) { return replay(selector, bufferSize, time, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable replay(Function, ? extends ObservableConsumable> selector, final int bufferSize, final long time, final TimeUnit unit, final Scheduler scheduler) { if (bufferSize < 0) { throw new IllegalArgumentException("bufferSize < 0"); } Objects.requireNonNull(selector, "selector is null"); return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public ConnectableObservable get() { return replay(bufferSize, time, unit, scheduler); } }, selector); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable replay(final Function, ? extends ObservableConsumable> selector, final int bufferSize, final Scheduler scheduler) { return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public ConnectableObservable get() { return replay(bufferSize); } }, new Function, Observable>() { @Override public Observable apply(Observable t) { return new NbpOperatorObserveOn(selector.apply(t), scheduler, false, bufferSize()); } }); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable replay(Function, ? extends ObservableConsumable> selector, long time, TimeUnit unit) { return replay(selector, time, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable replay(Function, ? extends ObservableConsumable> selector, final long time, final TimeUnit unit, final Scheduler scheduler) { Objects.requireNonNull(selector, "selector is null"); Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public ConnectableObservable get() { return replay(time, unit, scheduler); } }, selector); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable replay(final Function, ? extends ObservableConsumable> selector, final Scheduler scheduler) { Objects.requireNonNull(selector, "selector is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public ConnectableObservable get() { return replay(); } }, new Function, Observable>() { @Override public Observable apply(Observable t) { return new NbpOperatorObserveOn(selector.apply(t), scheduler, false, bufferSize()); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final ConnectableObservable replay(final int bufferSize) { return NbpOperatorReplay.create(this, bufferSize); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final ConnectableObservable replay(int bufferSize, long time, TimeUnit unit) { return replay(bufferSize, time, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final ConnectableObservable replay(final int bufferSize, final long time, final TimeUnit unit, final Scheduler scheduler) { if (bufferSize < 0) { throw new IllegalArgumentException("bufferSize < 0"); } Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return NbpOperatorReplay.create(this, time, unit, scheduler, bufferSize); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final ConnectableObservable replay(final int bufferSize, final Scheduler scheduler) { return NbpOperatorReplay.observeOn(replay(bufferSize), scheduler); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final ConnectableObservable replay(long time, TimeUnit unit) { return replay(time, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final ConnectableObservable replay(final long time, final TimeUnit unit, final Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return NbpOperatorReplay.create(this, time, unit, scheduler); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final ConnectableObservable replay(final Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return NbpOperatorReplay.observeOn(replay(), scheduler); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable retry() { return retry(Long.MAX_VALUE, Functions.alwaysTrue()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable retry(BiPredicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return (new ObservableRetryBiPredicate(this, predicate)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable retry(long times) { return retry(times, Functions.alwaysTrue()); } // Retries at most times or until the predicate returns false, whichever happens first @SchedulerSupport(SchedulerSupport.NONE) public final Observable retry(long times, Predicate predicate) { if (times < 0) { throw new IllegalArgumentException("times >= 0 required but it was " + times); } Objects.requireNonNull(predicate, "predicate is null"); return (new ObservableRetryPredicate(this, times, predicate)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable retry(Predicate predicate) { return retry(Long.MAX_VALUE, predicate); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable retryUntil(final BooleanSupplier stop) { Objects.requireNonNull(stop, "stop is null"); return retry(Long.MAX_VALUE, new Predicate() { @Override public boolean test(Throwable e) { return !stop.getAsBoolean(); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable retryWhen( final Function, ? extends ObservableConsumable> handler) { Objects.requireNonNull(handler, "handler is null"); Function>>, ObservableConsumable> f = new Function>>, ObservableConsumable>() { @Override public ObservableConsumable apply(Observable>> no) { return handler.apply(no .takeWhile(new Predicate>>() { @Override public boolean test(Try> e) { return e.hasError(); } }) .map(new Function>, Throwable>() { @Override public Throwable apply(Try> t) { return t.error(); } }) ); } } ; return (new ObservableRedo(this, f)); } // TODO decide if safe subscription or unsafe should be the default @SchedulerSupport(SchedulerSupport.NONE) public final void safeSubscribe(Observer s) { Objects.requireNonNull(s, "s is null"); if (s instanceof SafeObserver) { subscribe(s); } else { subscribe(new SafeObserver(s)); } } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable sample(long period, TimeUnit unit) { return sample(period, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable sample(long period, TimeUnit unit, Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorSampleTimed(period, unit, scheduler)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable sample(ObservableConsumable sampler) { Objects.requireNonNull(sampler, "sampler is null"); return lift(new NbpOperatorSampleWithObservable(sampler)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable scan(BiFunction accumulator) { Objects.requireNonNull(accumulator, "accumulator is null"); return lift(new NbpOperatorScan(accumulator)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable scan(final R seed, BiFunction accumulator) { Objects.requireNonNull(seed, "seed is null"); return scanWith(new Supplier() { @Override public R get() { return seed; } }, accumulator); } // Naming note, a plain scan would cause ambiguity with the value-seeded version @SchedulerSupport(SchedulerSupport.NONE) public final Observable scanWith(Supplier seedSupplier, BiFunction accumulator) { Objects.requireNonNull(seedSupplier, "seedSupplier is null"); Objects.requireNonNull(accumulator, "accumulator is null"); return lift(new NbpOperatorScanSeed(seedSupplier, accumulator)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable serialize() { return lift(new NbpOperator() { @Override public Observer apply(Observer s) { return new SerializedObserver(s); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable share() { return publish().refCount(); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable single() { return lift(NbpOperatorSingle.instanceNoDefault()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable single(T defaultValue) { Objects.requireNonNull(defaultValue, "defaultValue is null"); return lift(new NbpOperatorSingle(defaultValue)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable skip(long n) { // if (n < 0) { // throw new IllegalArgumentException("n >= 0 required but it was " + n); // } else // FIXME negative skip allowed?! if (n <= 0) { return this; } return lift(new NbpOperatorSkip(n)); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable skip(long time, TimeUnit unit, Scheduler scheduler) { // TODO consider inlining this behavior return skipUntil(timer(time, unit, scheduler)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable skipLast(int n) { if (n < 0) { throw new IndexOutOfBoundsException("n >= 0 required but it was " + n); } else if (n == 0) { return this; } return lift(new NbpOperatorSkipLast(n)); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable skipLast(long time, TimeUnit unit) { return skipLast(time, unit, Schedulers.trampoline(), false, bufferSize()); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable skipLast(long time, TimeUnit unit, boolean delayError) { return skipLast(time, unit, Schedulers.trampoline(), delayError, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable skipLast(long time, TimeUnit unit, Scheduler scheduler) { return skipLast(time, unit, scheduler, false, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable skipLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError) { return skipLast(time, unit, scheduler, delayError, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable skipLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError, int bufferSize) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); validateBufferSize(bufferSize); // the internal buffer holds pairs of (timestamp, value) so double the default buffer size int s = bufferSize << 1; return lift(new NbpOperatorSkipLastTimed(time, unit, scheduler, s, delayError)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable skipUntil(ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return lift(new NbpOperatorSkipUntil(other)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable skipWhile(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorSkipWhile(predicate)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public final Observable startWith(Iterable values) { return concatArray(fromIterable(values), this); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public final Observable startWith(ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return concatArray(other, this); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public final Observable startWith(T value) { Objects.requireNonNull(value, "value is null"); return concatArray(just(value), this); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerSupport.NONE) public final Observable startWithArray(T... values) { Observable fromArray = fromArray(values); if (fromArray == empty()) { return this; } return concatArray(fromArray, this); } @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe() { return subscribe(Functions.emptyConsumer(), RxJavaPlugins.errorConsumer(), Functions.emptyRunnable(), Functions.emptyConsumer()); } @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(Consumer onNext) { return subscribe(onNext, RxJavaPlugins.errorConsumer(), Functions.emptyRunnable(), Functions.emptyConsumer()); } @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(Consumer onNext, Consumer onError) { return subscribe(onNext, onError, Functions.emptyRunnable(), Functions.emptyConsumer()); } @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(Consumer onNext, Consumer onError, Runnable onComplete) { return subscribe(onNext, onError, onComplete, Functions.emptyConsumer()); } @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(Consumer onNext, Consumer onError, Runnable onComplete, Consumer onSubscribe) { Objects.requireNonNull(onNext, "onNext is null"); Objects.requireNonNull(onError, "onError is null"); Objects.requireNonNull(onComplete, "onComplete is null"); Objects.requireNonNull(onSubscribe, "onSubscribe is null"); NbpLambdaSubscriber ls = new NbpLambdaSubscriber(onNext, onError, onComplete, onSubscribe); unsafeSubscribe(ls); return ls; } @Override public final void subscribe(Observer observer) { Objects.requireNonNull(observer, "observer is null"); // TODO plugin wrappings subscribeActual(observer); } protected abstract void subscribeActual(Observer observer); @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable subscribeOn(Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return (new ObservableSubscribeOn(this, scheduler)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable switchIfEmpty(ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return lift(new NbpOperatorSwitchIfEmpty(other)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable switchMap(Function> mapper) { return switchMap(mapper, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable switchMap(Function> mapper, int bufferSize) { Objects.requireNonNull(mapper, "mapper is null"); validateBufferSize(bufferSize); return lift(new NbpOperatorSwitchMap(mapper, bufferSize)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable take(long n) { if (n < 0) { throw new IllegalArgumentException("n >= required but it was " + n); } else if (n == 0) { // FIXME may want to subscribe an cancel immediately // return lift(s -> CancelledSubscriber.INSTANCE); return empty(); } return new NbpOperatorTake(this, n); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable take(long time, TimeUnit unit, Scheduler scheduler) { // TODO consider inlining this behavior return takeUntil(timer(time, unit, scheduler)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable takeFirst(Predicate predicate) { return filter(predicate).take(1); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable takeLast(int n) { if (n < 0) { throw new IndexOutOfBoundsException("n >= required but it was " + n); } else if (n == 0) { return ignoreElements(); } else if (n == 1) { return lift(NbpOperatorTakeLastOne.instance()); } return lift(new NbpOperatorTakeLast(n)); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable takeLast(long count, long time, TimeUnit unit) { return takeLast(count, time, unit, Schedulers.trampoline(), false, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable takeLast(long count, long time, TimeUnit unit, Scheduler scheduler) { return takeLast(count, time, unit, scheduler, false, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable takeLast(long count, long time, TimeUnit unit, Scheduler scheduler, boolean delayError, int bufferSize) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); validateBufferSize(bufferSize); if (count < 0) { throw new IndexOutOfBoundsException("count >= 0 required but it was " + count); } return lift(new NbpOperatorTakeLastTimed(count, time, unit, scheduler, bufferSize, delayError)); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable takeLast(long time, TimeUnit unit) { return takeLast(time, unit, Schedulers.trampoline(), false, bufferSize()); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable takeLast(long time, TimeUnit unit, boolean delayError) { return takeLast(time, unit, Schedulers.trampoline(), delayError, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable takeLast(long time, TimeUnit unit, Scheduler scheduler) { return takeLast(time, unit, scheduler, false, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable takeLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError) { return takeLast(time, unit, scheduler, delayError, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable takeLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError, int bufferSize) { return takeLast(Long.MAX_VALUE, time, unit, scheduler, delayError, bufferSize); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> takeLastBuffer(int count) { return takeLast(count).toList(); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable> takeLastBuffer(int count, long time, TimeUnit unit) { return takeLast(count, time, unit).toList(); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> takeLastBuffer(int count, long time, TimeUnit unit, Scheduler scheduler) { return takeLast(count, time, unit, scheduler).toList(); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable> takeLastBuffer(long time, TimeUnit unit) { return takeLast(time, unit).toList(); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> takeLastBuffer(long time, TimeUnit unit, Scheduler scheduler) { return takeLast(time, unit, scheduler).toList(); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable takeUntil(ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return lift(new NbpOperatorTakeUntil(other)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable takeUntil(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorTakeUntilPredicate(predicate)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable takeWhile(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorTakeWhile(predicate)); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable throttleFirst(long windowDuration, TimeUnit unit) { return throttleFirst(windowDuration, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable throttleFirst(long skipDuration, TimeUnit unit, Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorThrottleFirstTimed(skipDuration, unit, scheduler)); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable throttleLast(long intervalDuration, TimeUnit unit) { return sample(intervalDuration, unit); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable throttleLast(long intervalDuration, TimeUnit unit, Scheduler scheduler) { return sample(intervalDuration, unit, scheduler); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable throttleWithTimeout(long timeout, TimeUnit unit) { return debounce(timeout, unit); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable throttleWithTimeout(long timeout, TimeUnit unit, Scheduler scheduler) { return debounce(timeout, unit, scheduler); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable> timeInterval() { return timeInterval(TimeUnit.MILLISECONDS, Schedulers.trampoline()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> timeInterval(Scheduler scheduler) { return timeInterval(TimeUnit.MILLISECONDS, scheduler); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable> timeInterval(TimeUnit unit) { return timeInterval(unit, Schedulers.trampoline()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> timeInterval(TimeUnit unit, Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorTimeInterval(unit, scheduler)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable timeout(Function> timeoutSelector) { return timeout0(null, timeoutSelector, null); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable timeout(Function> timeoutSelector, ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return timeout0(null, timeoutSelector, other); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable timeout(long timeout, TimeUnit timeUnit) { return timeout0(timeout, timeUnit, null, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable timeout(long timeout, TimeUnit timeUnit, ObservableConsumable other) { Objects.requireNonNull(other, "other is null"); return timeout0(timeout, timeUnit, other, Schedulers.computation()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable timeout(long timeout, TimeUnit timeUnit, ObservableConsumable other, Scheduler scheduler) { Objects.requireNonNull(other, "other is null"); return timeout0(timeout, timeUnit, other, scheduler); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable timeout(long timeout, TimeUnit timeUnit, Scheduler scheduler) { return timeout0(timeout, timeUnit, null, scheduler); } public final Observable timeout(Supplier> firstTimeoutSelector, Function> timeoutSelector) { Objects.requireNonNull(firstTimeoutSelector, "firstTimeoutSelector is null"); return timeout0(firstTimeoutSelector, timeoutSelector, null); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable timeout( Supplier> firstTimeoutSelector, Function> timeoutSelector, ObservableConsumable other) { Objects.requireNonNull(firstTimeoutSelector, "firstTimeoutSelector is null"); Objects.requireNonNull(other, "other is null"); return timeout0(firstTimeoutSelector, timeoutSelector, other); } private Observable timeout0(long timeout, TimeUnit timeUnit, ObservableConsumable other, Scheduler scheduler) { Objects.requireNonNull(timeUnit, "timeUnit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorTimeoutTimed(timeout, timeUnit, scheduler, other)); } private Observable timeout0( Supplier> firstTimeoutSelector, Function> timeoutSelector, ObservableConsumable other) { Objects.requireNonNull(timeoutSelector, "timeoutSelector is null"); return lift(new NbpOperatorTimeout(firstTimeoutSelector, timeoutSelector, other)); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable> timestamp() { return timestamp(TimeUnit.MILLISECONDS, Schedulers.trampoline()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> timestamp(Scheduler scheduler) { return timestamp(TimeUnit.MILLISECONDS, scheduler); } @SchedulerSupport(SchedulerSupport.TRAMPOLINE) public final Observable> timestamp(TimeUnit unit) { return timestamp(unit, Schedulers.trampoline()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> timestamp(final TimeUnit unit, final Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return map(new Function>() { @Override public Timed apply(T v) { return new Timed(v, scheduler.now(unit), unit); } }); } public final R to(Function, R> convert) { return convert.apply(this); } @SchedulerSupport(SchedulerSupport.NONE) public final BlockingObservable toBlocking() { return BlockingObservable.from(this); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> toList() { return lift(NbpOperatorToList.defaultInstance()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> toList(final int capacityHint) { if (capacityHint <= 0) { throw new IllegalArgumentException("capacityHint > 0 required but it was " + capacityHint); } return lift(new NbpOperatorToList>(new Supplier>() { @Override public List get() { return new ArrayList(capacityHint); } })); } @SchedulerSupport(SchedulerSupport.NONE) public final > Observable toList(Supplier collectionSupplier) { Objects.requireNonNull(collectionSupplier, "collectionSupplier is null"); return lift(new NbpOperatorToList(collectionSupplier)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> toMap(final Function keySelector) { return collect(new Supplier>() { @Override public Map get() { return new HashMap(); } }, new BiConsumer, T>() { @Override public void accept(Map m, T t) { K key = keySelector.apply(t); m.put(key, t); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> toMap( final Function keySelector, final Function valueSelector) { Objects.requireNonNull(keySelector, "keySelector is null"); Objects.requireNonNull(valueSelector, "valueSelector is null"); return collect(new Supplier>() { @Override public Map get() { return new HashMap(); } }, new BiConsumer, T>() { @Override public void accept(Map m, T t) { K key = keySelector.apply(t); V value = valueSelector.apply(t); m.put(key, value); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> toMap( final Function keySelector, final Function valueSelector, Supplier> mapSupplier) { return collect(mapSupplier, new BiConsumer, T>() { @Override public void accept(Map m, T t) { K key = keySelector.apply(t); V value = valueSelector.apply(t); m.put(key, value); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable>> toMultimap(Function keySelector) { @SuppressWarnings({ "rawtypes", "unchecked" }) Function valueSelector = (Function)Functions.identity(); Supplier>> mapSupplier = new Supplier>>() { @Override public Map> get() { return new HashMap>(); } }; Function> collectionFactory = new Function>() { @Override public Collection apply(K k) { return new ArrayList(); } }; return toMultimap(keySelector, valueSelector, mapSupplier, collectionFactory); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable>> toMultimap(Function keySelector, Function valueSelector) { Supplier>> mapSupplier = new Supplier>>() { @Override public Map> get() { return new HashMap>(); } }; Function> collectionFactory = new Function>() { @Override public Collection apply(K k) { return new ArrayList(); } }; return toMultimap(keySelector, valueSelector, mapSupplier, collectionFactory); } @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public final Observable>> toMultimap( final Function keySelector, final Function valueSelector, final Supplier>> mapSupplier, final Function> collectionFactory) { Objects.requireNonNull(keySelector, "keySelector is null"); Objects.requireNonNull(valueSelector, "valueSelector is null"); Objects.requireNonNull(mapSupplier, "mapSupplier is null"); Objects.requireNonNull(collectionFactory, "collectionFactory is null"); return collect(mapSupplier, new BiConsumer>, T>() { @Override public void accept(Map> m, T t) { K key = keySelector.apply(t); Collection coll = m.get(key); if (coll == null) { coll = (Collection)collectionFactory.apply(key); m.put(key, coll); } V value = valueSelector.apply(t); coll.add(value); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable>> toMultimap( Function keySelector, Function valueSelector, Supplier>> mapSupplier ) { return toMultimap(keySelector, valueSelector, mapSupplier, new Function>() { @Override public Collection apply(K k) { return new ArrayList(); } }); } public final Flowable toFlowable(BackpressureStrategy strategy) { Flowable o = Flowable.create(new Publisher() { @Override public void subscribe(final Subscriber s) { Observable.this.subscribe(new Observer() { @Override public void onComplete() { s.onComplete(); } @Override public void onError(Throwable e) { s.onError(e); } @Override public void onNext(T value) { s.onNext(value); } @Override public void onSubscribe(final Disposable d) { s.onSubscribe(new Subscription() { @Override public void cancel() { d.dispose(); } @Override public void request(long n) { // no backpressure so nothing we can do about this } }); } }); } }); switch (strategy) { case BUFFER: return o.onBackpressureBuffer(); case DROP: return o.onBackpressureDrop(); case LATEST: return o.onBackpressureLatest(); default: return o; } } @SchedulerSupport(SchedulerSupport.NONE) public final Single toSingle() { return Single.create(new SingleConsumable() { @Override public void subscribe(final SingleSubscriber s) { Observable.this.subscribe(new Observer() { T last; @Override public void onSubscribe(Disposable d) { s.onSubscribe(d); } @Override public void onNext(T value) { last = value; } @Override public void onError(Throwable e) { s.onError(e); } @Override public void onComplete() { T v = last; last = null; if (v != null) { s.onSuccess(v); } else { s.onError(new NoSuchElementException()); } } }); } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> toSortedList() { return toSortedList(Functions.naturalOrder()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> toSortedList(final Comparator comparator) { Objects.requireNonNull(comparator, "comparator is null"); return toList().map(new Function, List>() { @Override public List apply(List v) { Collections.sort(v, comparator); return v; } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> toSortedList(final Comparator comparator, int capacityHint) { Objects.requireNonNull(comparator, "comparator is null"); return toList(capacityHint).map(new Function, List>() { @Override public List apply(List v) { Collections.sort(v, comparator); return v; } }); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> toSortedList(int capacityHint) { return toSortedList(Functions.naturalOrder(), capacityHint); } @SchedulerSupport(SchedulerSupport.NONE) // TODO decide if safe subscription or unsafe should be the default public final void unsafeSubscribe(Observer s) { Objects.requireNonNull(s, "s is null"); subscribe(s); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable unsubscribeOn(Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorUnsubscribeOn(scheduler)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> window(long count) { return window(count, count, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> window(long count, long skip) { return window(count, skip, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> window(long count, long skip, int bufferSize) { if (skip <= 0) { throw new IllegalArgumentException("skip > 0 required but it was " + skip); } if (count <= 0) { throw new IllegalArgumentException("count > 0 required but it was " + count); } validateBufferSize(bufferSize); return lift(new NbpOperatorWindow(count, skip, bufferSize)); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable> window(long timespan, long timeskip, TimeUnit unit) { return window(timespan, timeskip, unit, Schedulers.computation(), bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> window(long timespan, long timeskip, TimeUnit unit, Scheduler scheduler) { return window(timespan, timeskip, unit, scheduler, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> window(long timespan, long timeskip, TimeUnit unit, Scheduler scheduler, int bufferSize) { validateBufferSize(bufferSize); Objects.requireNonNull(scheduler, "scheduler is null"); Objects.requireNonNull(unit, "unit is null"); return lift(new NbpOperatorWindowTimed(timespan, timeskip, unit, scheduler, Long.MAX_VALUE, bufferSize, false)); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable> window(long timespan, TimeUnit unit) { return window(timespan, unit, Schedulers.computation(), Long.MAX_VALUE, false); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable> window(long timespan, TimeUnit unit, long count) { return window(timespan, unit, Schedulers.computation(), count, false); } @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Observable> window(long timespan, TimeUnit unit, long count, boolean restart) { return window(timespan, unit, Schedulers.computation(), count, restart); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> window(long timespan, TimeUnit unit, Scheduler scheduler) { return window(timespan, unit, scheduler, Long.MAX_VALUE, false); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> window(long timespan, TimeUnit unit, Scheduler scheduler, long count) { return window(timespan, unit, scheduler, count, false); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> window(long timespan, TimeUnit unit, Scheduler scheduler, long count, boolean restart) { return window(timespan, unit, scheduler, count, restart, bufferSize()); } @SchedulerSupport(SchedulerSupport.CUSTOM) public final Observable> window( long timespan, TimeUnit unit, Scheduler scheduler, long count, boolean restart, int bufferSize) { validateBufferSize(bufferSize); Objects.requireNonNull(scheduler, "scheduler is null"); Objects.requireNonNull(unit, "unit is null"); if (count <= 0) { throw new IllegalArgumentException("count > 0 required but it was " + count); } return lift(new NbpOperatorWindowTimed(timespan, timespan, unit, scheduler, count, bufferSize, restart)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> window(ObservableConsumable boundary) { return window(boundary, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> window(ObservableConsumable boundary, int bufferSize) { Objects.requireNonNull(boundary, "boundary is null"); return lift(new NbpOperatorWindowBoundary(boundary, bufferSize)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> window( ObservableConsumable windowOpen, Function> windowClose) { return window(windowOpen, windowClose, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> window( ObservableConsumable windowOpen, Function> windowClose, int bufferSize) { Objects.requireNonNull(windowOpen, "windowOpen is null"); Objects.requireNonNull(windowClose, "windowClose is null"); return lift(new NbpOperatorWindowBoundarySelector(windowOpen, windowClose, bufferSize)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> window(Supplier> boundary) { return window(boundary, bufferSize()); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable> window(Supplier> boundary, int bufferSize) { Objects.requireNonNull(boundary, "boundary is null"); return lift(new NbpOperatorWindowBoundarySupplier(boundary, bufferSize)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable withLatestFrom(ObservableConsumable other, BiFunction combiner) { Objects.requireNonNull(other, "other is null"); Objects.requireNonNull(combiner, "combiner is null"); return lift(new NbpOperatorWithLatestFrom(combiner, other)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable zipWith(Iterable other, BiFunction zipper) { Objects.requireNonNull(other, "other is null"); Objects.requireNonNull(zipper, "zipper is null"); return (new ObservableZipIterable(this, other, zipper)); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable zipWith(ObservableConsumable other, BiFunction zipper) { Objects.requireNonNull(other, "other is null"); return zip(this, other, zipper); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable zipWith(ObservableConsumable other, BiFunction zipper, boolean delayError) { return zip(this, other, zipper, delayError); } @SchedulerSupport(SchedulerSupport.NONE) public final Observable zipWith(ObservableConsumable other, BiFunction zipper, boolean delayError, int bufferSize) { return zip(this, other, zipper, delayError, bufferSize); } }