Package ch.powerunit.extensions.exceptions

Functional interface similar to the once from java.util.function, but with throws Exception.

The various functional interface provided by this package exposes several dedicated methods :

  • uncheck() which converts the functional interface to the once from java.util.function, having the checked Exception wrapped in a RuntimeException. By default this will be an instance of WrappedException. An additional method unchecked(...) is provided to statically create unchecked version of the functional interface.
  • lift() and lifted(...) which converts the functional interface to a version that either return an Optional.empty() or a default value in case of error.
  • ignore() and ignored(...) which converts the functional interface to a version that return a default value in case of error.
Additionally, it is also possible to override the generated exception (for the uncheck... methods) by specifying a Function to compute the target RuntimeException.

Examples

It is possible to use the method unchecked to directly create a functional interface with only runtime exception :
 FunctionWithException<String, String, IOException> fonctionThrowingException = ...;

 Function<String, String> functionThrowingRuntimeException =
   FunctionWithException.unchecked(fonctionThrowingException);
 
When it is required to thrown a specific RuntimeException, it is also possible to specify it :
 FunctionWithException<String, String, IOException> fonctionThrowingException = ...;

 Function<String, String> functionThrowingRuntimeException =
   FunctionWithException.unchecked(
                                   fonctionThrowingException,
                                   IllegalArgumentException::new
                                  );
 
When the exception should not be thrown in case of error, it is possible to create a Function with Optional result :
 FunctionWithException<String, String, IOException> fonctionThrowingException = ...;

 Function<String, Optional<String>> functionWithOptionalResult =
   FunctionWithException.lifted(fonctionThrowingException);
 

Exception Mapping

By default, the exception are transformed into a RuntimeException using the WrappedException. This behaviour can be changed locally (second argument of the unchecked methods) or globally (by registering default ExceptionMapper. To do the global configuration, it is required first to create the ExceptionMapper:
 public class MyExceptionMapper implements ExceptionMapper {
   public RuntimeException apply(Exception e) {
     //Add the code
   }
   public Class>? extends Exception< targetException() {
     return //Add the code;
   }
   // Optional, to define the order between the ExceptionMapper
   public int order() {
     return 100;
   }
 }
 
Then, the ExceptionMapper must be registered :
 module XXX {
   requires powerunit.exceptions;
   provides ch.powerunit.extensions.exceptions.ExceptionMapper
     with ....MyExceptionMapper;
 }
 
See Also:
java.util.function