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

cyclops.monads.FlowableAnyM Maven / Gradle / Ivy

There is a newer version: 10.4.0
Show newest version
package cyclops.monads;

import com.oath.cyclops.anym.AnyMSeq;
import com.oath.cyclops.rx2.adapter.FlowableReactiveSeqImpl;
import cyclops.companion.rx2.Flowables;
import cyclops.companion.rx2.Observables;
import cyclops.monads.transformers.StreamT;
import cyclops.reactive.FlowableReactiveSeq;
import cyclops.reactive.ReactiveSeq;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import org.reactivestreams.Publisher;

import java.util.function.Function;
import java.util.stream.Stream;

public interface FlowableAnyM {
    public static  ,T> XorM xorM(Flowable type){
        return XorM.right(anyM(type));
    }
    public static  Flowable raw(AnyM anyM){
        return flowable(anyM);
    }

    public static > AnyM> fromStream(AnyM> anyM){
        return anyM.map(s-> Flowables.flowableFrom(ReactiveSeq.fromStream(s)));
    }

    public static ,T> StreamT flowablify(StreamT nested){
        AnyM> anyM = nested.unwrap();
        AnyM> flowableM = anyM.map(s -> {
            if (s instanceof FlowableReactiveSeqImpl) {
                return (FlowableReactiveSeqImpl)s;
            }
            if(s instanceof ReactiveSeq){
                return ((ReactiveSeq)s).fold(sync->new FlowableReactiveSeqImpl(Flowable.fromIterable(sync)),
                    rs->new FlowableReactiveSeqImpl(Flowable.fromPublisher(rs)),
                    async ->new FlowableReactiveSeqImpl(Observables.fromStream(async).toFlowable(BackpressureStrategy.BUFFER)));
            }
            return new FlowableReactiveSeqImpl(Flowable.fromIterable(ReactiveSeq.fromStream(s)));
        });
        StreamT res = StreamT.of(flowableM);
        return res;
    }

    public static ,T,R> R nestedFlowable(StreamT nested, Function>,? extends R> mapper){
        return mapper.apply(nestedFlowable(nested));
    }
    public static ,T> AnyM> nestedFlowable(StreamT nested){
        AnyM> anyM = nested.unwrap();
        return anyM.map(s->{
            if(s instanceof FlowableReactiveSeqImpl){
                return ((FlowableReactiveSeqImpl)s).getFlowable();
            }
            if(s instanceof ReactiveSeq){
                ReactiveSeq r = (ReactiveSeq)s;
                return r.fold(sync->Flowable.fromIterable(sync), rs->Flowable.fromPublisher((Publisher)s),
                    async->Flowable.fromPublisher(async));
            }
            if(s instanceof Publisher){
                return Flowable.fromPublisher((Publisher)s);
            }
            return Flowable.fromIterable(ReactiveSeq.fromStream(s));
        });
    }

    public static ,T> StreamT liftM(AnyM> nested){
        AnyM> monad = nested.map(s -> new FlowableReactiveSeqImpl(s));
        return StreamT.of(monad);
    }




    /**
     * Construct an AnyM type from a Flowable. This allows the Flowable to be manipulated according to a standard interface
     * along with a vast array of other Java Monad implementations
     *
     * 
     * {@code
     *
     *    AnyMSeq flowable = Flowables.anyM(Flowable.just(1,2,3));
     *    AnyMSeq transformedFlowable = myGenericOperation(flowable);
     *
     *    public AnyMSeq myGenericOperation(AnyMSeq monad);
     * }
     * 
* * @param flowable To wrap inside an AnyM * @return AnyMSeq wrapping a flowable */ public static AnyMSeq anyM(Flowable flowable) { return AnyM.ofSeq(FlowableReactiveSeq.reactiveSeq(flowable), Rx2Witness.flowable.INSTANCE); } public static Flowable flowable(AnyM flowable) { FlowableReactiveSeqImpl flowableSeq = flowable.unwrap(); return flowableSeq.getFlowable(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy