org.apache.commons.lang3

## Class Functions

- java.lang.Object
- - org.apache.commons.lang3.Functions

-     public class Functions
      extends 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.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `Functions.FailableBiConsumer<O1,O2,T extends Throwable>`
    `static interface` | `Functions.FailableBiFunction<I1,I2,O,T extends Throwable>`
    `static interface` | `Functions.FailableBiPredicate<O1,O2,T extends Throwable>`
    `static interface` | `Functions.FailableCallable<O,T extends Throwable>`
    `static interface` | `Functions.FailableConsumer<O,T extends Throwable>`
    `static interface` | `Functions.FailableFunction<I,O,T extends Throwable>`
    `static interface` | `Functions.FailablePredicate<O,T extends Throwable>`
    `static interface` | `Functions.FailableRunnable<T extends Throwable>`
    `static interface` | `Functions.FailableSupplier<O,T extends Throwable>`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Functions()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static <O1,O2,T extends Throwable> void` | `accept(Functions.FailableBiConsumer<O1,O2,T> consumer, O1 object1, O2 object2)` Consumes a consumer and rethrows any exception as a `RuntimeException`.
    `static <O,T extends Throwable> void` | `accept(Functions.FailableConsumer<O,T> consumer, O object)` Consumes a consumer and rethrows any exception as a `RuntimeException`.
    `static <I1,I2,O,T extends Throwable> O` | `apply(Functions.FailableBiFunction<I1,I2,O,T> function, I1 input1, I2 input2)` Applies a function and rethrows any exception as a `RuntimeException`.
    `static <I,O,T extends Throwable> O` | `apply(Functions.FailableFunction<I,O,T> function, I input)` Applies a function and rethrows any exception as a `RuntimeException`.
    `static <I1,I2> BiConsumer<I1,I2>` | `asBiConsumer(Functions.FailableBiConsumer<I1,I2,?> consumer)` Converts the given `Functions.FailableBiConsumer` into a standard `BiConsumer`.
    `static <I1,I2,O> BiFunction<I1,I2,O>` | `asBiFunction(Functions.FailableBiFunction<I1,I2,O,?> function)` Converts the given `Functions.FailableBiFunction` into a standard `BiFunction`.
    `static <I1,I2> BiPredicate<I1,I2>` | `asBiPredicate(Functions.FailableBiPredicate<I1,I2,?> predicate)` Converts the given `Functions.FailableBiPredicate` into a standard `BiPredicate`.
    `static <O> Callable<O>` | `asCallable(Functions.FailableCallable<O,?> callable)` Converts the given `Functions.FailableCallable` into a standard `Callable`.
    `static <I> Consumer<I>` | `asConsumer(Functions.FailableConsumer<I,?> consumer)` Converts the given `Functions.FailableConsumer` into a standard `Consumer`.
    `static <I,O> Function<I,O>` | `asFunction(Functions.FailableFunction<I,O,?> function)` Converts the given `Functions.FailableFunction` into a standard `Function`.
    `static <I> Predicate<I>` | `asPredicate(Functions.FailablePredicate<I,?> predicate)` Converts the given `Functions.FailablePredicate` into a standard `Predicate`.
    `static Runnable` | `asRunnable(Functions.FailableRunnable<?> runnable)` Converts the given `Functions.FailableRunnable` into a standard `Runnable`.
    `static <O> Supplier<O>` | `asSupplier(Functions.FailableSupplier<O,?> supplier)` Converts the given `Functions.FailableSupplier` into a standard `Supplier`.
    `static <O,T extends Throwable> O` | `call(Functions.FailableCallable<O,T> callable)` Calls a callable and rethrows any exception as a `RuntimeException`.
    `static <O,T extends Throwable> O` | `get(Functions.FailableSupplier<O,T> supplier)` Invokes the supplier, and returns the result.
    `static RuntimeException` | `rethrow(Throwable throwable)` Rethrows a `Throwable` as an unchecked exception.
    `static <T extends Throwable> void` | `run(Functions.FailableRunnable<T> runnable)` Runs a runnable and rethrows any exception as a `RuntimeException`.
    `static <O> Streams.FailableStream<O>` | `stream(Collection<O> collection)` Converts the given collection into a `Streams.FailableStream`.
    `static <O> Streams.FailableStream<O>` | `stream(Stream<O> stream)` Converts the given stream into a `Streams.FailableStream`.
    `static <O1,O2,T extends Throwable> boolean` | `test(Functions.FailableBiPredicate<O1,O2,T> predicate, O1 object1, O2 object2)` Tests a predicate and rethrows any exception as a `RuntimeException`.
    `static <O,T extends Throwable> boolean` | `test(Functions.FailablePredicate<O,T> predicate, O object)` Tests a predicate and rethrows any exception as a `RuntimeException`.
    `static void` | `tryWithResources(Functions.FailableRunnable<? extends Throwable> action, Functions.FailableConsumer<Throwable,? extends Throwable> errorHandler, Functions.FailableRunnable<? extends Throwable>... resources)` A simple try-with-resources implementation, that can be used, if your objects do not implement the `AutoCloseable` interface.
    `static void` | `tryWithResources(Functions.FailableRunnable<? extends Throwable> action, Functions.FailableRunnable<? extends Throwable>... resources)` A simple try-with-resources implementation, that can be used, if your objects do not implement the `AutoCloseable` interface.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Functions

          public Functions()

  - ### Method Detail

    - #### asRunnable

          public static Runnable asRunnable(Functions.FailableRunnable<?> runnable)

      Converts the given `Functions.FailableRunnable` into a standard `Runnable`.
      Parameters:
        `runnable` - a `FailableRunnable`
      Returns:
        a standard `Runnable`

    - #### asConsumer

          public static <I> Consumer<I> asConsumer(Functions.FailableConsumer<I,?> consumer)

      Converts the given `Functions.FailableConsumer` into a standard `Consumer`.
      Type Parameters:
        `I` - the type used by the consumers
      Parameters:
        `consumer` - a `FailableConsumer`
      Returns:
        a standard `Consumer`

    - #### asCallable

          public static <O> Callable<O> asCallable(Functions.FailableCallable<O,?> callable)

      Converts the given `Functions.FailableCallable` into a standard `Callable`.
      Type Parameters:
        `O` - the type used by the callables
      Parameters:
        `callable` - a `FailableCallable`
      Returns:
        a standard `Callable`

    - #### asBiConsumer

          public static <I1,I2> BiConsumer<I1,I2> asBiConsumer(Functions.FailableBiConsumer<I1,I2,?> consumer)

      Converts the given `Functions.FailableBiConsumer` into a standard `BiConsumer`.
      Type Parameters:
        `I1` - the type of the first argument of the consumers
        `I2` - the type of the second argument of the consumers
      Parameters:
        `consumer` - a failable `BiConsumer`
      Returns:
        a standard `BiConsumer`

    - #### asFunction

          public static <I,O> Function<I,O> asFunction(Functions.FailableFunction<I,O,?> function)

      Converts the given `Functions.FailableFunction` into a standard `Function`.
      Type Parameters:
        `I` - the type of the input of the functions
        `O` - the type of the output of the functions
      Parameters:
        `function` - a {code FailableFunction}
      Returns:
        a standard `Function`

    - #### asBiFunction

          public static <I1,I2,O> BiFunction<I1,I2,O> asBiFunction(Functions.FailableBiFunction<I1,I2,O,?> function)

      Converts the given `Functions.FailableBiFunction` into a standard `BiFunction`.
      Type Parameters:
        `I1` - the type of the first argument of the input of the functions
        `I2` - the type of the second argument of the input of the functions
        `O` - the type of the output of the functions
      Parameters:
        `function` - a `FailableBiFunction`
      Returns:
        a standard `BiFunction`

    - #### asPredicate

          public static <I> Predicate<I> asPredicate(Functions.FailablePredicate<I,?> predicate)

      Converts the given `Functions.FailablePredicate` into a standard `Predicate`.
      Type Parameters:
        `I` - the type used by the predicates
      Parameters:
        `predicate` - a `FailablePredicate`
      Returns:
        a standard `Predicate`

    - #### asBiPredicate

          public static <I1,I2> BiPredicate<I1,I2> asBiPredicate(Functions.FailableBiPredicate<I1,I2,?> predicate)

      Converts the given `Functions.FailableBiPredicate` into a standard `BiPredicate`.
      Type Parameters:
        `I1` - the type of the first argument used by the predicates
        `I2` - the type of the second argument used by the predicates
      Parameters:
        `predicate` - a `FailableBiPredicate`
      Returns:
        a standard `BiPredicate`

    - #### asSupplier

          public static <O> Supplier<O> asSupplier(Functions.FailableSupplier<O,?> supplier)

      Converts the given `Functions.FailableSupplier` into a standard `Supplier`.
      Type Parameters:
        `O` - the type supplied by the suppliers
      Parameters:
        `supplier` - a `FailableSupplier`
      Returns:
        a standard `Supplier`

    - #### run

          public static <T extends Throwable> void run(Functions.FailableRunnable<T> runnable)

      Runs a runnable and rethrows any exception as a `RuntimeException`.
      Type Parameters:
        `T` - the type of checked exception the runnable may throw
      Parameters:
        `runnable` - The runnable to run

    - #### call

          public static <O,T extends Throwable> O call(Functions.FailableCallable<O,T> callable)

      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:
        `callable` - the callable to call
      Returns:
        the value returned from the callable

    - #### accept

          public static <O,T extends Throwable> void accept(Functions.FailableConsumer<O,T> consumer,
                                                            O object)

      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:
        `consumer` - the consumer to consume
        `object` - the object to consume by `consumer`

    - #### accept

          public static <O1,O2,T extends Throwable> void accept(Functions.FailableBiConsumer<O1,O2,T> consumer,
                                                                O1 object1,
                                                                O2 object2)

      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:
        `consumer` - the consumer to consume
        `object1` - the first object to consume by `consumer`
        `object2` - the second object to consume by `consumer`

    - #### apply

          public static <I,O,T extends Throwable> O apply(Functions.FailableFunction<I,O,T> function,
                                                          I input)

      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:
        `function` - the function to apply
        `input` - the input to apply `function` on
      Returns:
        the value returned from the function

    - #### apply

          public static <I1,I2,O,T extends Throwable> O apply(Functions.FailableBiFunction<I1,I2,O,T> function,
                                                              I1 input1,
                                                              I2 input2)

      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:
        `function` - the function to apply
        `input1` - the first input to apply `function` on
        `input2` - the second input to apply `function` on
      Returns:
        the value returned from the function

    - #### test

          public static <O,T extends Throwable> boolean test(Functions.FailablePredicate<O,T> predicate,
                                                             O object)

      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:
        `predicate` - the predicate to test
        `object` - the input to test by `predicate`
      Returns:
        the boolean value returned by the predicate

    - #### test

          public static <O1,O2,T extends Throwable> boolean test(Functions.FailableBiPredicate<O1,O2,T> predicate,
                                                                 O1 object1,
                                                                 O2 object2)

      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:
        `predicate` - the predicate to test
        `object1` - the first input to test by `predicate`
        `object2` - the second input to test by `predicate`
      Returns:
        the boolean value returned by the predicate

    - #### get

          public static <O,T extends Throwable> O get(Functions.FailableSupplier<O,T> supplier)

      Invokes the supplier, and returns the result.
      Type Parameters:
        `O` - The suppliers output type.
        `T` - The type of checked exception, which the supplier can throw.
      Parameters:
        `supplier` - The supplier to invoke.
      Returns:
        The object, which has been created by the supplier

    - #### stream

          public static <O> Streams.FailableStream<O> stream(Stream<O> stream)

      Converts the given stream into a `Streams.FailableStream`. The `Streams.FailableStream` consists of the same elements, than the input stream. However, failable lambdas, like `Functions.FailablePredicate`, `Functions.FailableFunction`, and `Functions.FailableConsumer` may be applied, rather than `Predicate`, `Function`, `Consumer`, etc.
      Type Parameters:
        `O` - The streams element type.
      Parameters:
        `stream` - The stream, which is being converted into a `Streams.FailableStream`.
      Returns:
        The created `Streams.FailableStream`.

    - #### stream

          public static <O> Streams.FailableStream<O> stream(Collection<O> collection)

      Converts the given collection into a `Streams.FailableStream`. The `Streams.FailableStream` consists of the collections elements. Shortcut for

             Functions.stream(collection.stream());

      Type Parameters:
        `O` - The collections element type. (In turn, the result streams element type.)
      Parameters:
        `collection` - The collection, which is being converted into a `Streams.FailableStream`.
      Returns:
        The created `Streams.FailableStream`.

    - #### tryWithResources

          @SafeVarargs
          public static void tryWithResources(Functions.FailableRunnable<? extends Throwable> action,
                                                            Functions.FailableConsumer<Throwable,? extends Throwable> errorHandler,
                                                            Functions.FailableRunnable<? extends Throwable>... resources)

      A simple try-with-resources implementation, that can be used, if your objects do not implement the `AutoCloseable` interface. The method executes the `action`. The method guarantees, that all the `resources` 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:
        `action` - The action to execute. This object will always be invoked.
        `errorHandler` - An optional error handler, which will be invoked finally, if any error occurred. The error handler will receive the first error, aka `Throwable`.
        `resources` - 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 Throwable> action,
                                                            Functions.FailableRunnable<? extends Throwable>... resources)

      A simple try-with-resources implementation, that can be used, if your objects do not implement the `AutoCloseable` interface. The method executes the `action`. The method guarantees, that all the `resources` 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:
        `action` - The action to execute. This object will always be invoked.
        `resources` - 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 RuntimeException rethrow(Throwable throwable)

      Rethrows a `Throwable` as an unchecked exception. If the argument is already unchecked, namely a `RuntimeException` or `Error` then the argument will be rethrown without modification. If the exception is `IOException` then it will be wrapped into a `UncheckedIOException`. In every other cases the exception will be wrapped into a `UndeclaredThrowableException`

      Note that there is a declared return type for this method, even though it never returns. The reason for that is to support the usual pattern:

                throw rethrow(myUncheckedException);

      instead of just calling the method. This pattern may help the Java compiler to recognize that at that point an exception will be thrown and the code flow analysis will not demand otherwise mandatory commands that could follow the method call, like a `return` statement from a value returning method.

      Parameters:
        `throwable` - The throwable to rethrow ossibly wrapped into an unchecked exception
      Returns:
        Never returns anything, this method never terminates normally.

Copyright © 2001–2020 The Apache Software Foundation. All rights reserved.