Class Try


  • public final class Try
    extends java.lang.Object
    A utility class for handling try blocks using lambda expressions.
    Author:
    Ramsey Gurley
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <R> Either<java.lang.Exception,​R> either​(CheckedSupplier<R> supplier)
      This method exists to allow usage of either with checked suppliers without casting or needing to declare the checked supplier explicitly.
      static <R> Either<java.lang.Exception,​R> either​(java.util.function.Supplier<R> supplier)
      Wraps the call to get in a try block.
      static <T extends java.lang.AutoCloseable,​R>
      CheckedSupplier<R>
      trys​(CheckedSupplier<T> supplier, java.util.function.Function<java.lang.Exception,​CheckedSupplier<R>> errorHandler, java.util.function.Function<T,​CheckedSupplier<R>> function)
      A method to use auto closing try with resources with lambda parameters.
      static <T extends java.lang.AutoCloseable,​R>
      CheckedSupplier<R>
      trys​(CheckedSupplier<T> supplier, java.util.function.Function<T,​CheckedSupplier<R>> function)
      Calls the three argument form of this method with an error handler which simply wraps any exception in a WrappedException.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • trys

        public static <T extends java.lang.AutoCloseable,​R> CheckedSupplier<R> trys​(CheckedSupplier<T> supplier,
                                                                                          java.util.function.Function<java.lang.Exception,​CheckedSupplier<R>> errorHandler,
                                                                                          java.util.function.Function<T,​CheckedSupplier<R>> function)
        A method to use auto closing try with resources with lambda parameters. The supplier argument supplies the AutoCloseable resource of the try block, which is passed to the function argument in the body of the try block. The result of the function argument is rewrapped in a new supplier so that calls to this method may be chained. If a WrappedException is thrown by the supplier or function arguments, it is rethrown. All other exceptions are passed to the errorHandler argument for handling.
        Type Parameters:
        T - the supplied AutoCloseable type
        R - the supplied result type
        Parameters:
        supplier - the AutoCloseable supplier
        errorHandler - a function to handle exceptions thrown by the supplier or function arguments
        function - a function that accepts the supplied AutoCloseable type and returns a supplier of the result type
        Returns:
        a supplier of the result type
      • trys

        public static <T extends java.lang.AutoCloseable,​R> CheckedSupplier<R> trys​(CheckedSupplier<T> supplier,
                                                                                          java.util.function.Function<T,​CheckedSupplier<R>> function)
        Calls the three argument form of this method with an error handler which simply wraps any exception in a WrappedException.
        Type Parameters:
        T - the supplied AutoCloseable type
        R - the supplied result type
        Parameters:
        supplier - the AutoCloseable supplier
        function - a function that accepts the supplied AutoCloseable type and returns a supplier of the result type
        Returns:
        a supplier of the result type
      • either

        public static <R> Either<java.lang.Exception,​R> either​(java.util.function.Supplier<R> supplier)
        Wraps the call to get in a try block. Either an exception thrown by the supplier or the supplied value is returned. If a WrappedException is thrown, its cause is returned.
        Type Parameters:
        R - the supplied value type
        Parameters:
        supplier - the value supplier
        Returns:
        either an exception thrown by the supplier or the supplied value
      • either

        public static <R> Either<java.lang.Exception,​R> either​(CheckedSupplier<R> supplier)
        This method exists to allow usage of either with checked suppliers without casting or needing to declare the checked supplier explicitly.
        Type Parameters:
        R - the supplied value type
        Parameters:
        supplier - the supplier function
        Returns:
        either an exception or the supplied value