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

com.aol.cyclops.reactor.transformer.FluxT Maven / Gradle / Ivy

There is a newer version: 9.0.0-MI7
Show newest version
package com.aol.cyclops.reactor.transformer;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Stream;

import org.jooq.lambda.Seq;
import org.jooq.lambda.tuple.Tuple2;
import org.jooq.lambda.tuple.Tuple3;
import org.jooq.lambda.tuple.Tuple4;
import org.reactivestreams.Publisher;

import com.aol.cyclops.Matchables;
import com.aol.cyclops.Monoid;
import com.aol.cyclops.control.AnyM;
import com.aol.cyclops.control.Matchable.CheckValue1;
import com.aol.cyclops.control.ReactiveSeq;
import com.aol.cyclops.control.Trampoline;
import com.aol.cyclops.control.monads.transformers.values.FoldableTransformerSeq;
import com.aol.cyclops.data.collections.extensions.standard.ListX;
import com.aol.cyclops.reactor.Reactor;
import com.aol.cyclops.types.MonadicValue;
import com.aol.cyclops.types.anyM.AnyMSeq;
import com.aol.cyclops.types.anyM.AnyMValue;

import reactor.core.publisher.Flux;

/**
 * Monad Transformer for RxJava Fluxs
 * 
 * FluxT consists of an AnyM instance that in turns wraps anoter Monad type that contains an Flux
 * 
 * FluxT>>>
 * 
 * FluxT allows the deeply wrapped Flux to be manipulating within it's nested /contained context
 * @author johnmcclean
 *
 * @param 
 */
public interface FluxT extends FoldableTransformerSeq {

    public  FluxT unitIterator(Iterator it);

    public  FluxT unit(R t);

    public  FluxT empty();

    public  FluxT flatMap(Function> f);

    default Flux> fluxOfFlux() {
        return Flux.from(this.unwrap()
                             .stream());
    }

    /**
     * @return The wrapped AnyM
     */
    public AnyM> unwrap();

    /**
     * Peek at the current value of the Flux
     * 
     * {@code 
     *    FluxT.of(AnyM.fromFlux(Arrays.asFlux(10))
     *             .peek(System.out::println);
     *             
     *     //prints 10        
     * }
     * 
* * @param peek Consumer to accept current value of Flux * @return FluxT with peek call */ public FluxT peek(Consumer peek); /** * Filter the wrapped Flux *
     * {@code 
     *    FluxT.of(AnyM.fromFlux(Arrays.asFlux(10,11))
     *             .filter(t->t!=10);
     *             
     *     //FluxT>>
     * }
     * 
* @param test Predicate to filter the wrapped Flux * @return FluxT that applies the provided filter */ public FluxT filter(Predicate test); /** * Map the wrapped Flux * *
     * {@code 
     *  FluxT.of(AnyM.fromFlux(Arrays.asFlux(10))
     *             .map(t->t=t+1);
     *  
     *  
     *  //FluxT>>
     * }
     * 
* * @param f Mapping function for the wrapped Flux * @return FluxT that applies the map function to the wrapped Flux */ public FluxT map(Function f); /** * Flat Map the wrapped Flux *
     * {@code 
     *  FluxT.of(AnyM.fromFlux(Flux.just(10))
     *             .flatMap(t->Flux.empty());
     *  
     *  
     *  //FluxT>>
     * }
     * 
* @param f FlatMap function * @return FluxT that applies the flatMap function to the wrapped Flux */ default FluxT bind(Function> f) { return of(unwrap().map(Flux -> Flux.flatMap(a -> Flux.from(f.apply(a) .unwrap() .stream())) . flatMap(a -> a))); } /** * Lift a function into one that accepts and returns an FluxT * This allows multiple monad types to add functionality to existing functions and methods * * e.g. to add iteration handling (via Flux) and nullhandling (via Optional) to an existing function *
     * {@code 
    	Function add2 = i -> i+2;
    	Function, FluxT> optTAdd2 = FluxT.lift(add2);
    	
    	Flux nums = Flux.of(1,2);
    	AnyM> Flux = AnyM.fromOptional(Optional.of(nums));
    	
    	List results = optTAdd2.apply(FluxT.of(Flux))
    									.unwrap()
    									.>>unwrap()
    									.get()
    									.collect(Collectors.toList());
    	//Flux.of(3,4);
     * 
     * 
     * }
* * * @param fn Function to enhance with functionality from Flux and another monad type * @return Function that accepts and returns an FluxT */ public static Function, FluxT> lift(Function fn) { return optTu -> optTu.map(input -> fn.apply(input)); } /** * Construct an FluxT from an AnyM that contains a monad type that contains type other than Flux * The values in the underlying monad will be mapped to Flux * * @param anyM AnyM that doesn't contain a monad wrapping an Flux * @return FluxT */ public static FluxT fromAnyM(AnyM anyM) { return of(anyM.map(Flux::just)); } /** * Create a FluxT from an AnyM that wraps a monad containing a Flux * * @param monads * @return */ public static FluxT of(AnyM> monads) { return Matchables.anyM(monads) .visit(v -> FluxTValue.of(v), s -> FluxTSeq.of(s)); } public static FluxTValue fromAnyMValue(AnyMValue anyM) { return FluxTValue.fromAnyM(anyM); } public static FluxTSeq fromAnyMSeq(AnyMSeq anyM) { return FluxTSeq.fromAnyM(anyM); } public static FluxTSeq fromIterable(Iterable> iterableOfFluxs) { return FluxTSeq.of(AnyM.fromIterable(iterableOfFluxs)); } public static FluxTSeq fromFlux(Flux> FluxOfFluxs) { return FluxTSeq.of(Reactor.flux(FluxOfFluxs)); } public static FluxTSeq fromPublisher(Publisher> publisherOfFluxs) { return FluxTSeq.of(AnyM.fromPublisher(publisherOfFluxs)); } public static >> FluxTValue fromValue(V monadicValue) { return FluxTValue.fromValue(monadicValue); } public static FluxTValue fromOptional(Optional> optional) { return FluxTValue.of(AnyM.fromOptional(optional)); } public static FluxTValue fromFuture(CompletableFuture> future) { return FluxTValue.of(AnyM.fromCompletableFuture(future)); } public static FluxTValue fromIterableValue(Iterable> iterableOfFluxs) { return FluxTValue.of(AnyM.fromIterableValue(iterableOfFluxs)); } public static FluxTSeq emptyFlux() { return FluxT.fromIterable(ReactiveSeq.empty()); } public Flux Flux(); /* * (non-Javadoc) * * @see com.aol.cyclops.types.Functor#cast(java.lang.Class) */ @Override default FluxT cast(Class type) { return (FluxT) FoldableTransformerSeq.super.cast(type); } /* * (non-Javadoc) * * @see * com.aol.cyclops.types.Functor#trampoline(java.util.function.Function) */ @Override default FluxT trampoline(Function> mapper) { return (FluxT) FoldableTransformerSeq.super.trampoline(mapper); } /* * (non-Javadoc) * * @see * com.aol.cyclops.types.Functor#patternMatch(java.util.function.Function, * java.util.function.Supplier) */ @Override default FluxT patternMatch(Function, CheckValue1> case1, Supplier otherwise) { return (FluxT) FoldableTransformerSeq.super.patternMatch(case1, otherwise); } /* * (non-Javadoc) * * @see com.aol.cyclops.types.Filterable#ofType(java.lang.Class) */ @Override default FluxT ofType(Class type) { return (FluxT) FoldableTransformerSeq.super.ofType(type); } /* * (non-Javadoc) * * @see * com.aol.cyclops.types.Filterable#filterNot(java.util.function.Predicate) */ @Override default FluxT filterNot(Predicate fn) { return (FluxT) FoldableTransformerSeq.super.filterNot(fn); } /* * (non-Javadoc) * * @see com.aol.cyclops.types.Filterable#notNull() */ @Override default FluxT notNull() { return (FluxT) FoldableTransformerSeq.super.notNull(); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#combine * (java.util.function.BiPredicate, java.util.function.BinaryOperator) */ @Override default FluxT combine(BiPredicate predicate, BinaryOperator op) { return (FluxT) FoldableTransformerSeq.super.combine(predicate, op); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#cycle( * int) */ @Override default FluxT cycle(int times) { return (FluxT) FoldableTransformerSeq.super.cycle(times); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#cycle( * com.aol.cyclops.Monoid, int) */ @Override default FluxT cycle(Monoid m, int times) { return (FluxT) FoldableTransformerSeq.super.cycle(m, times); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * cycleWhile(java.util.function.Predicate) */ @Override default FluxT cycleWhile(Predicate predicate) { return (FluxT) FoldableTransformerSeq.super.cycleWhile(predicate); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * cycleUntil(java.util.function.Predicate) */ @Override default FluxT cycleUntil(Predicate predicate) { return (FluxT) FoldableTransformerSeq.super.cycleUntil(predicate); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#zip( * java.lang.Iterable, java.util.function.BiFunction) */ @Override default FluxT zip(Iterable other, BiFunction zipper) { return (FluxT) FoldableTransformerSeq.super.zip(other, zipper); } @Override default FluxT zip(Seq other, BiFunction zipper) { return (FluxT) FoldableTransformerSeq.super.zip(other, zipper); } @Override default FluxT zip(Stream other, BiFunction zipper) { return (FluxT) FoldableTransformerSeq.super.zip(other, zipper); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#zip( * java.util.Flux.Flux) */ @Override default FluxT> zip(Stream other) { return (FluxT) FoldableTransformerSeq.super.zip(other); } @Override default FluxT> zip(Seq other) { return (FluxT) FoldableTransformerSeq.super.zip(other); } @Override default FluxT> zip(Iterable other) { return (FluxT) FoldableTransformerSeq.super.zip(other); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#zip3( * java.util.Flux.Flux, java.util.Flux.Flux) */ @Override default FluxT> zip3(Stream second, Stream third) { return (FluxT) FoldableTransformerSeq.super.zip3(second, third); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#zip4( * java.util.Flux.Flux, java.util.Flux.Flux, java.util.Flux.Flux) */ @Override default FluxT> zip4(Stream second, Stream third, Stream fourth) { return (FluxT) FoldableTransformerSeq.super.zip4(second, third, fourth); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * zipWithIndex() */ @Override default FluxT> zipWithIndex() { return (FluxT>) FoldableTransformerSeq.super.zipWithIndex(); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#sliding * (int) */ @Override default FluxT> sliding(int windowSize) { return (FluxT>) FoldableTransformerSeq.super.sliding(windowSize); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#sliding * (int, int) */ @Override default FluxT> sliding(int windowSize, int increment) { return (FluxT>) FoldableTransformerSeq.super.sliding(windowSize, increment); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#grouped * (int, java.util.function.Supplier) */ @Override default > FluxT grouped(int size, Supplier supplier) { return (FluxT) FoldableTransformerSeq.super.grouped(size, supplier); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * groupedUntil(java.util.function.Predicate) */ @Override default FluxT> groupedUntil(Predicate predicate) { return (FluxT>) FoldableTransformerSeq.super.groupedUntil(predicate); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * groupedStatefullyWhile(java.util.function.BiPredicate) */ @Override default FluxT> groupedStatefullyUntil(BiPredicate, ? super T> predicate) { return (FluxT>) FoldableTransformerSeq.super.groupedStatefullyUntil(predicate); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * groupedWhile(java.util.function.Predicate) */ @Override default FluxT> groupedWhile(Predicate predicate) { return (FluxT>) FoldableTransformerSeq.super.groupedWhile(predicate); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * groupedWhile(java.util.function.Predicate, java.util.function.Supplier) */ @Override default > FluxT groupedWhile(Predicate predicate, Supplier factory) { return (FluxT) FoldableTransformerSeq.super.groupedWhile(predicate, factory); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * groupedUntil(java.util.function.Predicate, java.util.function.Supplier) */ @Override default > FluxT groupedUntil(Predicate predicate, Supplier factory) { return (FluxT) FoldableTransformerSeq.super.groupedUntil(predicate, factory); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#grouped * (int) */ @Override default FluxT> grouped(int groupSize) { return (FluxT>) FoldableTransformerSeq.super.grouped(groupSize); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#grouped * (java.util.function.Function, java.util.Flux.Collector) */ @Override default FluxT> grouped(Function classifier, Collector downFlux) { return (FluxT) FoldableTransformerSeq.super.grouped(classifier, downFlux); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#grouped * (java.util.function.Function) */ @Override default FluxT>> grouped(Function classifier) { return (FluxT) FoldableTransformerSeq.super.grouped(classifier); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * distinct() */ @Override default FluxT distinct() { return (FluxT) FoldableTransformerSeq.super.distinct(); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * scanLeft(com.aol.cyclops.Monoid) */ @Override default FluxT scanLeft(Monoid monoid) { return (FluxT) FoldableTransformerSeq.super.scanLeft(monoid); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * scanLeft(java.lang.Object, java.util.function.BiFunction) */ @Override default FluxT scanLeft(U seed, BiFunction function) { return (FluxT) FoldableTransformerSeq.super.scanLeft(seed, function); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * scanRight(com.aol.cyclops.Monoid) */ @Override default FluxT scanRight(Monoid monoid) { return (FluxT) FoldableTransformerSeq.super.scanRight(monoid); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * scanRight(java.lang.Object, java.util.function.BiFunction) */ @Override default FluxT scanRight(U identity, BiFunction combiner) { return (FluxT) FoldableTransformerSeq.super.scanRight(identity, combiner); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#sorted( * ) */ @Override default FluxT sorted() { return (FluxT) FoldableTransformerSeq.super.sorted(); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#sorted( * java.util.Comparator) */ @Override default FluxT sorted(Comparator c) { return (FluxT) FoldableTransformerSeq.super.sorted(c); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * takeWhile(java.util.function.Predicate) */ @Override default FluxT takeWhile(Predicate p) { return (FluxT) FoldableTransformerSeq.super.takeWhile(p); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * dropWhile(java.util.function.Predicate) */ @Override default FluxT dropWhile(Predicate p) { return (FluxT) FoldableTransformerSeq.super.dropWhile(p); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * takeUntil(java.util.function.Predicate) */ @Override default FluxT takeUntil(Predicate p) { return (FluxT) FoldableTransformerSeq.super.takeUntil(p); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * dropUntil(java.util.function.Predicate) */ @Override default FluxT dropUntil(Predicate p) { return (FluxT) FoldableTransformerSeq.super.dropUntil(p); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * dropRight(int) */ @Override default FluxT dropRight(int num) { return (FluxT) FoldableTransformerSeq.super.dropRight(num); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * takeRight(int) */ @Override default FluxT takeRight(int num) { return (FluxT) FoldableTransformerSeq.super.takeRight(num); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#skip( * long) */ @Override default FluxT skip(long num) { return (FluxT) FoldableTransformerSeq.super.skip(num); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * skipWhile(java.util.function.Predicate) */ @Override default FluxT skipWhile(Predicate p) { return (FluxT) FoldableTransformerSeq.super.skipWhile(p); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * skipUntil(java.util.function.Predicate) */ @Override default FluxT skipUntil(Predicate p) { return (FluxT) FoldableTransformerSeq.super.skipUntil(p); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#limit( * long) */ @Override default FluxT limit(long num) { return (FluxT) FoldableTransformerSeq.super.limit(num); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * limitWhile(java.util.function.Predicate) */ @Override default FluxT limitWhile(Predicate p) { return (FluxT) FoldableTransformerSeq.super.limitWhile(p); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * limitUntil(java.util.function.Predicate) */ @Override default FluxT limitUntil(Predicate p) { return (FluxT) FoldableTransformerSeq.super.limitUntil(p); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * intersperse(java.lang.Object) */ @Override default FluxT intersperse(T value) { return (FluxT) FoldableTransformerSeq.super.intersperse(value); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#reverse * () */ @Override default FluxT reverse() { return (FluxT) FoldableTransformerSeq.super.reverse(); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#shuffle * () */ @Override default FluxT shuffle() { return (FluxT) FoldableTransformerSeq.super.shuffle(); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * skipLast(int) */ @Override default FluxT skipLast(int num) { return (FluxT) FoldableTransformerSeq.super.skipLast(num); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * limitLast(int) */ @Override default FluxT limitLast(int num) { return (FluxT) FoldableTransformerSeq.super.limitLast(num); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#onEmpty * (java.lang.Object) */ @Override default FluxT onEmpty(T value) { return (FluxT) FoldableTransformerSeq.super.onEmpty(value); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * onEmptyGet(java.util.function.Supplier) */ @Override default FluxT onEmptyGet(Supplier supplier) { return (FluxT) FoldableTransformerSeq.super.onEmptyGet(supplier); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.TransformerSeq# * onEmptyThrow(java.util.function.Supplier) */ @Override default FluxT onEmptyThrow(Supplier supplier) { return (FluxT) FoldableTransformerSeq.super.onEmptyThrow(supplier); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#shuffle * (java.util.Random) */ @Override default FluxT shuffle(Random random) { return (FluxT) FoldableTransformerSeq.super.shuffle(random); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#slice( * long, long) */ @Override default FluxT slice(long from, long to) { return (FluxT) FoldableTransformerSeq.super.slice(from, to); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.TransformerSeq#sorted( * java.util.function.Function) */ @Override default > FluxT sorted(Function function) { return (FluxT) FoldableTransformerSeq.super.sorted(function); } }