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

com.aol.cyclops.reactor.transformer.FluxTSeq 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.Random;
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 com.aol.cyclops.Monoid;
import com.aol.cyclops.control.AnyM;
import com.aol.cyclops.control.ReactiveSeq;
import com.aol.cyclops.data.collections.extensions.standard.ListX;
import com.aol.cyclops.types.IterableFoldable;
import com.aol.cyclops.types.Traversable;
import com.aol.cyclops.types.anyM.AnyMSeq;
import com.aol.cyclops.types.stream.CyclopsCollectable;

import reactor.core.publisher.Flux;

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

    private final AnyMSeq> run;

    private FluxTSeq(final AnyMSeq> run) {
        this.run = (AnyMSeq) (run);
    }

    public boolean isSeqPresent() {
        return !run.isEmpty();
    }

    /**
     * @return The wrapped AnyM
     */
    public AnyMSeq> unwrap() {
        return run;
    }

    /**
     * Peek at the current value of the Stream
     * 
     * {@code 
     *    FluxT.fromIterable(ListX.of(Stream.of(10))
     *             .peek(System.out::println);
     *             
     *     //prints 10        
     * }
     * 
* * @param peek Consumer to accept current value of Stream * @return FluxT with peek call */ public FluxTSeq peek(Consumer peek) { return map(a -> { peek.accept(a); return a; }); } /** * Filter the wrapped Stream *
     * {@code 
     *   FluxT.fromIterable(ListX.of(Stream.of(10,11))
     *          .filter(t->t!=10);
     *             
     *     //FluxT<[11]>>
     * }
     * 
* @param test Predicate to filter the wrapped Stream * @return FluxT that applies the provided filter */ public FluxTSeq filter(Predicate test) { return of(run.map(stream -> stream.filter(i -> test.test(i)))); } /** * Map the wrapped Stream * *
     * {@code 
     *  FluxT.of(AnyM.fromStream(Arrays.asStream(10))
     *             .map(t->t=t+1);
     *  
     *  
     *  //FluxT>>
     * }
     * 
* * @param f Mapping function for the wrapped Stream * @return FluxT that applies the map function to the wrapped Stream */ public FluxTSeq map(Function f) { return new FluxTSeq( run.map(o -> o.map(i -> f.apply(i)))); } /** * Flat Map the wrapped Stream *
     * {@code 
     *  FluxT.of(AnyM.fromStream(Arrays.asStream(10))
     *             .flatMap(t->Stream.empty();
     *  
     *  
     *  //FluxT>>
     * }
     * 
* @param f FlatMap function * @return FluxT that applies the flatMap function to the wrapped Stream */ public FluxTSeq flatMapT(Function> f) { return of(run.map(stream -> stream.flatMap(a -> Flux.from(f.apply(a).run.stream())) . flatMap(a -> a))); } public FluxTSeq flatMap(Function> f) { return new FluxTSeq( run.map(o -> o.flatMap(f))); } /** * 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 Stream) and nullhandling (via Optional) to an existing function *
     * {@code 
    	Function add2 = i -> i+2;
    	Function, FluxT> optTAdd2 = FluxT.lift(add2);
    	
    	Stream nums = Stream.of(1,2);
    	AnyM> stream = AnyM.fromOptional(Optional.of(nums));
    	
    	List results = optTAdd2.apply(FluxT.of(stream))
    									.unwrap()
    									.>>unwrap()
    									.get()
    									.collect(Collectors.toList());
    	//Stream.of(3,4);
     * 
     * 
     * }
* * * @param fn Function to enhance with functionality from Stream and another monad type * @return Function that accepts and returns an FluxT */ public static Function, FluxTSeq> 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 Stream * The values in the underlying monad will be mapped to Stream * * @param anyM AnyM that doesn't contain a monad wrapping an Stream * @return FluxT */ public static FluxTSeq fromAnyM(AnyMSeq anyM) { return of(anyM.map(Flux::just)); } /** * Create a FluxT from an AnyM that wraps a monad containing a Stream * * @param monads * @return */ public static FluxTSeq of(AnyMSeq> monads) { return new FluxTSeq<>( monads); } public static FluxTSeq of(Flux monads) { return FluxT.fromIterable(ReactiveSeq.of(monads)); } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ public String toString() { return String.format("FluxTSeq[%s]", run); } /* * (non-Javadoc) * * @see com.aol.cyclops.types.Unit#unit(java.lang.Object) */ @Override public FluxTSeq unit(T unit) { return of(run.unit(Flux.just(unit))); } @Override public ReactiveSeq stream() { return run.map(i -> ReactiveSeq.fromPublisher(i)) .stream() .flatMap(e -> e); } @Override public Flux Flux() { return Flux.from(stream()); } @Override public Iterator iterator() { return stream().iterator(); } public FluxTSeq unitIterator(Iterator it) { return of(run.unitIterator(it) .map(i -> Flux.just(i))); } @Override public FluxT empty() { return of(run.empty()); } @Override public AnyM> nestedFoldables() { return run.map(i -> ReactiveSeq.fromPublisher(i)); } @Override public AnyM> nestedCollectables() { return run.map(i -> ReactiveSeq.fromPublisher(i)); } @Override public FluxTSeq unitAnyM(AnyM> traversable) { return of((AnyMSeq) traversable.map(t -> Flux.fromIterable(t))); } @Override public AnyMSeq> transformerStream() { return run.map(i -> ReactiveSeq.fromPublisher(i)); } public static FluxTSeq emptyStream() { return FluxT.fromIterable(ReactiveSeq.empty()); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#combine(java. * util.function.BiPredicate, java.util.function.BinaryOperator) */ @Override public FluxTSeq combine(BiPredicate predicate, BinaryOperator op) { return (FluxTSeq) FluxT.super.combine(predicate, op); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.FluxT#cycle(int) */ @Override public FluxTSeq cycle(int times) { return (FluxTSeq) FluxT.super.cycle(times); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#cycle(com.aol. * cyclops.Monoid, int) */ @Override public FluxTSeq cycle(Monoid m, int times) { return (FluxTSeq) FluxT.super.cycle(m, times); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#cycleWhile(java. * util.function.Predicate) */ @Override public FluxTSeq cycleWhile(Predicate predicate) { return (FluxTSeq) FluxT.super.cycleWhile(predicate); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#cycleUntil(java. * util.function.Predicate) */ @Override public FluxTSeq cycleUntil(Predicate predicate) { return (FluxTSeq) FluxT.super.cycleUntil(predicate); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#zip(java.lang. * Iterable, java.util.function.BiFunction) */ @Override public FluxTSeq zip(Iterable other, BiFunction zipper) { return (FluxTSeq) FluxT.super.zip(other, zipper); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#zipStream(java. * util.stream.Stream) */ @Override public FluxTSeq> zip(Stream other) { return (FluxTSeq) FluxT.super.zip(other); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#zip3(java.util. * stream.Stream, java.util.stream.Stream) */ @Override public FluxTSeq> zip3(Stream second, Stream third) { return (FluxTSeq) FluxT.super.zip3(second, third); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#zip4(java.util. * stream.Stream, java.util.stream.Stream, java.util.stream.Stream) */ @Override public FluxTSeq> zip4(Stream second, Stream third, Stream fourth) { return (FluxTSeq) FluxT.super.zip4(second, third, fourth); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#zipWithIndex() */ @Override public FluxTSeq> zipWithIndex() { return (FluxTSeq>) FluxT.super.zipWithIndex(); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#sliding(int) */ @Override public FluxTSeq> sliding(int windowSize) { return (FluxTSeq>) FluxT.super.sliding(windowSize); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#sliding(int, * int) */ @Override public FluxTSeq> sliding(int windowSize, int increment) { return (FluxTSeq>) FluxT.super.sliding(windowSize, increment); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#grouped(int, * java.util.function.Supplier) */ @Override public > FluxTSeq grouped(int size, Supplier supplier) { return (FluxTSeq) FluxT.super.grouped(size, supplier); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#groupedUntil( * java.util.function.Predicate) */ @Override public FluxTSeq> groupedUntil(Predicate predicate) { return (FluxTSeq>) FluxT.super.groupedUntil(predicate); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.FluxT# * groupedStatefullyWhile(java.util.function.BiPredicate) */ @Override public FluxTSeq> groupedStatefullyUntil(BiPredicate, ? super T> predicate) { return (FluxTSeq>) FluxT.super.groupedStatefullyUntil(predicate); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#groupedWhile( * java.util.function.Predicate) */ @Override public FluxTSeq> groupedWhile(Predicate predicate) { return (FluxTSeq>) FluxT.super.groupedWhile(predicate); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#groupedWhile( * java.util.function.Predicate, java.util.function.Supplier) */ @Override public > FluxTSeq groupedWhile(Predicate predicate, Supplier factory) { return (FluxTSeq) FluxT.super.groupedWhile(predicate, factory); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#groupedUntil( * java.util.function.Predicate, java.util.function.Supplier) */ @Override public > FluxTSeq groupedUntil(Predicate predicate, Supplier factory) { return (FluxTSeq) FluxT.super.groupedUntil(predicate, factory); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#grouped(int) */ @Override public FluxTSeq> grouped(int groupSize) { return (FluxTSeq>) FluxT.super.grouped(groupSize); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#grouped(java. * util.function.Function, java.util.stream.Collector) */ @Override public FluxTSeq> grouped(Function classifier, Collector downstream) { return (FluxTSeq) FluxT.super.grouped(classifier, downstream); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#grouped(java. * util.function.Function) */ @Override public FluxTSeq>> grouped(Function classifier) { return (FluxTSeq) FluxT.super.grouped(classifier); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.FluxT#distinct() */ @Override public FluxTSeq distinct() { return (FluxTSeq) FluxT.super.distinct(); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#scanLeft(com.aol * .cyclops.Monoid) */ @Override public FluxTSeq scanLeft(Monoid monoid) { return (FluxTSeq) FluxT.super.scanLeft(monoid); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#scanLeft(java. * lang.Object, java.util.function.BiFunction) */ @Override public FluxTSeq scanLeft(U seed, BiFunction function) { return (FluxTSeq) FluxT.super.scanLeft(seed, function); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#scanRight(com. * aol.cyclops.Monoid) */ @Override public FluxTSeq scanRight(Monoid monoid) { return (FluxTSeq) FluxT.super.scanRight(monoid); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#scanRight(java. * lang.Object, java.util.function.BiFunction) */ @Override public FluxTSeq scanRight(U identity, BiFunction combiner) { return (FluxTSeq) FluxT.super.scanRight(identity, combiner); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.FluxT#sorted() */ @Override public FluxTSeq sorted() { return (FluxTSeq) FluxT.super.sorted(); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#sorted(java.util * .Comparator) */ @Override public FluxTSeq sorted(Comparator c) { return (FluxTSeq) FluxT.super.sorted(c); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#takeWhile(java. * util.function.Predicate) */ @Override public FluxTSeq takeWhile(Predicate p) { return (FluxTSeq) FluxT.super.takeWhile(p); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#dropWhile(java. * util.function.Predicate) */ @Override public FluxTSeq dropWhile(Predicate p) { return (FluxTSeq) FluxT.super.dropWhile(p); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#takeUntil(java. * util.function.Predicate) */ @Override public FluxTSeq takeUntil(Predicate p) { return (FluxTSeq) FluxT.super.takeUntil(p); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#dropUntil(java. * util.function.Predicate) */ @Override public FluxTSeq dropUntil(Predicate p) { return (FluxTSeq) FluxT.super.dropUntil(p); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#dropRight(int) */ @Override public FluxTSeq dropRight(int num) { return (FluxTSeq) FluxT.super.dropRight(num); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#takeRight(int) */ @Override public FluxTSeq takeRight(int num) { return (FluxTSeq) FluxT.super.takeRight(num); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.FluxT#skip(long) */ @Override public FluxTSeq skip(long num) { return (FluxTSeq) FluxT.super.skip(num); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#skipWhile(java. * util.function.Predicate) */ @Override public FluxTSeq skipWhile(Predicate p) { return (FluxTSeq) FluxT.super.skipWhile(p); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#skipUntil(java. * util.function.Predicate) */ @Override public FluxTSeq skipUntil(Predicate p) { return (FluxTSeq) FluxT.super.skipUntil(p); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.FluxT#limit(long) */ @Override public FluxTSeq limit(long num) { return (FluxTSeq) FluxT.super.limit(num); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#limitWhile(java. * util.function.Predicate) */ @Override public FluxTSeq limitWhile(Predicate p) { return (FluxTSeq) FluxT.super.limitWhile(p); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#limitUntil(java. * util.function.Predicate) */ @Override public FluxTSeq limitUntil(Predicate p) { return (FluxTSeq) FluxT.super.limitUntil(p); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#intersperse(java * .lang.Object) */ @Override public FluxTSeq intersperse(T value) { return (FluxTSeq) FluxT.super.intersperse(value); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.FluxT#reverse() */ @Override public FluxTSeq reverse() { return (FluxTSeq) FluxT.super.reverse(); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.FluxT#shuffle() */ @Override public FluxTSeq shuffle() { return (FluxTSeq) FluxT.super.shuffle(); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#skipLast(int) */ @Override public FluxTSeq skipLast(int num) { return (FluxTSeq) FluxT.super.skipLast(num); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#limitLast(int) */ @Override public FluxTSeq limitLast(int num) { return (FluxTSeq) FluxT.super.limitLast(num); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#onEmpty(java. * lang.Object) */ @Override public FluxTSeq onEmpty(T value) { return (FluxTSeq) FluxT.super.onEmpty(value); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#onEmptyGet(java. * util.function.Supplier) */ @Override public FluxTSeq onEmptyGet(Supplier supplier) { return (FluxTSeq) FluxT.super.onEmptyGet(supplier); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#onEmptyThrow( * java.util.function.Supplier) */ @Override public FluxTSeq onEmptyThrow(Supplier supplier) { return (FluxTSeq) FluxT.super.onEmptyThrow(supplier); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#shuffle(java. * util.Random) */ @Override public FluxTSeq shuffle(Random random) { return (FluxTSeq) FluxT.super.shuffle(random); } /* * (non-Javadoc) * * @see com.aol.cyclops.control.monads.transformers.values.FluxT#slice(long, * long) */ @Override public FluxTSeq slice(long from, long to) { return (FluxTSeq) FluxT.super.slice(from, to); } /* * (non-Javadoc) * * @see * com.aol.cyclops.control.monads.transformers.values.FluxT#sorted(java.util * .function.Function) */ @Override public > FluxTSeq sorted(Function function) { return (FluxTSeq) FluxT.super.sorted(function); } @Override public int hashCode() { return run.hashCode(); } @Override public boolean equals(Object o) { if (o instanceof FluxTSeq) { return run.equals(((FluxTSeq) o).run); } return false; } }