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

io.atleon.core.GroupFlux Maven / Gradle / Ivy

package io.atleon.core;

import org.reactivestreams.Publisher;
import reactor.core.observability.SignalListenerFactory;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * A wrapped {@link Flux} of grouped {@link AloFlux} sequences. Exposes convenience methods
 * (prefixed with "inner") to transform the emitted inner grouped fluxes. By convention, in order
 * to do useful operations on the underlying sequence(s) (like subscribe to them), any instance
 * must be converted (back) to an {@link AloFlux} through any of the "flatMapAlo" methods.
 *
 * @param  the type of groups emitted by this {@link GroupFlux}
 * @param  the type of {@link AloFlux} emitted as each group
 */
public class GroupFlux {

    private final Flux> wrapped;

    private final int cardinality;

    GroupFlux(Flux> wrapped, int cardinality) {
        this.wrapped = wrapped;
        this.cardinality = cardinality;
    }

    static  GroupFlux create(Flux> flux, int cardinality) {
        return new GroupFlux<>(flux, cardinality);
    }

    /**
     * Return the underlying Flux backing this GroupFlux
     */
    public Flux> unwrap() {
        return wrapped;
    }

    /**
     * Convenience method for applying {@link AloFlux#doOnNext(Consumer)} to each inner grouped
     * sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux innerDoOnNext(Consumer onNext) {
        return map(group -> group.doOnNext(onNext));
    }

    /**
     * Convenience method for applying {@link AloFlux#filter(Predicate)} to each inner grouped
     * sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux innerFilter(Predicate predicate) {
        return map(group -> group.filter(predicate));
    }

    /**
     * Convenience method for applying {@link AloFlux#map(Function)} to each inner grouped
     * sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public  GroupFlux innerMap(Function mapper) {
        return map(group -> group.map(mapper));
    }

    /**
     * Convenience method for applying {@link AloFlux#mapNotNull(Function)} to each inner grouped
     * sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public  GroupFlux innerMapNotNull(Function mapper) {
        return map(group -> group.mapNotNull(mapper));
    }

    /**
     * Convenience method for applying {@link AloFlux#mapPresent(Function)}} to each inner grouped
     * sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public  GroupFlux innerMapPresent(Function> mapper) {
        return map(group -> group.mapPresent(mapper));
    }

    /**
     * Convenience method for applying {@link AloFlux#consume(Consumer)}} to each inner grouped
     * sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux innerConsume(Consumer consumer) {
        return map(group -> group.consume(consumer));
    }

    /**
     * Convenience method for applying {@link AloFlux#concatMap(Function)} to each inner grouped
     * sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public  GroupFlux innerConcatMap(Function> mapper) {
        return map(group -> group.concatMap(mapper));
    }

    /**
     * Convenience method for applying {@link AloFlux#concatMap(Function, int)} to each inner
     * grouped sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public  GroupFlux innerConcatMap(Function> mapper, int prefetch) {
        return map(group -> group.concatMap(mapper, prefetch));
    }

    /**
     * Convenience method for applying {@link AloFlux#flatMapIterable(Function)} to each inner
     * grouped sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public  GroupFlux innerFlatMapIterable(Function> mapper) {
        return map(group -> group.flatMapIterable(mapper));
    }

    /**
     * Convenience method for applying {@link AloFlux#bufferTimeout(int, Duration)} to each inner
     * grouped sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux> innerBufferTimeout(int maxSize, Duration maxTime) {
        return map(group -> group.bufferTimeout(maxSize, maxTime));
    }

    /**
     * Convenience method for applying {@link AloFlux#bufferTimeout(int, Duration, Scheduler)} to
     * each inner grouped sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux> innerBufferTimeout(int maxSize, Duration maxTime, Scheduler scheduler) {
        return map(group -> group.bufferTimeout(maxSize, maxTime, scheduler));
    }

    /**
     * Convenience method for applying {@link AloFlux#bufferTimeout(int, Duration, boolean)} to
     * each inner grouped sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux> innerBufferTimeout(int maxSize, Duration maxTime, boolean fairBackpressure) {
        return map(group -> group.bufferTimeout(maxSize, maxTime, fairBackpressure));
    }

    /**
     * Convenience method for applying {@link AloFlux#bufferTimeout(int, Duration, Scheduler, boolean)}
     * to each inner grouped sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux> innerBufferTimeout(
        int maxSize,
        Duration maxTime,
        Scheduler scheduler,
        boolean fairBackpressure
    ) {
        return map(group -> group.bufferTimeout(maxSize, maxTime, scheduler, fairBackpressure));
    }

    /**
     * Convenience method for applying {@link AloFlux#publishOn(Scheduler)} to each inner grouped
     * sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux innerPublishOn(Scheduler scheduler) {
        return map(group -> group.publishOn(scheduler));
    }

    /**
     * Convenience method for applying {@link AloFlux#publishOn(Scheduler, int)} to each inner
     * grouped sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux innerPublishOn(Scheduler scheduler, int prefetch) {
        return map(group -> group.publishOn(scheduler, prefetch));
    }

    /**
     * Convenience method for applying {@link AloFlux#tap(SignalListenerFactory)} to each inner
     * grouped sequence.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux innerTap(SignalListenerFactory, ?> signalListenerFactory) {
        return map(group -> group.tap(signalListenerFactory));
    }

    /**
     * Apply a cumulative limit on the rate at which items are emitted across all inner
     * grouped sequences. Especially useful when interacting with resource-constrained I/O
     * dependencies.
     *
     * @return a transformed {@link GroupFlux}
     */
    public GroupFlux limitPerSecond(double limitPerSecond) {
        RateLimitingConfig config = new RateLimitingConfig(limitPerSecond);
        return map(new RateLimitingTransformer<>(config, Schedulers.boundedElastic()));
    }

    /**
     * @deprecated Use {{@link #map(Function)}} instead
     */
    @Deprecated
    public  GroupFlux mapExtended(Function, ? extends Publisher>> mapper) {
        return map(mapper);
    }

    /**
     * Transform the items emitted by this {@link GroupFlux} by applying a synchronous function
     * to each item.
     *
     * @param mapper the synchronous transforming {@link Function}
     * @param     the transformed type
     * @return a transformed {@link GroupFlux}
     * @see Flux#map(Function)
     */
    public  GroupFlux map(Function, ? extends Publisher>> mapper) {
        return new GroupFlux<>(wrapped.map(group -> group.transformGrouped(mapper)), cardinality);
    }

    /**
     * Flatten each inner grouped Publisher in to a single {@link AloFlux}, allowing values to
     * interleave
     *
     * @return a new {@link AloFlux} of the merged results
     */
    public AloFlux flatMapAlo() {
        return flatMapAlo(Function.identity());
    }

    /**
     * Transform the elements emitted by this {@link GroupFlux} asynchronously into Publishers of
     * {@link Alo}, then flatten these inner Publishers in to a single {@link AloFlux}, which
     * allows them to interleave
     *
     * @param mapper {@link Function} to transform each emission into {@link Publisher} of {@link Alo}
     * @param     the type referenced by each resulting {@link Alo}
     * @return a new {@link AloFlux} of the merged results
     * @see Flux#flatMap(Function)
     */
    public  AloFlux flatMapAlo(Function, ? extends Publisher>> mapper) {
        return wrapped.flatMap(mapper, cardinality).as(AloFlux::wrap);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy