Class FunctionalUtil

java.lang.Object
org.frankframework.functional.FunctionalUtil

public class FunctionalUtil extends Object
Static utility class with helper functions for functional programming and using lambdas.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T,R> Function<T,R>
    Helper function to cast parameter as Function when the compiler cannot work it out by itself.
    static <T> org.apache.logging.log4j.util.Supplier<T>
    logMethod(Supplier<T> method)
    Helper function to create a Log4J Supplier to be able to use a method as supplier in a row of mixed arguments.
    static <T> org.apache.logging.log4j.util.Supplier<T>
    logValue(T value)
    Helper function to create a Log4J Supplier to supply a single constant argument value.
    static <T> Supplier<T>
    Helper function to cast parameter as a Supplier when the compiler cannot work it out by itself.
    static <T> Supplier<T>
    supply(T value)
    Helper function to create a Supplier to supply the single constant argument value.
    static <T, E extends Exception>
    T
    Allow a Lambda function to throw an exception in places where a checked exception is not expected, such as lambda functions in Streams.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • supply

      public static <T> Supplier<T> supply(T value)
      Helper function to create a Supplier to supply the single constant argument value.

      This function is useful to disambiguate method-overloads when an array of mixed arguments should be passed all as type Supplier to a function that takes a number of Object parameters, such as Log4J log methods (in particular the methods where an exception is passed as last argument). For example:

              log.error("{} Error with message id [{}]", supplier(this::getLogPrefix), supply(messageId), e);
          

      Type Parameters:
      T - Type of the value to be supplied.
      Parameters:
      value - Value to be supplied. NB: This should be a constant, otherwise its value is instantly computed instead of being delayed on-demand!
      Returns:
      Supplier that will return the value parameter.
    • logValue

      public static <T> org.apache.logging.log4j.util.Supplier<T> logValue(T value)
      Helper function to create a Log4J Supplier to supply a single constant argument value.

      This function is useful when you need to pass a variable as constant which is not effectively final, or to disambiguate method-overloads when an array of mixed arguments should be passed all as type Supplier to a logger method (in particular the methods where an exception is passed as last argument). For example:

              String messageId = null;
              if (true) messageId = "msg id";
              log.error("{} Error with message id [{}]", supplier(this::getLogPrefix), logValue(messageId), e);
          

      Type Parameters:
      T - Type of the value to be supplied.
      Parameters:
      value - Value to be supplied. NB: This should be a constant, otherwise its value is instantly computed instead of being delayed on-demand!
      Returns:
      Supplier that will return the value parameter.
    • logMethod

      public static <T> org.apache.logging.log4j.util.Supplier<T> logMethod(Supplier<T> method)
      Helper function to create a Log4J Supplier to be able to use a method as supplier in a row of mixed arguments.

      This function is useful when you need to pass a variable as constant which is not effectively final, or to disambiguate method-overloads when an array of mixed arguments should be passed all as type Supplier to a logger method (in particular the methods where an exception is passed as last argument). For example:

              log.error("Error with message id [{}]", logMethod(this::getLogPrefix), logValue(v), e);
          

      Type Parameters:
      T - Type of the value of the method argument.
      Parameters:
      method - Single-argument Method to be invoked.
      Returns:
      Supplier that will return the method parameter.
    • supplier

      public static <T> Supplier<T> supplier(Supplier<T> s)
      Helper function to cast parameter as a Supplier when the compiler cannot work it out by itself.

      This function is useful to disambiguate method-overloads when an array of mixed arguments should be passed all as type Supplier to a function that takes a number of Object parameters, such as Log4J log methods (in particular the methods where an exception is passed as last argument).

      For example:

              log.error("{} Error with message id [{}]", supplier(this::getLogPrefix), e);
          
      This can also be useful when for instance a no-arguments function should be passed to a JUnit arguments supplier for a parameterized unit test:
      	    public static Stream<Arguments> transactionManagers() {
      		    return Stream.of(
      			    Arguments.of(supplier(ReceiverTest::buildNarayanaTransactionManager))
      		    );
           }
          

      Type Parameters:
      T - Return type of the Supplier function.
      Parameters:
      s - Supplier lambda or function reference.
      Returns:
      The same Supplier but now type verified by the compiler.
    • function

      public static <T,R> Function<T,R> function(Function<T,R> f)
      Helper function to cast parameter as Function when the compiler cannot work it out by itself.

      This function is useful when a single-argument function needs to be passed to a method where the compiler cannot determine correct argument types due to method overloads of methods that take generic object parameters, such as JUnit Arguments.of().

      For Example:

       public static Stream<Arguments> transactionManagers() {
      		return Stream.of(
      			Arguments.of(function(ReceiverTest::buildNarayanaTransactionManager))
      		);
       }
      
      Type Parameters:
      T - Type of the argument to the function parameter.
      R - Return type of the function parameter.
      Parameters:
      f - Lambda or function reference that takes a single parameter and returns a value.
      Returns:
      Same lambda or function reference but now type verified by the compiler.
    • throwingLambda

      public static <T, E extends Exception> T throwingLambda(ThrowingSupplier<T,E> f)
      Allow a Lambda function to throw an exception in places where a checked exception is not expected, such as lambda functions in Streams.

      Lambda functions can usually not call methods that throw checked exceptions, such as IOException. These have to be surrounded with try/catch blocks which make the Lambda function harder to read.

      By wrapping the Lambda function with FunctionalUtil.throwingLambda(...), the checked exception is hidden from the compiler. The original checked exception will still be thrown by the code, but without triggering the compiler. It is therefore the caller's responsibility to handle these exceptions.

      Type Parameters:
      T - Return type of Lambda function
      E - Exception type of the Lambda function
      Parameters:
      f - The Lambda function to execute
      Returns:
      The return-value of the Lambda function