Class FunctionalCheckedExceptionWrappers


  • public final class FunctionalCheckedExceptionWrappers
    extends java.lang.Object
    A collections of utility methods for simplifying the syntax of lambda expressions with APIs that don't accept checked exceptions (such as Stream): they provide wrapped functions that have no checked exception in the signature and whose implementation delegates to the original function wrapping an eventual checked exception into a RuntimeException. For instance, given the following method that could not be used as a Stream.filter(Predicate) argument:
       private boolean matchEven (final int number)
               throws Exception
         {
           if (number == 13)
             {
               throw new Exception("13!");
             }
    
           return number % 2 == 0;
         }
     
    working code can be written as:
       try
         {
           List<Integer> numbers = IntStream.rangeClosed(1, 20)
                                            .mapToObj(Integer::valueOf)
                                            .filter(_p(this::matchEven)) // note the wrapper here
                                            .collect(Collectors.toList());
           ...
         }
       catch (RuntimeException e)
         {
           ...
         }
     
    Any checked exception is wrapped by a RuntimeException, but IOException is wrapped by a UncheckedIOException.
    Since:
    3.2-ALPHA-1
    Author:
    Fabrizio Giudici
    Status: draft API
    • Constructor Detail

      • FunctionalCheckedExceptionWrappers

        public FunctionalCheckedExceptionWrappers()
    • Method Detail

      • _f

        @Nonnull
        public static <T,​R> java.util.function.Function<T,​R> _f​(@Nonnull
                                                                            FunctionalCheckedExceptionWrappers.FunctionWithException<T,​R> function)
        A wrapper for a Function that catches exceptions and wraps them into RuntimeExceptions.
        Type Parameters:
        T - the type of the function argument
        R - the type of the function return value
        Parameters:
        function - the Function to wrap.
        Returns:
        the wrapped Function
      • _c

        @Nonnull
        public static <T> java.util.function.Consumer<T> _c​(@Nonnull
                                                            FunctionalCheckedExceptionWrappers.ConsumerWithException<T> consumer)
        A wrapper for a Consumer that catches exceptions and wraps them into RuntimeExceptions.
        Type Parameters:
        T - the type of the Consumer argument
        Parameters:
        consumer - the Consumer to wrap.
        Returns:
        the wrapped Consumer
      • _s

        @Nonnull
        public static <T> java.util.function.Supplier<T> _s​(@Nonnull
                                                            FunctionalCheckedExceptionWrappers.SupplierWithException<T> supplier)
        A wrapper for a Supplier that catches exceptions and wraps them into RuntimeExceptions.
        Type Parameters:
        T - the type of the Supplier argument
        Parameters:
        supplier - the Supplier to wrap.
        Returns:
        åthe wrapped Supplier
      • _p

        @Nonnull
        public static <T> java.util.function.Predicate<T> _p​(@Nonnull
                                                             FunctionalCheckedExceptionWrappers.PredicateWithException<T> predicate)
        A wrapper for a Predicate that catches exceptions and wraps them into RuntimeExceptions.
        Type Parameters:
        T - the type of the Predicate argument
        Parameters:
        predicate - the Predicate to wrap.
        Returns:
        åthe wrapped Predicate