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

fj.Try Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

There is a newer version: 5.0
Show newest version
package fj;

import fj.data.IO;
import fj.data.IOFunctions;
import fj.data.Validation;
import fj.function.*;

import java.io.IOException;

import static fj.data.Validation.fail;
import static fj.data.Validation.success;

/**
 * Created by mperry on 24/07/2014.
 */
public class Try {

    /**
     * Promotes the Try0 to a Validation that returns an Exception on the failure side and its result on the success side.
     *
     * @param t A Try0 to promote
     * @return A Validation with an Exception on the failure side and its result on the success side.
     */
    static public  P1> f(final Try0 t) {
        return P.lazy(() -> {
            try {
                return success(t.f());
            } catch (Exception e) {
                return fail((E) e);
            }
        });
    }

    /**
     * Promotes the Try1 to a Validation that returns an Exception on the failure side and its result on the success side.
     *
     * @param t A Try1 to promote
     * @return A Validation with an Exception on the failure side and its result on the success side.
     */
    static public  F> f(final Try1 t) {
        return a -> {
            try {
                return Validation.success(t.f(a));
            } catch (Exception e) {
                return fail((E) e);
            }
        };
    }

    /**
     * Promotes the Try2 to a Validation that returns an Exception on the failure side and its result on the success side.
     *
     * @param t A Try2 to promote
     * @return A Validation with an Exception on the failure side and its result on the success side.
     */
    static public  F2> f(final Try2 t) {
        return (a, b) -> {
            try {
                return success(t.f(a, b));
            } catch (Exception e) {
                return fail((E) e);
            }
        };
    }

    /**
     * Promotes the Try3 to a Validation that returns an Exception on the failure side and its result on the success side.
     *
     * @param t A Try3 to promote
     * @return A Validation with an Exception on the failure side and its result on the success side.
     */
    static public  F3> f(final Try3 t) {
        return (a, b, c) -> {
            try {
                return success(t.f(a, b, c));
            } catch (Exception e) {
                return fail((E) e);
            }
        };
    }

    /**
     * Promotes the Try4 to a Validation that returns an Exception on the failure side and its result on the success side.
     *
     * @param t A Try4 to promote
     * @return A Validation with an Exception on the failure side and its result on the success side.
     */
    static public  F4> f(final Try4 t) {
        return (a, b, c, d) -> {
            try {
                return success(t.f(a, b, c, d));
            } catch (Exception ex) {
                return fail((Z) ex);
            }
        };
    }

    /**
     * Promotes the Try5 to a Validation that returns an Exception on the failure side and its result on the success side.
     *
     * @param t A Try5 to promote
     * @return A Validation with an Exception on the failure side and its result on the success side.
     */
    static public  F5> f(final Try5 t) {
        return (a, b, c, d, e) -> {
            try {
                return success(t.f(a, b, c, d, e));
            } catch (Exception ex) {
                return fail((Z) ex);
            }
        };
    }

    /**
     * Promotes the Try6 to a Validation that returns an Exception on the failure side and its result on the success side.
     *
     * @param t A Try6 to promote
     * @return A Validation with an Exception on the failure side and its result on the success side.
     */
    static public  F6> f(final Try6 t) {
        return (a, b, c, d, e, f) -> {
            try {
                return success(t.f(a, b, c, d, e, f));
            } catch (Exception ex) {
                return fail((Z) ex);
            }
        };
    }

    /**
     * Promotes the Try7 to a Validation that returns an Exception on the failure side and its result on the success side.
     *
     * @param t A Try7 to promote
     * @return A Validation with an Exception on the failure side and its result on the success side.
     */
    static public  F7> f(final Try7 t) {
        return (a, b, c, d, e, f, g) -> {
            try {
                return success(t.f(a, b, c, d, e, f, g));
            } catch (Exception ex) {
                return fail((Z) ex);
            }
        };
    }

    /**
     * Promotes the Try8 to a Validation that returns an Exception on the failure side and its result on the success side.
     *
     * @param t A Try8 to promote
     * @return A Validation with an Exception on the failure side and its result on the success side.
     */
    public static  F8> f(final Try8 t) {
        return (a, b, c, d, e, f, g, h) -> {
            try {
                return success(t.f(a, b, c, d, e, f, g, h));
            } catch (Exception ex) {
                return fail((Z) ex);
            }
        };
    }

    public static  IO io(Try0 t) {
        return IOFunctions.fromTry(t);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy