Class Functions


  • public class Functions
    extends java.lang.Object
    This class provides utility functions, and classes for working with the java.util.function package, or more generally, with Java 8 lambdas. More specifically, it attempts to address the fact that lambdas are supposed not to throw Exceptions, at least not checked Exceptions, aka instances of Exception. This enforces the use of constructs like
       Consumer<java.lang.reflect.Method> consumer = (m) -> {
           try {
               m.invoke(o, args);
           } catch (Throwable t) {
               throw Functions.rethrow(t);
           }
       };
     
    By replacing a Consumer<O> with a FailableConsumer<O,? extends Throwable>, this can be written like follows:
       Functions.accept((m) -> m.invoke(o,args));
     
    Obviously, the second version is much more concise and the spirit of Lambda expressions is met better than the second version.
    • Constructor Detail

      • Functions

        public Functions()
    • Method Detail

      • run

        public static <T extends java.lang.Throwable> void run​(Functions.FailableRunnable<T> pRunnable)
        Runs a runnable and rethrows any exception as a RuntimeException.
        Type Parameters:
        T - the type of checked exception the runnable may throw
        Parameters:
        pRunnable - The runnable to run
      • call

        public static <O,​T extends java.lang.Throwable> O call​(Functions.FailableCallable<O,​T> pCallable)
        Calls a callable and rethrows any exception as a RuntimeException.
        Type Parameters:
        O - the return type of the callable
        T - the type of checked exception the callable may throw
        Parameters:
        pCallable - the callable to call
        Returns:
        the value returned from the callable
      • accept

        public static <O,​T extends java.lang.Throwable> void accept​(Functions.FailableConsumer<O,​T> pConsumer,
                                                                          O pObject)
        Consumes a consumer and rethrows any exception as a RuntimeException.
        Type Parameters:
        O - the type the consumer accepts
        T - the type of checked exception the consumer may throw
        Parameters:
        pConsumer - the consumer to consume
        pObject - the object to consume by pConsumer
      • accept

        public static <O1,​O2,​T extends java.lang.Throwable> void accept​(Functions.FailableBiConsumer<O1,​O2,​T> pConsumer,
                                                                                    O1 pObject1,
                                                                                    O2 pObject2)
        Consumes a consumer and rethrows any exception as a RuntimeException.
        Type Parameters:
        O1 - the type of the first argument the consumer accepts
        O2 - the type of the second argument the consumer accepts
        T - the type of checked exception the consumer may throw
        Parameters:
        pConsumer - the consumer to consume
        pObject1 - the first object to consume by pConsumer
        pObject2 - the second object to consume by pConsumer
      • apply

        public static <I,​O,​T extends java.lang.Throwable> O apply​(Functions.FailableFunction<I,​O,​T> pFunction,
                                                                              I pInput)
        Applies a function and rethrows any exception as a RuntimeException.
        Type Parameters:
        I - the type of the argument the function accepts
        O - the return type of the function
        T - the type of checked exception the function may throw
        Parameters:
        pFunction - the function to apply
        pInput - the input to apply pFunction on
        Returns:
        the value returned from the function
      • apply

        public static <I1,​I2,​O,​T extends java.lang.Throwable> O apply​(Functions.FailableBiFunction<I1,​I2,​O,​T> pFunction,
                                                                                        I1 pInput1,
                                                                                        I2 pInput2)
        Applies a function and rethrows any exception as a RuntimeException.
        Type Parameters:
        I1 - the type of the first argument the function accepts
        I2 - the type of the second argument the function accepts
        O - the return type of the function
        T - the type of checked exception the function may throw
        Parameters:
        pFunction - the function to apply
        pInput1 - the first input to apply pFunction on
        pInput2 - the second input to apply pFunction on
        Returns:
        the value returned from the function
      • test

        public static <O,​T extends java.lang.Throwable> boolean test​(Functions.FailablePredicate<O,​T> pPredicate,
                                                                           O pObject)
        Tests a predicate and rethrows any exception as a RuntimeException.
        Type Parameters:
        O - the type of argument the predicate tests
        T - the type of checked exception the predicate may throw
        Parameters:
        pPredicate - the predicate to test
        pObject - the input to test by pPredicate
        Returns:
        the boolean value returned by the predicate
      • test

        public static <O1,​O2,​T extends java.lang.Throwable> boolean test​(Functions.FailableBiPredicate<O1,​O2,​T> pPredicate,
                                                                                     O1 pObject1,
                                                                                     O2 pObject2)
        Tests a predicate and rethrows any exception as a RuntimeException.
        Type Parameters:
        O1 - the type of the first argument the predicate tests
        O2 - the type of the second argument the predicate tests
        T - the type of checked exception the predicate may throw
        Parameters:
        pPredicate - the predicate to test
        pObject1 - the first input to test by pPredicate
        pObject2 - the second input to test by pPredicate
        Returns:
        the boolean value returned by the predicate
      • tryWithResources

        @SafeVarargs
        public static void tryWithResources​(Functions.FailableRunnable<? extends java.lang.Throwable> pAction,
                                            Functions.FailableConsumer<java.lang.Throwable,​? extends java.lang.Throwable> pErrorHandler,
                                            Functions.FailableRunnable<? extends java.lang.Throwable>... pResources)
        A simple try-with-resources implementation, that can be used, if your objects do not implement the AutoCloseable interface. The method executes the pAction. The method guarantees, that all the pResources are being executed, in the given order, afterwards, and regardless of success, or failure. If either the original action, or any of the resource action fails, then the first failure (aka Throwable is rethrown. Example use:
           final FileInputStream fis = new FileInputStream("my.file");
           Functions.tryWithResources(useInputStream(fis), null, () -> fis.close());
         
        Parameters:
        pAction - The action to execute. This object will always be invoked.
        pErrorHandler - An optional error handler, which will be invoked finally, if any error occurred. The error handler will receive the first error, aka Throwable.
        pResources - The resource actions to execute. All resource actions will be invoked, in the given order. A resource action is an instance of Functions.FailableRunnable, which will be executed.
        See Also:
        tryWithResources(FailableRunnable, FailableRunnable...)
      • tryWithResources

        @SafeVarargs
        public static void tryWithResources​(Functions.FailableRunnable<? extends java.lang.Throwable> pAction,
                                            Functions.FailableRunnable<? extends java.lang.Throwable>... pResources)
        A simple try-with-resources implementation, that can be used, if your objects do not implement the AutoCloseable interface. The method executes the pAction. The method guarantees, that all the pResources are being executed, in the given order, afterwards, and regardless of success, or failure. If either the original action, or any of the resource action fails, then the first failure (aka Throwable is rethrown. Example use:
           final FileInputStream fis = new FileInputStream("my.file");
           Functions.tryWithResources(useInputStream(fis), () -> fis.close());
         
        Parameters:
        pAction - The action to execute. This object will always be invoked.
        pResources - The resource actions to execute. All resource actions will be invoked, in the given order. A resource action is an instance of Functions.FailableRunnable, which will be executed.
        See Also:
        tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
      • rethrow

        public static java.lang.RuntimeException rethrow​(java.lang.Throwable pThrowable)
        Rethrow a Throwable as an unchecked exception.
        Parameters:
        pThrowable - The throwable to rethrow
        Returns:
        Never returns anything, this method never terminates normally