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

com.aol.cyclops.vavr.hkt.TryKind Maven / Gradle / Ivy

The newest version!
package com.aol.cyclops.vavr.hkt;


import com.aol.cyclops2.hkt.Higher;
import cyclops.companion.vavr.Futures;
import cyclops.companion.vavr.Trys;
import cyclops.conversion.vavr.FromCyclopsReact;
import cyclops.conversion.vavr.ToCyclopsReact;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.future;
import cyclops.monads.VavrWitness.tryType;
import cyclops.monads.WitnessType;
import cyclops.monads.transformers.FutureT;
import cyclops.monads.transformers.XorT;
import cyclops.typeclasses.Active;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.typeclasses.Nested;
import io.vavr.collection.List;
import io.vavr.concurrent.Future;
import io.vavr.concurrent.Promise;
import io.vavr.control.Option;
import io.vavr.control.Try;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Simulates Higher Kinded Types for Vavr Future's
 * 
 * FutureKind is a Future and a Higher Kinded Type (future,T)
 * 
 * @author johnmcclean
 *
 * @param  Data type stored within the Future
 */

public interface TryKind extends Higher, Try {

    public static  Higher widenK(final Try completableList) {

        return new TryKind.Box<>(
                completableList);
    }
    default Active allTypeclasses(){
        return Active.of(this, Trys.Instances.definitions());
    }
    default  Nested mapM(Function> fn, InstanceDefinitions defs){
        return Trys.mapM(this,fn,defs);
    }
    default > XorT liftM(W witness) {
        return XorT.of(witness.adapter().unit(ToCyclopsReact.toTry(this).asXor()));
    }
    default  TryKind fold(Function,? extends Try> op){
        return widen(op.apply(this));
    }
    
    public static  TryKind failed(Throwable exception){
        return widen(Try.failure(exception));
    }
    /**
     * Construct a HKT encoded completed Future
     * 
     * @param value To encode inside a HKT encoded Future
     * @return Completed HKT encoded Future
     */
    public static  TryKind successful(T value){
        return widen(Try.success(value));
    }

    /**
     * Convert a Future to a simulated HigherKindedType that captures Future nature
     * and Future element data type separately. Recover via @see FutureKind#narrow
     * 
     * If the supplied Future implements FutureKind it is returned already, otherwise it
     * is wrapped into a Future implementation that does implement FutureKind
     * 
     * @param newTry Future to widen to a FutureKind
     * @return FutureKind encoding HKT info about Futures
     */
    public static  TryKind widen(final Try newTry) {
        if (newTry instanceof TryKind)
            return (TryKind) newTry;
        return new Box<>(
                         newTry);
    }
    public static  TryKind widen(final cyclops.control.Try newTry) {

        return new Box<>(FromCyclopsReact.toTry(
                newTry));
    }

    /**
     * Convert the raw Higher Kinded Type for FutureKind types into the FutureKind type definition class
     * 
     * @param future HKT encoded list into a FutureKind
     * @return FutureKind
     */
    public static  TryKind narrowK(final Higher future) {
       return (TryKind)future;
    }

    /**
     * Convert the HigherKindedType definition for a Future into
     * 
     * @param newTry Type Constructor to convert back into narrowed type
     * @return Future from Higher Kinded Type
     */
    public static  Try narrow(final Higher newTry) {
        if (newTry instanceof Try) {
            return (Try)newTry;
           
        }
        // this code should be unreachable due to HKT type checker
        final Box type = (Box) newTry;
        final Try stage = type.narrow();
        return stage;

    }

    @AllArgsConstructor(access = AccessLevel.PRIVATE)
    static final class Box implements TryKind {

        private final Try boxed;

        public Try narrow() {
            return boxed;
        }
        @Override
        public T get() {
            return boxed.get();
        }

        @Override
        public Throwable getCause() {
            return boxed.getCause();
        }

        @Override
        public boolean isEmpty() {
            return boxed.isEmpty();
        }

        @Override
        public String stringPrefix() {
            return boxed.stringPrefix();
        }

        @Override
        public boolean isFailure() {
            return boxed.isFailure();
        }

        @Override
        public boolean isSuccess() {
            return boxed.isSuccess();
        }
    }

    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy