All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.akarnokd.rxjava2.NbpObservable Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC3
Show newest version
/**
 * Copyright 2015 David Karnok and 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 hu.akarnokd.rxjava2;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.Single.*;
import hu.akarnokd.rxjava2.annotations.*;
import hu.akarnokd.rxjava2.disposables.*;
import hu.akarnokd.rxjava2.functions.*;
import hu.akarnokd.rxjava2.internal.disposables.EmptyDisposable;
import hu.akarnokd.rxjava2.internal.functions.Functions;
import hu.akarnokd.rxjava2.internal.functions.Objects;
import hu.akarnokd.rxjava2.internal.operators.nbp.*;
import hu.akarnokd.rxjava2.internal.subscribers.nbp.*;
import hu.akarnokd.rxjava2.internal.util.Exceptions;
import hu.akarnokd.rxjava2.observables.nbp.*;
import hu.akarnokd.rxjava2.plugins.RxJavaPlugins;
import hu.akarnokd.rxjava2.schedulers.*;
import hu.akarnokd.rxjava2.subscribers.nbp.*;

/**
 * Observable for delivering a sequence of values without backpressure. 
 * @param 
 */
public class NbpObservable {

    public interface NbpOnSubscribe extends Consumer> {
        
    }
    
    public interface NbpOperator extends Function, NbpSubscriber> {
        
    }
    
    public interface NbpSubscriber {
        
        void onSubscribe(Disposable d);

        void onNext(T value);
        
        void onError(Throwable e);
        
        void onComplete();
        
    }
    
    public interface NbpTransformer extends Function, NbpObservable> {
        
    }
    
    /** An empty observable instance as there is no need to instantiate this more than once. */
    static final NbpObservable EMPTY = create(new NbpOnSubscribe() {
        @Override
        public void accept(NbpSubscriber s) {
            s.onSubscribe(EmptyDisposable.INSTANCE);
            s.onComplete();
        }
    });
    
    /** A never NbpObservable instance as there is no need to instantiate this more than once. */
    static final NbpObservable NEVER = create(new NbpOnSubscribe() {
        @Override
        public void accept(NbpSubscriber s) {
            s.onSubscribe(EmptyDisposable.INSTANCE);
        }
    });
    
    static final Object OBJECT = new Object();
    
    public static  NbpObservable amb(Iterable> sources) {
        Objects.requireNonNull(sources, "sources is null");
        return create(new NbpOnSubscribeAmb(null, sources));
    }
    
    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable amb(NbpObservable... sources) {
        Objects.requireNonNull(sources, "sources is null");
        int len = sources.length;
        if (len == 0) {
            return empty();
        } else
        if (len == 1) {
            return (NbpObservable)sources[0];
        }
        return create(new NbpOnSubscribeAmb(sources, null));
    }
    
    /**
     * Returns the default 'island' size or capacity-increment hint for unbounded buffers.
     * @return
     */
    static int bufferSize() {
        return Observable.bufferSize();
    }
    
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(Function combiner, boolean delayError, int bufferSize, NbpObservable... sources) {
        return combineLatest(sources, combiner, delayError, bufferSize);
    }

    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(Iterable> sources, Function combiner) {
        return combineLatest(sources, combiner, false, bufferSize());
    }
    
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(Iterable> sources, Function combiner, boolean delayError) {
        return combineLatest(sources, combiner, delayError, bufferSize());
    }
    
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable 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 create(new NbpOnSubscribeCombineLatest(null, sources, combiner, s, delayError));
    }

    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(NbpObservable[] sources, Function combiner) {
        return combineLatest(sources, combiner, false, bufferSize());
    }
    
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(NbpObservable[] sources, Function combiner, boolean delayError) {
        return combineLatest(sources, combiner, delayError, bufferSize());
    }
    
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(NbpObservable[] 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 create(new NbpOnSubscribeCombineLatest(sources, null, combiner, s, delayError));
    }
    
    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(
            NbpObservable p1, NbpObservable p2, 
            BiFunction combiner) {
        return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2);
    }
    
    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, 
            Function3 combiner) {
        return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3);
    }
    
    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            Function4 combiner) {
        return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4);
    }
    
    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5,
            Function5 combiner) {
        return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5);
    }
    
    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5, NbpObservable p6,
            Function6 combiner) {
        return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5, p6);
    }
    
    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5, NbpObservable p6,
            NbpObservable p7,
            Function7 combiner) {
        return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7);
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5, NbpObservable p6,
            NbpObservable p7, NbpObservable p8,
            Function8 combiner) {
        return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7, p8);
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable combineLatest(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5, NbpObservable p6,
            NbpObservable p7, NbpObservable p8,
            NbpObservable p9,
            Function9 combiner) {
        return combineLatest(Functions.toFunction(combiner), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7, p8, p9);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(int prefetch, Iterable> sources) {
        Objects.requireNonNull(sources, "sources is null");
        return fromIterable(sources).concatMap((Function)Functions.identity(), prefetch);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(Iterable> sources) {
        Objects.requireNonNull(sources, "sources is null");
        return fromIterable(sources).concatMap((Function)Functions.identity());
    }

    @SchedulerSupport(SchedulerKind.NONE)
    public static final  NbpObservable concat(NbpObservable> sources) {
        return concat(sources, bufferSize());
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @SchedulerSupport(SchedulerKind.NONE)
    public static final  NbpObservable concat(NbpObservable> sources, int bufferSize) {
        return sources.concatMap((Function)Functions.identity());
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(NbpObservable p1, NbpObservable p2) {
        return concatArray(p1, p2);
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(
            NbpObservable p1, NbpObservable p2,
            NbpObservable p3) {
        return concatArray(p1, p2, p3);
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(
            NbpObservable p1, NbpObservable p2,
            NbpObservable p3, NbpObservable p4) {
        return concatArray(p1, p2, p3, p4);
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5
    ) {
        return concatArray(p1, p2, p3, p4, p5);
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5, NbpObservable p6
    ) {
        return concatArray(p1, p2, p3, p4, p5, p6);
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(
            NbpObservable p1, NbpObservable p2,
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5, NbpObservable p6,
            NbpObservable p7
    ) {
        return concatArray(p1, p2, p3, p4, p5, p6, p7);
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5, NbpObservable p6,
            NbpObservable p7, NbpObservable p8
    ) {
        return concatArray(p1, p2, p3, p4, p5, p6, p7, p8);
    }

    @SuppressWarnings("unchecked")
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concat(
            NbpObservable p1, NbpObservable p2, 
            NbpObservable p3, NbpObservable p4,
            NbpObservable p5, NbpObservable p6,
            NbpObservable p7, NbpObservable p8,
            NbpObservable p9
    ) {
        return concatArray(p1, p2, p3, p4, p5, p6, p7, p8, p9);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @SchedulerSupport(SchedulerKind.NONE)
    public static  NbpObservable concatArray(int prefetch, NbpObservable... 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(SchedulerKind.NONE) public static NbpObservable concatArray(NbpObservable... sources) { if (sources.length == 0) { return empty(); } else if (sources.length == 1) { return (NbpObservable)sources[0]; } return fromArray(sources).concatMap((Function)Functions.identity()); } public static NbpObservable create(NbpOnSubscribe onSubscribe) { Objects.requireNonNull(onSubscribe, "onSubscribe is null"); // TODO plugin wrapper return new NbpObservable(onSubscribe); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable defer(Supplier> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return create(new NbpOnSubscribeDefer(supplier)); } @SchedulerSupport(SchedulerKind.NONE) @SuppressWarnings("unchecked") public static NbpObservable empty() { return (NbpObservable)EMPTY; } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable error(Supplier errorSupplier) { Objects.requireNonNull(errorSupplier, "errorSupplier is null"); return create(new NbpOnSubscribeErrorSource(errorSupplier)); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable error(final Throwable e) { Objects.requireNonNull(e, "e is null"); return error(new Supplier() { @Override public Throwable get() { return e; } }); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable 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 create(new NbpOnSubscribeArraySource(values)); } // TODO match naming with RxJava 1.x @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable fromCallable(Callable supplier) { Objects.requireNonNull(supplier, "supplier is null"); return create(new NbpOnSubscribeScalarAsyncSource(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(SchedulerKind.NONE) public static NbpObservable fromFuture(Future future) { Objects.requireNonNull(future, "future is null"); return create(new NbpOnSubscribeFutureSource(future, 0L, null)); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable fromFuture(Future future, long timeout, TimeUnit unit) { Objects.requireNonNull(future, "future is null"); Objects.requireNonNull(unit, "unit is null"); NbpObservable o = create(new NbpOnSubscribeFutureSource(future, timeout, unit)); return o; } @SchedulerSupport(SchedulerKind.CUSTOM) public static NbpObservable fromFuture(Future future, long timeout, TimeUnit unit, Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); NbpObservable o = fromFuture(future, timeout, unit); return o.subscribeOn(scheduler); } @SchedulerSupport(SchedulerKind.IO) public static NbpObservable fromFuture(Future future, Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); NbpObservable o = fromFuture(future); return o.subscribeOn(Schedulers.io()); } public static NbpObservable fromIterable(Iterable source) { Objects.requireNonNull(source, "source is null"); return create(new NbpOnSubscribeIterableSource(source)); } public static NbpObservable fromPublisher(final Publisher publisher) { Objects.requireNonNull(publisher, "publisher is null"); return create(new NbpOnSubscribe() { @Override public void accept(final NbpSubscriber 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(SchedulerKind.NONE) public static NbpObservable generate(final Consumer> generator) { Objects.requireNonNull(generator, "generator is null"); return generate(Functions.nullSupplier(), new BiFunction, Object>() { @Override public Object apply(Object s, NbpSubscriber o) { generator.accept(o); return s; } }, Functions.emptyConsumer()); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable generate(Supplier initialState, final BiConsumer> generator) { Objects.requireNonNull(generator, "generator is null"); return generate(initialState, new BiFunction, S>() { @Override public S apply(S s, NbpSubscriber o) { generator.accept(s, o); return s; } }, Functions.emptyConsumer()); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable 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, NbpSubscriber o) { generator.accept(s, o); return s; } }, disposeState); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable generate(Supplier initialState, BiFunction, S> generator) { return generate(initialState, generator, Functions.emptyConsumer()); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable 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 create(new NbpOnSubscribeGenerate(initialState, generator, disposeState)); } @SchedulerSupport(SchedulerKind.COMPUTATION) public static NbpObservable interval(long initialDelay, long period, TimeUnit unit) { return interval(initialDelay, period, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public static NbpObservable 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 create(new NbpOnSubscribeIntervalSource(initialDelay, period, unit, scheduler)); } @SchedulerSupport(SchedulerKind.COMPUTATION) public static NbpObservable interval(long period, TimeUnit unit) { return interval(period, period, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public static NbpObservable interval(long period, TimeUnit unit, Scheduler scheduler) { return interval(period, period, unit, scheduler); } @SchedulerSupport(SchedulerKind.COMPUTATION) public static NbpObservable intervalRange(long start, long count, long initialDelay, long period, TimeUnit unit) { return intervalRange(start, count, initialDelay, period, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public static NbpObservable 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 create(new NbpOnSubscribeIntervalRangeSource(start, end, initialDelay, period, unit, scheduler)); } public static NbpObservable just(T value) { Objects.requireNonNull(value, "The value is null"); return new NbpObservableScalarSource(value); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static final NbpObservable 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(SchedulerKind.NONE) public static final NbpObservable 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(SchedulerKind.NONE) public static final NbpObservable 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(SchedulerKind.NONE) public static final NbpObservable 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(SchedulerKind.NONE) public static final NbpObservable 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(SchedulerKind.NONE) public static final NbpObservable 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(SchedulerKind.NONE) public static final NbpObservable 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(SchedulerKind.NONE) public static final NbpObservable 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(SchedulerKind.NONE) public static NbpObservable merge(int maxConcurrency, int bufferSize, Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity(), false, maxConcurrency, bufferSize); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable merge(int maxConcurrency, int bufferSize, NbpObservable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), false, maxConcurrency, bufferSize); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable merge(int maxConcurrency, NbpObservable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable merge(Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity()); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable merge(Iterable> sources, int maxConcurrency) { return fromIterable(sources).flatMap((Function)Functions.identity(), maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static NbpObservable merge(NbpObservable> sources) { return sources.flatMap((Function)Functions.identity()); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable merge(NbpObservable> sources, int maxConcurrency) { return sources.flatMap((Function)Functions.identity(), maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable merge(NbpObservable p1, NbpObservable 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(SchedulerKind.NONE) public static NbpObservable merge(NbpObservable p1, NbpObservable p2, NbpObservable 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(SchedulerKind.NONE) public static NbpObservable merge( NbpObservable p1, NbpObservable p2, NbpObservable p3, NbpObservable 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(SchedulerKind.NONE) public static NbpObservable merge(NbpObservable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), sources.length); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable mergeDelayError(boolean delayErrors, Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity(), true); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable mergeDelayError(int maxConcurrency, int bufferSize, Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity(), true, maxConcurrency, bufferSize); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable mergeDelayError(int maxConcurrency, int bufferSize, NbpObservable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), true, maxConcurrency, bufferSize); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable mergeDelayError(int maxConcurrency, Iterable> sources) { return fromIterable(sources).flatMap((Function)Functions.identity(), true, maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable mergeDelayError(int maxConcurrency, NbpObservable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), true, maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static NbpObservable mergeDelayError(NbpObservable> sources) { return sources.flatMap((Function)Functions.identity(), true); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable mergeDelayError(NbpObservable> sources, int maxConcurrency) { return sources.flatMap((Function)Functions.identity(), true, maxConcurrency); } @SuppressWarnings({ "unchecked", "rawtypes" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable mergeDelayError(NbpObservable p1, NbpObservable 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(SchedulerKind.NONE) public static NbpObservable mergeDelayError(NbpObservable p1, NbpObservable p2, NbpObservable 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(SchedulerKind.NONE) public static NbpObservable mergeDelayError( NbpObservable p1, NbpObservable p2, NbpObservable p3, NbpObservable 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(SchedulerKind.NONE) public static NbpObservable mergeDelayError(NbpObservable... sources) { return fromArray(sources).flatMap((Function)Functions.identity(), true, sources.length); } @SchedulerSupport(SchedulerKind.NONE) @SuppressWarnings("unchecked") public static NbpObservable never() { return (NbpObservable)NEVER; } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable 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 NbpOnSubscribe() { @Override public void accept(NbpSubscriber s) { BooleanDisposable d = new BooleanDisposable(); 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(SchedulerKind.NONE) public static NbpObservable sequenceEqual(NbpObservable p1, NbpObservable p2) { return sequenceEqual(p1, p2, Objects.equalsPredicate(), bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable sequenceEqual(NbpObservable p1, NbpObservable p2, BiPredicate isEqual) { return sequenceEqual(p1, p2, isEqual, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable sequenceEqual(NbpObservable p1, NbpObservable 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 create(new NbpOnSubscribeSequenceEqual(p1, p2, isEqual, bufferSize)); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable sequenceEqual(NbpObservable p1, NbpObservable p2, int bufferSize) { return sequenceEqual(p1, p2, Objects.equalsPredicate(), bufferSize); } @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable switchOnNext(int bufferSize, NbpObservable> sources) { return sources.switchMap((Function)Functions.identity(), bufferSize); } @SuppressWarnings({ "rawtypes", "unchecked" }) @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable switchOnNext(NbpObservable> sources) { return sources.switchMap((Function)Functions.identity()); } @SchedulerSupport(SchedulerKind.COMPUTATION) public static NbpObservable timer(long delay, TimeUnit unit) { return timer(delay, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public static NbpObservable 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 create(new NbpOnSubscribeTimerOnceSource(delay, unit, scheduler)); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable using(Supplier resourceSupplier, Function> sourceSupplier, Consumer disposer) { return using(resourceSupplier, sourceSupplier, disposer, true); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable 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 create(new NbpOnSubscribeUsing(resourceSupplier, sourceSupplier, disposer, eager)); } private static void validateBufferSize(int bufferSize) { if (bufferSize <= 0) { throw new IllegalArgumentException("bufferSize > 0 required but it was " + bufferSize); } } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip(Iterable> sources, Function zipper) { Objects.requireNonNull(zipper, "zipper is null"); Objects.requireNonNull(sources, "sources is null"); return create(new NbpOnSubscribeZip(null, sources, zipper, bufferSize(), false)); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip(NbpObservable> sources, final Function zipper) { Objects.requireNonNull(zipper, "zipper is null"); return sources.toList().flatMap(new Function>, NbpObservable>() { @Override public NbpObservable apply(List> list) { return zipIterable(zipper, false, bufferSize(), list); } }); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, BiFunction zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, BiFunction zipper, boolean delayError) { return zipArray(Functions.toFunction(zipper), delayError, bufferSize(), p1, p2); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, BiFunction zipper, boolean delayError, int bufferSize) { return zipArray(Functions.toFunction(zipper), delayError, bufferSize, p1, p2); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, NbpObservable p3, Function3 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, NbpObservable p3, NbpObservable p4, Function4 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, NbpObservable p3, NbpObservable p4, NbpObservable p5, Function5 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, NbpObservable p3, NbpObservable p4, NbpObservable p5, NbpObservable p6, Function6 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5, p6); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, NbpObservable p3, NbpObservable p4, NbpObservable p5, NbpObservable p6, NbpObservable p7, Function7 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, NbpObservable p3, NbpObservable p4, NbpObservable p5, NbpObservable p6, NbpObservable p7, NbpObservable p8, Function8 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7, p8); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zip( NbpObservable p1, NbpObservable p2, NbpObservable p3, NbpObservable p4, NbpObservable p5, NbpObservable p6, NbpObservable p7, NbpObservable p8, NbpObservable p9, Function9 zipper) { return zipArray(Functions.toFunction(zipper), false, bufferSize(), p1, p2, p3, p4, p5, p6, p7, p8, p9); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zipArray(Function zipper, boolean delayError, int bufferSize, NbpObservable... sources) { if (sources.length == 0) { return empty(); } Objects.requireNonNull(zipper, "zipper is null"); validateBufferSize(bufferSize); return create(new NbpOnSubscribeZip(sources, null, zipper, bufferSize, delayError)); } @SchedulerSupport(SchedulerKind.NONE) public static NbpObservable zipIterable(Function zipper, boolean delayError, int bufferSize, Iterable> sources) { Objects.requireNonNull(zipper, "zipper is null"); Objects.requireNonNull(sources, "sources is null"); validateBufferSize(bufferSize); return create(new NbpOnSubscribeZip(null, sources, zipper, bufferSize, delayError)); } protected final NbpOnSubscribe onSubscribe; protected NbpObservable(NbpOnSubscribe onSubscribe) { this.onSubscribe = onSubscribe; } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable all(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorAll(predicate)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable ambWith(NbpObservable other) { Objects.requireNonNull(other, "other is null"); return amb(this, other); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable any(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorAny(predicate)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable asObservable() { return create(new NbpOnSubscribe() { @Override public void accept(NbpSubscriber s) { NbpObservable.this.subscribe(s); } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> buffer(int count) { return buffer(count, count); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> buffer(int count, int skip) { return buffer(count, skip, new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerKind.NONE) public final > NbpObservable 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(SchedulerKind.NONE) public final > NbpObservable buffer(int count, Supplier bufferSupplier) { return buffer(count, count, bufferSupplier); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable> buffer(long timespan, long timeskip, TimeUnit unit) { return buffer(timespan, timeskip, unit, Schedulers.computation(), new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> 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(SchedulerKind.CUSTOM) public final > NbpObservable 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(SchedulerKind.COMPUTATION) public final NbpObservable> buffer(long timespan, TimeUnit unit) { return buffer(timespan, unit, Integer.MAX_VALUE, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable> buffer(long timespan, TimeUnit unit, int count) { return buffer(timespan, unit, count, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> 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(SchedulerKind.CUSTOM) public final > NbpObservable 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(SchedulerKind.CUSTOM) public final NbpObservable> 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(SchedulerKind.NONE) public final NbpObservable> buffer( NbpObservable bufferOpenings, Function> bufferClosingSelector) { return buffer(bufferOpenings, bufferClosingSelector, new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerKind.NONE) public final > NbpObservable buffer( NbpObservable 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(SchedulerKind.NONE) public final NbpObservable> buffer(NbpObservable 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(SchedulerKind.NONE) public final NbpObservable> buffer(NbpObservable boundary, final int initialCapacity) { return buffer(boundary, new Supplier>() { @Override public List get() { return new ArrayList(initialCapacity); } }); } @SchedulerSupport(SchedulerKind.NONE) public final > NbpObservable buffer(NbpObservable boundary, Supplier bufferSupplier) { Objects.requireNonNull(boundary, "boundary is null"); Objects.requireNonNull(bufferSupplier, "bufferSupplier is null"); return lift(new NbpOperatorBufferExactBoundary(boundary, bufferSupplier)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> buffer(Supplier> boundarySupplier) { return buffer(boundarySupplier, new Supplier>() { @Override public List get() { return new ArrayList(); } }); } @SchedulerSupport(SchedulerKind.NONE) public final > NbpObservable buffer(Supplier> boundarySupplier, Supplier bufferSupplier) { Objects.requireNonNull(boundarySupplier, "boundarySupplier is null"); Objects.requireNonNull(bufferSupplier, "bufferSupplier is null"); return lift(new NbpOperatorBufferBoundarySupplier(boundarySupplier, bufferSupplier)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable cache() { return NbpCachedObservable.from(this); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable cache(int capacityHint) { return NbpCachedObservable.from(this, capacityHint); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable collect(Supplier initialValueSupplier, BiConsumer collector) { Objects.requireNonNull(initialValueSupplier, "initalValueSupplier is null"); Objects.requireNonNull(collector, "collector is null"); return lift(new NbpOperatorCollect(initialValueSupplier, collector)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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 NbpObservable compose(Function, ? extends NbpObservable> convert) { return to(convert); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable concatMap(Function> mapper) { return concatMap(mapper, 2); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable concatMapIterable(final Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return concatMap(new Function>() { @Override public NbpObservable apply(T v) { return fromIterable(mapper.apply(v)); } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable concatMapIterable(final Function> mapper, int prefetch) { return concatMap(new Function>() { @Override public NbpObservable apply(T v) { return fromIterable(mapper.apply(v)); } }, prefetch); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable concatWith(NbpObservable other) { Objects.requireNonNull(other, "other is null"); return concat(this, other); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable count() { return lift(NbpOperatorCount.instance()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable debounce(Function> debounceSelector) { Objects.requireNonNull(debounceSelector, "debounceSelector is null"); return lift(new NbpOperatorDebounce(debounceSelector)); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable debounce(long timeout, TimeUnit unit) { return debounce(timeout, unit, Schedulers.computation()); } @BackpressureSupport(BackpressureKind.ERROR) @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable defaultIfEmpty(T value) { Objects.requireNonNull(value, "value is null"); return switchIfEmpty(just(value)); } @SchedulerSupport(SchedulerKind.NONE) // TODO a more efficient implementation if necessary public final NbpObservable delay(final Function> itemDelay) { Objects.requireNonNull(itemDelay, "itemDelay is null"); return flatMap(new Function>() { @Override public NbpObservable apply(final T v) { return itemDelay.apply(v).take(1).map(new Function() { @Override public T apply(U u) { return v; } }).defaultIfEmpty(v); } }); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable delay(long delay, TimeUnit unit) { return delay(delay, unit, Schedulers.computation(), false); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable delay(long delay, TimeUnit unit, boolean delayError) { return delay(delay, unit, Schedulers.computation(), delayError); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable delay(long delay, TimeUnit unit, Scheduler scheduler) { return delay(delay, unit, scheduler, false); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable 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 NbpObservable delaySubscription(NbpObservable other) { Objects.requireNonNull(other, "other is null"); return create(new NbpOnSubscribeDelaySubscriptionOther(this, other)); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable delaySubscription(long delay, TimeUnit unit) { return delaySubscription(delay, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) // TODO a more efficient implementation if necessary public final NbpObservable 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 NbpObservable apply(Long v) { return NbpObservable.this; } }); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable delaySubscription(final Supplier> delaySupplier) { Objects.requireNonNull(delaySupplier, "delaySupplier is null"); return fromCallable(new Callable>() { @Override public NbpObservable call() throws Exception { return delaySupplier.get(); } }) .flatMap((Function)Functions.identity()) .take(1) .cast(Object.class) .defaultIfEmpty(OBJECT) .flatMap(new Function>() { @Override public NbpObservable apply(Object v) { return NbpObservable.this; } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable dematerialize() { @SuppressWarnings("unchecked") NbpObservable>> m = (NbpObservable>>)this; return m.lift(NbpOperatorDematerialize.instance()); } @SuppressWarnings({ "rawtypes", "unchecked" }) @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable distinct() { return distinct((Function)Functions.identity(), new Supplier>() { @Override public Collection get() { return new HashSet(); } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable distinct(Function keySelector) { return distinct(keySelector, new Supplier>() { @Override public Collection get() { return new HashSet(); } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable distinct(Function keySelector, Supplier> collectionSupplier) { Objects.requireNonNull(keySelector, "keySelector is null"); Objects.requireNonNull(collectionSupplier, "collectionSupplier is null"); return lift(NbpOperatorDistinct.withCollection(keySelector, collectionSupplier)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable distinctUntilChanged() { return lift(NbpOperatorDistinct.untilChanged()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable distinctUntilChanged(Function keySelector) { Objects.requireNonNull(keySelector, "keySelector is null"); return lift(NbpOperatorDistinct.untilChanged(keySelector)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable doOnCancel(Runnable onCancel) { return doOnLifecycle(Functions.emptyConsumer(), onCancel); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable doOnComplete(Runnable onComplete) { return doOnEach(Functions.emptyConsumer(), Functions.emptyConsumer(), onComplete, Functions.emptyRunnable()); } @SchedulerSupport(SchedulerKind.NONE) private NbpObservable 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(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable doOnEach(final NbpSubscriber 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(SchedulerKind.NONE) public final NbpObservable doOnError(Consumer onError) { return doOnEach(Functions.emptyConsumer(), onError, Functions.emptyRunnable(), Functions.emptyRunnable()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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 NbpSubscriber apply(NbpSubscriber s) { return new NbpSubscriptionLambdaSubscriber(s, onSubscribe, onCancel); } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable doOnNext(Consumer onNext) { return doOnEach(onNext, Functions.emptyConsumer(), Functions.emptyRunnable(), Functions.emptyRunnable()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable doOnSubscribe(Consumer onSubscribe) { return doOnLifecycle(onSubscribe, Functions.emptyRunnable()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable doOnTerminate(final Runnable onTerminate) { return doOnEach(Functions.emptyConsumer(), new Consumer() { @Override public void accept(Throwable e) { onTerminate.run(); } }, onTerminate, Functions.emptyRunnable()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable elementAt(long index) { if (index < 0) { throw new IndexOutOfBoundsException("index >= 0 required but it was " + index); } return lift(new NbpOperatorElementAt(index, null)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable endWith(Iterable values) { return concatArray(this, fromIterable(values)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable endWith(NbpObservable other) { Objects.requireNonNull(other, "other is null"); return concatArray(this, other); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable endWith(T value) { Objects.requireNonNull(value, "value is null"); return concatArray(this, just(value)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable endWithArray(T... values) { NbpObservable fromArray = fromArray(values); if (fromArray == empty()) { return this; } return concatArray(this, fromArray); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable filter(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorFilter(predicate)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable finallyDo(Runnable onFinally) { return doOnEach(Functions.emptyConsumer(), Functions.emptyConsumer(), Functions.emptyRunnable(), onFinally); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable first() { return take(1).single(); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable first(T defaultValue) { return take(1).single(defaultValue); } public final NbpObservable flatMap(Function> mapper) { return flatMap(mapper, false); } public final NbpObservable flatMap(Function> mapper, boolean delayError) { return flatMap(mapper, delayError, Integer.MAX_VALUE); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable flatMap(Function> mapper, boolean delayErrors, int maxConcurrency) { return flatMap(mapper, delayErrors, maxConcurrency, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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 NbpObservableScalarSource) { NbpObservableScalarSource scalar = (NbpObservableScalarSource) this; return create(scalar.scalarFlatMap(mapper)); } return lift(new NbpOperatorFlatMap(mapper, delayErrors, maxConcurrency, bufferSize)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable flatMap(Function> mapper, int maxConcurrency) { return flatMap(mapper, false, maxConcurrency, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable flatMap(Function> mapper, BiFunction resultSelector) { return flatMap(mapper, resultSelector, false, bufferSize(), bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable flatMap(Function> mapper, BiFunction combiner, boolean delayError) { return flatMap(mapper, combiner, delayError, bufferSize(), bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable flatMap(Function> mapper, BiFunction combiner, boolean delayError, int maxConcurrency) { return flatMap(mapper, combiner, delayError, maxConcurrency, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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 NbpObservable apply(final T t) { @SuppressWarnings("unchecked") NbpObservable u = (NbpObservable)mapper.apply(t); return u.map(new Function() { @Override public R apply(U w) { return combiner.apply(t, w); } }); } }, delayError, maxConcurrency, bufferSize); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable flatMap(Function> mapper, BiFunction combiner, int maxConcurrency) { return flatMap(mapper, combiner, false, maxConcurrency, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable flatMapIterable(final Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return flatMap(new Function>() { @Override public NbpObservable apply(T v) { return fromIterable(mapper.apply(v)); } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable flatMapIterable(final Function> mapper, BiFunction resultSelector) { return flatMap(new Function>() { @Override public NbpObservable apply(T t) { return fromIterable(mapper.apply(t)); } }, resultSelector, false, bufferSize(), bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable flatMapIterable(final Function> mapper, int bufferSize) { return flatMap(new Function>() { @Override public NbpObservable apply(T v) { return fromIterable(mapper.apply(v)); } }, false, bufferSize); } @SchedulerSupport(SchedulerKind.NONE) public final Disposable forEach(Consumer onNext) { return subscribe(onNext); } @SchedulerSupport(SchedulerKind.NONE) public final Disposable forEachWhile(Predicate onNext) { return forEachWhile(onNext, RxJavaPlugins.errorConsumer(), Functions.emptyRunnable()); } @SchedulerSupport(SchedulerKind.NONE) public final Disposable forEachWhile(Predicate onNext, Consumer onError) { return forEachWhile(onNext, onError, Functions.emptyRunnable()); } @SchedulerSupport(SchedulerKind.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(SchedulerKind.NONE) public final List getList() { final List result = new ArrayList(); final Throwable[] error = { null }; final CountDownLatch cdl = new CountDownLatch(1); subscribe(new NbpSubscriber() { @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(SchedulerKind.NONE) public final NbpObservable> groupBy(Function keySelector) { return groupBy(keySelector, (Function)Functions.identity(), false, bufferSize()); } @SuppressWarnings({ "unchecked", "rawtypes" }) @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> groupBy(Function keySelector, boolean delayError) { return groupBy(keySelector, (Function)Functions.identity(), delayError, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> groupBy(Function keySelector, Function valueSelector) { return groupBy(keySelector, valueSelector, false, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> groupBy(Function keySelector, Function valueSelector, boolean delayError) { return groupBy(keySelector, valueSelector, false, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> 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(SchedulerKind.NONE) public final NbpObservable ignoreElements() { return lift(NbpOperatorIgnoreElements.instance()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable isEmpty() { return all(new Predicate() { @Override public boolean test(T v) { return false; } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable last() { return takeLast(1).single(); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable last(T defaultValue) { return takeLast(1).single(defaultValue); } public final NbpObservable lift(NbpOperator onLift) { Objects.requireNonNull(onLift, "onLift is null"); return create(new NbpOnSubscribeLift(this, onLift)); } public final NbpObservable map(Function mapper) { Objects.requireNonNull(mapper, "mapper is null"); return lift(new NbpOperatorMap(mapper)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable>> materialize() { return lift(NbpOperatorMaterialize.instance()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable mergeWith(NbpObservable other) { Objects.requireNonNull(other, "other is null"); return merge(this, other); } @SchedulerSupport(SchedulerKind.NONE) @Deprecated public final NbpObservable> nest() { return just(this); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable observeOn(Scheduler scheduler) { return observeOn(scheduler, false, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable observeOn(Scheduler scheduler, boolean delayError) { return observeOn(scheduler, delayError, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable observeOn(Scheduler scheduler, boolean delayError, int bufferSize) { Objects.requireNonNull(scheduler, "scheduler is null"); validateBufferSize(bufferSize); return lift(new NbpOperatorObserveOn(scheduler, delayError, bufferSize)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable onErrorResumeNext(Function> resumeFunction) { Objects.requireNonNull(resumeFunction, "resumeFunction is null"); return lift(new NbpOperatorOnErrorNext(resumeFunction, false)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable onErrorResumeNext(final NbpObservable next) { Objects.requireNonNull(next, "next is null"); return onErrorResumeNext(new Function>() { @Override public NbpObservable apply(Throwable e) { return next; } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable onErrorReturn(Function valueSupplier) { Objects.requireNonNull(valueSupplier, "valueSupplier is null"); return lift(new NbpOperatorOnErrorReturn(valueSupplier)); } // TODO would result in ambiguity with onErrorReturn(Function) @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable onErrorReturnValue(final T value) { Objects.requireNonNull(value, "value is null"); return onErrorReturn(new Function() { @Override public T apply(Throwable e) { return value; } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable onExceptionResumeNext(final NbpObservable next) { Objects.requireNonNull(next, "next is null"); return lift(new NbpOperatorOnErrorNext(new Function>() { @Override public NbpObservable apply(Throwable e) { return next; } }, true)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpConnectableObservable publish() { return publish(bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable publish(Function, ? extends NbpObservable> selector) { return publish(selector, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable publish(Function, ? extends NbpObservable> selector, int bufferSize) { validateBufferSize(bufferSize); Objects.requireNonNull(selector, "selector is null"); return NbpOperatorPublish.create(this, selector, bufferSize); } @SchedulerSupport(SchedulerKind.NONE) public final NbpConnectableObservable publish(int bufferSize) { validateBufferSize(bufferSize); return NbpOperatorPublish.create(this, bufferSize); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable reduce(BiFunction reducer) { return scan(reducer).last(); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable reduce(R seed, BiFunction reducer) { return scan(seed, reducer).last(); } // Naming note, a plain scan would cause ambiguity with the value-seeded version @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable reduceWith(Supplier seedSupplier, BiFunction reducer) { return scanWith(seedSupplier, reducer).last(); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable repeat() { return repeat(Long.MAX_VALUE); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable repeat(long times) { if (times < 0) { throw new IllegalArgumentException("times >= 0 required but it was " + times); } if (times == 0) { return empty(); } return create(new NbpOnSubscribeRepeat(this, times)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable repeatUntil(BooleanSupplier stop) { Objects.requireNonNull(stop, "stop is null"); return create(new NbpOnSubscribeRepeatUntil(this, stop)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable repeatWhen(final Function, ? extends NbpObservable> handler) { Objects.requireNonNull(handler, "handler is null"); Function>>, NbpObservable> f = new Function>>, NbpObservable>() { @Override public NbpObservable apply(NbpObservable>> no) { return handler.apply(no.map(new Function>, Object>() { @Override public Object apply(Try> v) { return 0; } })); } } ; return create(new NbpOnSubscribeRedo(this, f)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpConnectableObservable replay() { return NbpOperatorReplay.createFrom(this); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable replay(Function, ? extends NbpObservable> selector) { Objects.requireNonNull(selector, "selector is null"); return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public NbpConnectableObservable get() { return replay(); } }, selector); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable replay(Function, ? extends NbpObservable> selector, final int bufferSize) { Objects.requireNonNull(selector, "selector is null"); return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public NbpConnectableObservable get() { return replay(bufferSize); } }, selector); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable replay(Function, ? extends NbpObservable> selector, int bufferSize, long time, TimeUnit unit) { return replay(selector, bufferSize, time, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable replay(Function, ? extends NbpObservable> 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 NbpConnectableObservable get() { return replay(bufferSize, time, unit, scheduler); } }, selector); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable replay(final Function, ? extends NbpObservable> selector, final int bufferSize, final Scheduler scheduler) { return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public NbpConnectableObservable get() { return replay(bufferSize); } }, new Function, NbpObservable>() { @Override public NbpObservable apply(NbpObservable t) { return selector.apply(t).observeOn(scheduler); } }); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable replay(Function, ? extends NbpObservable> selector, long time, TimeUnit unit) { return replay(selector, time, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable replay(Function, ? extends NbpObservable> 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 NbpConnectableObservable get() { return replay(time, unit, scheduler); } }, selector); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable replay(final Function, ? extends NbpObservable> selector, final Scheduler scheduler) { Objects.requireNonNull(selector, "selector is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return NbpOperatorReplay.multicastSelector(new Supplier>() { @Override public NbpConnectableObservable get() { return replay(); } }, new Function, NbpObservable>() { @Override public NbpObservable apply(NbpObservable t) { return selector.apply(t).observeOn(scheduler); } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpConnectableObservable replay(final int bufferSize) { return NbpOperatorReplay.create(this, bufferSize); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpConnectableObservable replay(int bufferSize, long time, TimeUnit unit) { return replay(bufferSize, time, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpConnectableObservable 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(SchedulerKind.CUSTOM) public final NbpConnectableObservable replay(final int bufferSize, final Scheduler scheduler) { return NbpOperatorReplay.observeOn(replay(bufferSize), scheduler); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpConnectableObservable replay(long time, TimeUnit unit) { return replay(time, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpConnectableObservable 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(SchedulerKind.CUSTOM) public final NbpConnectableObservable replay(final Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return NbpOperatorReplay.observeOn(replay(), scheduler); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable retry() { return retry(Long.MAX_VALUE, Functions.alwaysTrue()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable retry(BiPredicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return create(new NbpOnSubscribeRetryBiPredicate(this, predicate)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable retry(long times) { return retry(times, Functions.alwaysTrue()); } // Retries at most times or until the predicate returns false, whichever happens first @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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 create(new NbpOnSubscribeRetryPredicate(this, times, predicate)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable retry(Predicate predicate) { return retry(Long.MAX_VALUE, predicate); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable retryWhen( final Function, ? extends NbpObservable> handler) { Objects.requireNonNull(handler, "handler is null"); Function>>, NbpObservable> f = new Function>>, NbpObservable>() { @Override public NbpObservable apply(NbpObservable>> 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 create(new NbpOnSubscribeRedo(this, f)); } // TODO decide if safe subscription or unsafe should be the default @SchedulerSupport(SchedulerKind.NONE) public final void safeSubscribe(NbpSubscriber s) { Objects.requireNonNull(s, "s is null"); if (s instanceof NbpSafeSubscriber) { subscribe(s); } else { subscribe(new NbpSafeSubscriber(s)); } } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable sample(long period, TimeUnit unit) { return sample(period, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable sample(NbpObservable sampler) { Objects.requireNonNull(sampler, "sampler is null"); return lift(new NbpOperatorSampleWithObservable(sampler)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable scan(BiFunction accumulator) { Objects.requireNonNull(accumulator, "accumulator is null"); return lift(new NbpOperatorScan(accumulator)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable scanWith(Supplier seedSupplier, BiFunction accumulator) { Objects.requireNonNull(seedSupplier, "seedSupplier is null"); Objects.requireNonNull(accumulator, "accumulator is null"); return lift(new NbpOperatorScanSeed(seedSupplier, accumulator)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable serialize() { return lift(new NbpOperator() { @Override public NbpSubscriber apply(NbpSubscriber s) { return new NbpSerializedSubscriber(s); } }); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable share() { return publish().refCount(); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable single() { return lift(NbpOperatorSingle.instanceNoDefault()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable single(T defaultValue) { Objects.requireNonNull(defaultValue, "defaultValue is null"); return lift(new NbpOperatorSingle(defaultValue)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.CUSTOM) public final NbpObservable skip(long time, TimeUnit unit, Scheduler scheduler) { // TODO consider inlining this behavior return skipUntil(timer(time, unit, scheduler)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.TRAMPOLINE) public final NbpObservable skipLast(long time, TimeUnit unit) { return skipLast(time, unit, Schedulers.trampoline(), false, bufferSize()); } @SchedulerSupport(SchedulerKind.TRAMPOLINE) public final NbpObservable skipLast(long time, TimeUnit unit, boolean delayError) { return skipLast(time, unit, Schedulers.trampoline(), delayError, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable skipLast(long time, TimeUnit unit, Scheduler scheduler) { return skipLast(time, unit, scheduler, false, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable skipLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError) { return skipLast(time, unit, scheduler, delayError, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable 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(SchedulerKind.NONE) public final NbpObservable skipUntil(NbpObservable other) { Objects.requireNonNull(other, "other is null"); return lift(new NbpOperatorSkipUntil(other)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable skipWhile(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorSkipWhile(predicate)); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable startWith(Iterable values) { return concatArray(fromIterable(values), this); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable startWith(NbpObservable other) { Objects.requireNonNull(other, "other is null"); return concatArray(other, this); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable startWith(T value) { Objects.requireNonNull(value, "value is null"); return concatArray(just(value), this); } @SuppressWarnings("unchecked") @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable startWithArray(T... values) { NbpObservable fromArray = fromArray(values); if (fromArray == empty()) { return this; } return concatArray(fromArray, this); } @SchedulerSupport(SchedulerKind.NONE) public final Disposable subscribe() { return subscribe(Functions.emptyConsumer(), RxJavaPlugins.errorConsumer(), Functions.emptyRunnable(), Functions.emptyConsumer()); } @SchedulerSupport(SchedulerKind.NONE) public final Disposable subscribe(Consumer onNext) { return subscribe(onNext, RxJavaPlugins.errorConsumer(), Functions.emptyRunnable(), Functions.emptyConsumer()); } @SchedulerSupport(SchedulerKind.NONE) public final Disposable subscribe(Consumer onNext, Consumer onError) { return subscribe(onNext, onError, Functions.emptyRunnable(), Functions.emptyConsumer()); } @SchedulerSupport(SchedulerKind.NONE) public final Disposable subscribe(Consumer onNext, Consumer onError, Runnable onComplete) { return subscribe(onNext, onError, onComplete, Functions.emptyConsumer()); } @SchedulerSupport(SchedulerKind.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; } public final void subscribe(NbpSubscriber subscriber) { Objects.requireNonNull(subscriber, "subscriber is null"); onSubscribe.accept(subscriber); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable subscribeOn(Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return create(new NbpOnSubscribeSubscribeOn(this, scheduler)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable switchIfEmpty(NbpObservable other) { Objects.requireNonNull(other, "other is null"); return lift(new NbpOperatorSwitchIfEmpty(other)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable switchMap(Function> mapper) { return switchMap(mapper, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable switchMap(Function> mapper, int bufferSize) { Objects.requireNonNull(mapper, "mapper is null"); validateBufferSize(bufferSize); return lift(new NbpOperatorSwitchMap(mapper, bufferSize)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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 lift(new NbpOperatorTake(n)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable take(long time, TimeUnit unit, Scheduler scheduler) { // TODO consider inlining this behavior return takeUntil(timer(time, unit, scheduler)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable takeFirst(Predicate predicate) { return filter(predicate).take(1); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable 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(SchedulerKind.TRAMPOLINE) public final NbpObservable takeLast(long count, long time, TimeUnit unit) { return takeLast(count, time, unit, Schedulers.trampoline(), false, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable takeLast(long count, long time, TimeUnit unit, Scheduler scheduler) { return takeLast(count, time, unit, scheduler, false, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable 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(SchedulerKind.TRAMPOLINE) public final NbpObservable takeLast(long time, TimeUnit unit) { return takeLast(time, unit, Schedulers.trampoline(), false, bufferSize()); } @SchedulerSupport(SchedulerKind.TRAMPOLINE) public final NbpObservable takeLast(long time, TimeUnit unit, boolean delayError) { return takeLast(time, unit, Schedulers.trampoline(), delayError, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable takeLast(long time, TimeUnit unit, Scheduler scheduler) { return takeLast(time, unit, scheduler, false, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable takeLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError) { return takeLast(time, unit, scheduler, delayError, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable takeLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError, int bufferSize) { return takeLast(Long.MAX_VALUE, time, unit, scheduler, delayError, bufferSize); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> takeLastBuffer(int count) { return takeLast(count).toList(); } @SchedulerSupport(SchedulerKind.TRAMPOLINE) public final NbpObservable> takeLastBuffer(int count, long time, TimeUnit unit) { return takeLast(count, time, unit).toList(); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> takeLastBuffer(int count, long time, TimeUnit unit, Scheduler scheduler) { return takeLast(count, time, unit, scheduler).toList(); } @SchedulerSupport(SchedulerKind.TRAMPOLINE) public final NbpObservable> takeLastBuffer(long time, TimeUnit unit) { return takeLast(time, unit).toList(); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> takeLastBuffer(long time, TimeUnit unit, Scheduler scheduler) { return takeLast(time, unit, scheduler).toList(); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable takeUntil(NbpObservable other) { Objects.requireNonNull(other, "other is null"); return lift(new NbpOperatorTakeUntil(other)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable takeUntil(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorTakeUntilPredicate(predicate)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable takeWhile(Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return lift(new NbpOperatorTakeWhile(predicate)); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable throttleFirst(long windowDuration, TimeUnit unit) { return throttleFirst(windowDuration, unit, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable 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(SchedulerKind.COMPUTATION) public final NbpObservable throttleLast(long intervalDuration, TimeUnit unit) { return sample(intervalDuration, unit); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable throttleLast(long intervalDuration, TimeUnit unit, Scheduler scheduler) { return sample(intervalDuration, unit, scheduler); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable throttleWithTimeout(long timeout, TimeUnit unit) { return debounce(timeout, unit); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable throttleWithTimeout(long timeout, TimeUnit unit, Scheduler scheduler) { return debounce(timeout, unit, scheduler); } @SchedulerSupport(SchedulerKind.TRAMPOLINE) public final NbpObservable> timeInterval() { return timeInterval(TimeUnit.MILLISECONDS, Schedulers.trampoline()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> timeInterval(Scheduler scheduler) { return timeInterval(TimeUnit.MILLISECONDS, scheduler); } @SchedulerSupport(SchedulerKind.TRAMPOLINE) public final NbpObservable> timeInterval(TimeUnit unit) { return timeInterval(unit, Schedulers.trampoline()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> timeInterval(TimeUnit unit, Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorTimeInterval(unit, scheduler)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable timeout(Function> timeoutSelector) { return timeout0(null, timeoutSelector, null); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable timeout(Function> timeoutSelector, NbpObservable other) { Objects.requireNonNull(other, "other is null"); return timeout0(null, timeoutSelector, other); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable timeout(long timeout, TimeUnit timeUnit) { return timeout0(timeout, timeUnit, null, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable timeout(long timeout, TimeUnit timeUnit, NbpObservable other) { Objects.requireNonNull(other, "other is null"); return timeout0(timeout, timeUnit, other, Schedulers.computation()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable timeout(long timeout, TimeUnit timeUnit, NbpObservable other, Scheduler scheduler) { Objects.requireNonNull(other, "other is null"); return timeout0(timeout, timeUnit, other, scheduler); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable timeout(long timeout, TimeUnit timeUnit, Scheduler scheduler) { return timeout0(timeout, timeUnit, null, scheduler); } public final NbpObservable timeout(Supplier> firstTimeoutSelector, Function> timeoutSelector) { Objects.requireNonNull(firstTimeoutSelector, "firstTimeoutSelector is null"); return timeout0(firstTimeoutSelector, timeoutSelector, null); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable timeout( Supplier> firstTimeoutSelector, Function> timeoutSelector, NbpObservable other) { Objects.requireNonNull(firstTimeoutSelector, "firstTimeoutSelector is null"); Objects.requireNonNull(other, "other is null"); return timeout0(firstTimeoutSelector, timeoutSelector, other); } private NbpObservable timeout0(long timeout, TimeUnit timeUnit, NbpObservable other, Scheduler scheduler) { Objects.requireNonNull(timeUnit, "timeUnit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorTimeoutTimed(timeout, timeUnit, scheduler, other)); } private NbpObservable timeout0( Supplier> firstTimeoutSelector, Function> timeoutSelector, NbpObservable other) { Objects.requireNonNull(timeoutSelector, "timeoutSelector is null"); return lift(new NbpOperatorTimeout(firstTimeoutSelector, timeoutSelector, other)); } @SchedulerSupport(SchedulerKind.TRAMPOLINE) public final NbpObservable> timestamp() { return timestamp(TimeUnit.MILLISECONDS, Schedulers.trampoline()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> timestamp(Scheduler scheduler) { return timestamp(TimeUnit.MILLISECONDS, scheduler); } @SchedulerSupport(SchedulerKind.TRAMPOLINE) public final NbpObservable> timestamp(TimeUnit unit) { return timestamp(unit, Schedulers.trampoline()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> 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(SchedulerKind.NONE) public final NbpBlockingObservable toBlocking() { return NbpBlockingObservable.from(this); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> toList() { return lift(NbpOperatorToList.defaultInstance()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> 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(SchedulerKind.NONE) public final > NbpObservable toList(Supplier collectionSupplier) { Objects.requireNonNull(collectionSupplier, "collectionSupplier is null"); return lift(new NbpOperatorToList(collectionSupplier)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> 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(SchedulerKind.NONE) public final NbpObservable> 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(SchedulerKind.NONE) public final NbpObservable> 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(SchedulerKind.NONE) public final NbpObservable>> 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(SchedulerKind.NONE) public final NbpObservable>> 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(SchedulerKind.NONE) @SuppressWarnings("unchecked") public final NbpObservable>> 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(SchedulerKind.NONE) public final NbpObservable>> 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 Observable toObservable(BackpressureStrategy strategy) { Observable o = Observable.create(new Publisher() { @Override public void subscribe(final Subscriber s) { NbpObservable.this.subscribe(new NbpSubscriber() { @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(SchedulerKind.NONE) public final Single toSingle() { return Single.create(new SingleOnSubscribe() { @Override public void accept(final SingleSubscriber s) { NbpObservable.this.subscribe(new NbpSubscriber() { 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(SchedulerKind.NONE) public final NbpObservable> toSortedList() { return toSortedList(Functions.naturalOrder()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> 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(SchedulerKind.NONE) public final NbpObservable> 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(SchedulerKind.NONE) public final NbpObservable> toSortedList(int capacityHint) { return toSortedList(Functions.naturalOrder(), capacityHint); } @SchedulerSupport(SchedulerKind.NONE) // TODO decide if safe subscription or unsafe should be the default public final void unsafeSubscribe(NbpSubscriber s) { Objects.requireNonNull(s, "s is null"); subscribe(s); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable unsubscribeOn(Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return lift(new NbpOperatorUnsubscribeOn(scheduler)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> window(long count) { return window(count, count, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> window(long count, long skip) { return window(count, skip, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> 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(SchedulerKind.COMPUTATION) public final NbpObservable> window(long timespan, long timeskip, TimeUnit unit) { return window(timespan, timeskip, unit, Schedulers.computation(), bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> window(long timespan, long timeskip, TimeUnit unit, Scheduler scheduler) { return window(timespan, timeskip, unit, scheduler, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> 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(SchedulerKind.COMPUTATION) public final NbpObservable> window(long timespan, TimeUnit unit) { return window(timespan, unit, Schedulers.computation(), Long.MAX_VALUE, false); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable> window(long timespan, TimeUnit unit, long count) { return window(timespan, unit, Schedulers.computation(), count, false); } @SchedulerSupport(SchedulerKind.COMPUTATION) public final NbpObservable> window(long timespan, TimeUnit unit, long count, boolean restart) { return window(timespan, unit, Schedulers.computation(), count, restart); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> window(long timespan, TimeUnit unit, Scheduler scheduler) { return window(timespan, unit, scheduler, Long.MAX_VALUE, false); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> window(long timespan, TimeUnit unit, Scheduler scheduler, long count) { return window(timespan, unit, scheduler, count, false); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> window(long timespan, TimeUnit unit, Scheduler scheduler, long count, boolean restart) { return window(timespan, unit, scheduler, count, restart, bufferSize()); } @SchedulerSupport(SchedulerKind.CUSTOM) public final NbpObservable> 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(SchedulerKind.NONE) public final NbpObservable> window(NbpObservable boundary) { return window(boundary, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> window(NbpObservable boundary, int bufferSize) { Objects.requireNonNull(boundary, "boundary is null"); return lift(new NbpOperatorWindowBoundary(boundary, bufferSize)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> window( NbpObservable windowOpen, Function> windowClose) { return window(windowOpen, windowClose, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> window( NbpObservable 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(SchedulerKind.NONE) public final NbpObservable> window(Supplier> boundary) { return window(boundary, bufferSize()); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable> window(Supplier> boundary, int bufferSize) { Objects.requireNonNull(boundary, "boundary is null"); return lift(new NbpOperatorWindowBoundarySupplier(boundary, bufferSize)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable withLatestFrom(NbpObservable other, BiFunction combiner) { Objects.requireNonNull(other, "other is null"); Objects.requireNonNull(combiner, "combiner is null"); return lift(new NbpOperatorWithLatestFrom(combiner, other)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable zipWith(Iterable other, BiFunction zipper) { Objects.requireNonNull(other, "other is null"); Objects.requireNonNull(zipper, "zipper is null"); return create(new NbpOnSubscribeZipIterable(this, other, zipper)); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable zipWith(NbpObservable other, BiFunction zipper) { Objects.requireNonNull(other, "other is null"); return zip(this, other, zipper); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable zipWith(NbpObservable other, BiFunction zipper, boolean delayError) { return zip(this, other, zipper, delayError); } @SchedulerSupport(SchedulerKind.NONE) public final NbpObservable zipWith(NbpObservable other, BiFunction zipper, boolean delayError, int bufferSize) { return zip(this, other, zipper, delayError, bufferSize); } }