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

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

There is a newer version: 3.1.9
Show 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.rxjava3.internal.operators.observable;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.functions.*;
import io.reactivex.rxjava3.observables.ConnectableObservable;

/**
 * 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 Throwable {
            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 Throwable {
            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 Throwable {
            ObservableSource o = Objects.requireNonNull(itemDelay.apply(v), "The itemDelay returned a null ObservableSource");
            return new ObservableTake<>(o, 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) {
            observer.onNext(v);
        }
    }

    static final class ObserverOnError implements Consumer {
        final Observer observer;

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

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

    static final class ObserverOnComplete implements Action {
        final Observer observer;

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

        @Override
        public void run() {
            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 Throwable {
            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 Throwable {
            @SuppressWarnings("unchecked")
            ObservableSource u = (ObservableSource)Objects.requireNonNull(mapper.apply(t), "The mapper returned a null ObservableSource");
            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 Throwable {
            return new ObservableFromIterable<>(Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Iterable"));
        }
    }

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

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

    public static  Supplier> replaySupplier(final Observable parent) {
        return new ReplaySupplier<>(parent);
    }

    public static  Supplier> replaySupplier(final Observable parent, final int bufferSize, boolean eagerTruncate) {
        return new BufferedReplaySupplier<>(parent, bufferSize, eagerTruncate);
    }

    public static  Supplier> replaySupplier(final Observable parent, final int bufferSize, final long time, final TimeUnit unit, final Scheduler scheduler, boolean eagerTruncate) {
        return new BufferedTimedReplaySupplier<>(parent, bufferSize, time, unit, scheduler, eagerTruncate);
    }

    public static  Supplier> replaySupplier(final Observable parent, final long time, final TimeUnit unit, final Scheduler scheduler, boolean eagerTruncate) {
        return new TimedReplayCallable<>(parent, time, unit, scheduler, eagerTruncate);
    }

    static final class ReplaySupplier implements Supplier> {
        private final Observable parent;

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

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

    static final class BufferedReplaySupplier implements Supplier> {
        final Observable parent;
        final int bufferSize;

        final boolean eagerTruncate;

        BufferedReplaySupplier(Observable parent, int bufferSize, boolean eagerTruncate) {
            this.parent = parent;
            this.bufferSize = bufferSize;
            this.eagerTruncate = eagerTruncate;
        }

        @Override
        public ConnectableObservable get() {
            return parent.replay(bufferSize, eagerTruncate);
        }
    }

    static final class BufferedTimedReplaySupplier implements Supplier> {
        final Observable parent;
        final int bufferSize;
        final long time;
        final TimeUnit unit;
        final Scheduler scheduler;

        final boolean eagerTruncate;

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

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

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

        final boolean eagerTruncate;

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

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