Interface ThrowingFunction<T,R>

Type Parameters:
T - input parameter type
R - result type
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface ThrowingFunction<T,R>
An alternative to Java's Function, that declares a checked exception. This helps with handling checked exceptions in lambdas.
  • Method Details

    • apply

      R apply(T input) throws Exception
      Parameters:
      input - function input parameter of type T
      Returns:
      function result
      Throws:
      Exception - a checked exception might be thrown
    • unchecked

      static <T, R> Function<T,R> unchecked(ThrowingFunction<T,R> lambda)
      Wraps a lambda by handling checked Exception - converting it into RuntimeException
      Type Parameters:
      T - lambda input type
      R - lambda result type
      Parameters:
      lambda - lambda function
      Returns:
      function that does not declare checked exceptions
    • unchecked

      static <T, R> Function<T,R> unchecked(ThrowingFunction<T,R> lambda, Function<Exception,? extends RuntimeException> exceptionMapper)
      Wraps a lambda by handling checked Exception. A custom mapper should be provided for transforming the caught checked exception into a RuntimeException
      Type Parameters:
      T - lambda input type
      R - lambda result type
      Parameters:
      lambda - lambda function
      exceptionMapper - mapper, transforming checked exception into runtime
      Returns:
      function that does not declare checked exceptions