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

io.reactivex.internal.operators.flowable.FlowableInternalHelper Maven / Gradle / Ivy

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

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

import org.reactivestreams.*;

import io.reactivex.*;
import io.reactivex.flowables.ConnectableFlowable;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.Functions;

/**
 * Helper utility class to support Flowable with inner classes.
 */
public final class FlowableInternalHelper {

    /** Utility class. */
    private FlowableInternalHelper() {
        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 Publisher apply(final T v) throws Exception {
            return new FlowableTake(itemDelay.apply(v), 1).map(Functions.justFunction(v)).defaultIfEmpty(v);
        }
    }

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

    static final class SubscriberOnNext implements Consumer {
        final Subscriber subscriber;

        SubscriberOnNext(Subscriber subscriber) {
            this.subscriber = subscriber;
        }

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

    static final class SubscriberOnError implements Consumer {
        final Subscriber subscriber;

        SubscriberOnError(Subscriber subscriber) {
            this.subscriber = subscriber;
        }

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

    static final class SubscriberOnComplete implements Action {
        final Subscriber subscriber;

        SubscriberOnComplete(Subscriber subscriber) {
            this.subscriber = subscriber;
        }

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

    public static  Consumer subscriberOnNext(Subscriber subscriber) {
        return new SubscriberOnNext(subscriber);
    }

    public static  Consumer subscriberOnError(Subscriber subscriber) {
        return new SubscriberOnError(subscriber);
    }

    public static  Action subscriberOnComplete(Subscriber subscriber) {
        return new SubscriberOnComplete(subscriber);
    }

    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 Publisher apply(final T t) throws Exception {
            @SuppressWarnings("unchecked")
            Publisher u = (Publisher)mapper.apply(t);
            return new FlowableMap(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 Publisher apply(T t) throws Exception {
            return new FlowableFromIterable(mapper.apply(t));
        }
    }

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

    public static  Callable> replayCallable(final Flowable parent) {
        return new Callable>() {
            @Override
            public ConnectableFlowable call() {
                return parent.replay();
            }
        };
    }

    public static  Callable> replayCallable(final Flowable parent, final int bufferSize) {
        return new Callable>() {
            @Override
            public ConnectableFlowable call() {
                return parent.replay(bufferSize);
            }
        };
    }

    public static  Callable> replayCallable(final Flowable parent, final int bufferSize, final long time, final TimeUnit unit, final Scheduler scheduler) {
        return new Callable>() {
            @Override
            public ConnectableFlowable call() {
                return parent.replay(bufferSize, time, unit, scheduler);
            }
        };
    }

    public static  Callable> replayCallable(final Flowable parent, final long time, final TimeUnit unit, final Scheduler scheduler) {
        return new Callable>() {
            @Override
            public ConnectableFlowable call() {
                return parent.replay(time, unit, scheduler);
            }
        };
    }

    public static  Function, Publisher> replayFunction(final Function, ? extends Publisher> selector, final Scheduler scheduler) {
        return new Function, Publisher>() {
            @Override
            public Publisher apply(Flowable t) throws Exception {
                return Flowable.fromPublisher(selector.apply(t)).observeOn(scheduler);
            }
        };
    }

    public enum RequestMax implements Consumer {
        INSTANCE;
        @Override
        public void accept(Subscription t) throws Exception {
            t.request(Long.MAX_VALUE);
        }
    }

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

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

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

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

}