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

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

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

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.rxjava3.internal.operators.flowable;

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

import org.reactivestreams.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.flowables.ConnectableFlowable;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.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 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 Publisher apply(final T v) throws Throwable {
            Publisher p = Objects.requireNonNull(itemDelay.apply(v), "The itemDelay returned a null Publisher");
            return new FlowableTakePublisher<>(p, 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) {
            subscriber.onNext(v);
        }
    }

    static final class SubscriberOnError implements Consumer {
        final Subscriber subscriber;

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

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

    static final class SubscriberOnComplete implements Action {
        final Subscriber subscriber;

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

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

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

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

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

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

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

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

    static final class ReplaySupplier implements Supplier> {

        final Flowable parent;

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

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

    static final class BufferedReplaySupplier implements Supplier> {

        final Flowable parent;

        final int bufferSize;

        final boolean eagerTruncate;

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

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

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

        final boolean eagerTruncate;

        BufferedTimedReplay(Flowable 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 ConnectableFlowable get() {
            return parent.replay(bufferSize, time, unit, scheduler, eagerTruncate);
        }
    }

    static final class TimedReplay implements Supplier> {
        private final Flowable parent;
        private final long time;
        private final TimeUnit unit;
        private final Scheduler scheduler;

        final boolean eagerTruncate;

        TimedReplay(Flowable 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 ConnectableFlowable get() {
            return parent.replay(time, unit, scheduler, eagerTruncate);
        }
    }
}