java.lang.Object
com.github.nullterminated.trylambda.Try

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

    • trys

      public static <T extends AutoCloseable, R> CheckedSupplier<R> trys(CheckedSupplier<T> supplier, Function<Exception,CheckedSupplier<R>> errorHandler, 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 AutoCloseable, R> CheckedSupplier<R> trys(CheckedSupplier<T> supplier, 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<Exception,R> either(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<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