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 final class Try { private 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. */ @SuppressWarnings("unchecked") public static 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. */ @SuppressWarnings("unchecked") public static F> f(final Try1 t) { return a -> { try { return 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. */ @SuppressWarnings("unchecked") public static 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. */ @SuppressWarnings("unchecked") public static 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. */ @SuppressWarnings("unchecked") public static 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. */ @SuppressWarnings("unchecked") public static 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. */ @SuppressWarnings("unchecked") public static 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. */ @SuppressWarnings("unchecked") public static 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. */ @SuppressWarnings("unchecked") 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); } }