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

io.reactivex.observable.internal.operators.ObservableInternalHelper Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2016-present, RxJava Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
 * the License for the specific language governing permissions and limitations under the License.
 */
package io.reactivex.observable.internal.operators;

import java.util.List;
import java.util.concurrent.*;

import io.reactivex.common.*;
import io.reactivex.common.functions.*;
import io.reactivex.common.internal.functions.*;
import io.reactivex.observable.*;

/**
 * Helper utility class to support Observable with inner classes.
 */
public final class ObservableInternalHelper {

    private ObservableInternalHelper() {
        throw new IllegalStateException("No instances!");
    }

    static final class SimpleGenerator implements BiFunction, S> {
        final Consumer> consumer;

        SimpleGenerator(Consumer> consumer) {
            this.consumer = consumer;
        }

        @Override
        public S apply(S t1, Emitter t2) throws Exception {
            consumer.accept(t2);
            return t1;
        }
    }

    public static  BiFunction, S> simpleGenerator(Consumer> consumer) {
        return new SimpleGenerator(consumer);
    }

    static final class SimpleBiGenerator implements BiFunction, S> {
        final BiConsumer> consumer;

        SimpleBiGenerator(BiConsumer> consumer) {
            this.consumer = consumer;
        }

        @Override
        public S apply(S t1, Emitter t2) throws Exception {
            consumer.accept(t1, t2);
            return t1;
        }
    }

    public static  BiFunction, S> simpleBiGenerator(BiConsumer> consumer) {
        return new SimpleBiGenerator(consumer);
    }

    static final class ItemDelayFunction implements Function> {
        final Function> itemDelay;

        ItemDelayFunction(Function> itemDelay) {
            this.itemDelay = itemDelay;
        }

        @Override
        public ObservableSource apply(final T v) throws Exception {
            return new ObservableTake(itemDelay.apply(v), 1).map(Functions.justFunction(v)).defaultIfEmpty(v);
        }
    }

    public static  Function> itemDelay(final Function> itemDelay) {
        return new ItemDelayFunction(itemDelay);
    }


    static final class ObserverOnNext implements Consumer {
        final Observer observer;

        ObserverOnNext(Observer observer) {
            this.observer = observer;
        }

        @Override
        public void accept(T v) throws Exception {
            observer.onNext(v);
        }
    }

    static final class ObserverOnError implements Consumer {
        final Observer observer;

        ObserverOnError(Observer observer) {
            this.observer = observer;
        }

        @Override
        public void accept(Throwable v) throws Exception {
            observer.onError(v);
        }
    }

    static final class ObserverOnComplete implements Action {
        final Observer observer;

        ObserverOnComplete(Observer observer) {
            this.observer = observer;
        }

        @Override
        public void run() throws Exception {
            observer.onComplete();
        }
    }

    public static  Consumer observerOnNext(Observer observer) {
        return new ObserverOnNext(observer);
    }

    public static  Consumer observerOnError(Observer observer) {
        return new ObserverOnError(observer);
    }

    public static  Action observerOnComplete(Observer observer) {
        return new ObserverOnComplete(observer);
    }

    static final class FlatMapWithCombinerInner implements Function {
        private final BiFunction combiner;
        private final T t;

        FlatMapWithCombinerInner(BiFunction combiner, T t) {
            this.combiner = combiner;
            this.t = t;
        }

        @Override
        public R apply(U w) throws Exception {
            return combiner.apply(t, w);
        }
    }

    static final class FlatMapWithCombinerOuter implements Function> {
        private final BiFunction combiner;
        private final Function> mapper;

        FlatMapWithCombinerOuter(BiFunction combiner,
                Function> mapper) {
            this.combiner = combiner;
            this.mapper = mapper;
        }

        @Override
        public ObservableSource apply(final T t) throws Exception {
            @SuppressWarnings("unchecked")
            ObservableSource u = (ObservableSource)mapper.apply(t);
            return new ObservableMap(u, new FlatMapWithCombinerInner(combiner, t));
        }
    }

    public static  Function> flatMapWithCombiner(
            final Function> mapper,
                    final BiFunction combiner) {
        return new FlatMapWithCombinerOuter(combiner, mapper);
    }

    static final class FlatMapIntoIterable implements Function> {
        private final Function> mapper;

        FlatMapIntoIterable(Function> mapper) {
            this.mapper = mapper;
        }

        @Override
        public ObservableSource apply(T t) throws Exception {
            return new ObservableFromIterable(mapper.apply(t));
        }
    }

    public static  Function> flatMapIntoIterable(final Function> mapper) {
        return new FlatMapIntoIterable(mapper);
    }

    enum MapToInt implements Function {
        INSTANCE;
        @Override
        public Object apply(Object t) throws Exception {
            return 0;
        }
    }

    static final class RepeatWhenOuterHandler
    implements Function>, ObservableSource> {
        private final Function, ? extends ObservableSource> handler;

        RepeatWhenOuterHandler(Function, ? extends ObservableSource> handler) {
            this.handler = handler;
        }

        @Override
        public ObservableSource apply(Observable> no) throws Exception {
            return handler.apply(no.map(MapToInt.INSTANCE));
        }
    }

    public static Function>, ObservableSource> repeatWhenHandler(final Function, ? extends ObservableSource> handler) {
        return new RepeatWhenOuterHandler(handler);
    }

    public static  Callable> replayCallable(final Observable parent) {
        return new ReplayCallable(parent);
    }

    public static  Callable> replayCallable(final Observable parent, final int bufferSize) {
        return new BufferedReplayCallable(parent, bufferSize);
    }

    public static  Callable> replayCallable(final Observable parent, final int bufferSize, final long time, final TimeUnit unit, final Scheduler scheduler) {
        return new BufferedTimedReplayCallable(parent, bufferSize, time, unit, scheduler);
    }

    public static  Callable> replayCallable(final Observable parent, final long time, final TimeUnit unit, final Scheduler scheduler) {
        return new TimedReplayCallable(parent, time, unit, scheduler);
    }

    public static  Function, ObservableSource> replayFunction(final Function, ? extends ObservableSource> selector, final Scheduler scheduler) {
        return new ReplayFunction(selector, scheduler);
    }

    enum ErrorMapperFilter implements Function, Throwable>, Predicate> {
        INSTANCE;

        @Override
        public Throwable apply(Notification t) throws Exception {
            return t.getError();
        }

        @Override
        public boolean test(Notification t) throws Exception {
            return t.isOnError();
        }
    }

    static final class RetryWhenInner
    implements Function>, ObservableSource> {
        private final Function, ? extends ObservableSource> handler;

        RetryWhenInner(
                Function, ? extends ObservableSource> handler) {
            this.handler = handler;
        }

        @Override
        public ObservableSource apply(Observable> no) throws Exception {
            Observable map = no
                    .takeWhile(ErrorMapperFilter.INSTANCE)
                    .map(ErrorMapperFilter.INSTANCE);
            return handler.apply(map);
        }
    }

    public static  Function>, ObservableSource> retryWhenHandler(final Function, ? extends ObservableSource> handler) {
        return new RetryWhenInner(handler);
    }

    static final class ZipIterableFunction
    implements Function>, ObservableSource> {
        private final Function zipper;

        ZipIterableFunction(Function zipper) {
            this.zipper = zipper;
        }

        @Override
        public ObservableSource apply(List> list) {
            return Observable.zipIterable(list, zipper, false, Observable.bufferSize());
        }
    }

    public static  Function>, ObservableSource> zipIterable(final Function zipper) {
        return new ZipIterableFunction(zipper);
    }

    public static  Observable switchMapSingle(Observable source, final Function> mapper) {
        return source.switchMap(convertSingleMapperToObservableMapper(mapper), 1);
    }

    public static  Observable switchMapSingleDelayError(Observable source,
            Function> mapper) {
        return source.switchMapDelayError(convertSingleMapperToObservableMapper(mapper), 1);
    }

    private static  Function> convertSingleMapperToObservableMapper(
            final Function> mapper) {
        ObjectHelper.requireNonNull(mapper, "mapper is null");
        return new ObservableMapper(mapper);
    }

    static final class ObservableMapper implements Function> {

        final Function> mapper;

        ObservableMapper(Function> mapper) {
            this.mapper = mapper;
        }

        @Override
        public Observable apply(T t) throws Exception {
            return RxJavaObservablePlugins.onAssembly(new SingleToObservable(
                ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null value")));
        }

    }

    static final class ReplayCallable implements Callable> {
        private final Observable parent;

        ReplayCallable(Observable parent) {
            this.parent = parent;
        }

        @Override
        public ConnectableObservable call() {
            return parent.replay();
        }
    }

    static final class BufferedReplayCallable implements Callable> {
        private final Observable parent;
        private final int bufferSize;

        BufferedReplayCallable(Observable parent, int bufferSize) {
            this.parent = parent;
            this.bufferSize = bufferSize;
        }

        @Override
        public ConnectableObservable call() {
            return parent.replay(bufferSize);
        }
    }

    static final class BufferedTimedReplayCallable implements Callable> {
        private final Observable parent;
        private final int bufferSize;
        private final long time;
        private final TimeUnit unit;
        private final Scheduler scheduler;

        BufferedTimedReplayCallable(Observable parent, int bufferSize, long time, TimeUnit unit, Scheduler scheduler) {
            this.parent = parent;
            this.bufferSize = bufferSize;
            this.time = time;
            this.unit = unit;
            this.scheduler = scheduler;
        }

        @Override
        public ConnectableObservable call() {
            return parent.replay(bufferSize, time, unit, scheduler);
        }
    }

    static final class TimedReplayCallable implements Callable> {
        private final Observable parent;
        private final long time;
        private final TimeUnit unit;
        private final Scheduler scheduler;

        TimedReplayCallable(Observable parent, long time, TimeUnit unit, Scheduler scheduler) {
            this.parent = parent;
            this.time = time;
            this.unit = unit;
            this.scheduler = scheduler;
        }

        @Override
        public ConnectableObservable call() {
            return parent.replay(time, unit, scheduler);
        }
    }

    static final class ReplayFunction implements Function, ObservableSource> {
        private final Function, ? extends ObservableSource> selector;
        private final Scheduler scheduler;

        ReplayFunction(Function, ? extends ObservableSource> selector, Scheduler scheduler) {
            this.selector = selector;
            this.scheduler = scheduler;
        }

        @Override
        public ObservableSource apply(Observable t) throws Exception {
            return Observable.wrap(selector.apply(t)).observeOn(scheduler);
        }
    }
}