Class FunctionalUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
R> Function <T, R> Helper function to cast parameter asFunction
when the compiler cannot work it out by itself.static <T> org.apache.logging.log4j.util.Supplier
<T> Helper function to create a Log4JSupplier
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 Log4JSupplier
to supply a single constant argument value.static <T> Supplier
<T> Helper function to cast parameter as aSupplier
when the compiler cannot work it out by itself.static <T> Supplier
<T> supply
(T value) Helper function to create aSupplier
to supply the single constant argument value.
-
Method Details
-
supply
Helper function to create aSupplier
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 ofObject
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 thevalue
parameter.
-
logValue
public static <T> org.apache.logging.log4j.util.Supplier<T> logValue(T value) Helper function to create a Log4JSupplier
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 thevalue
parameter.
-
logMethod
Helper function to create a Log4JSupplier
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 themethod
parameter.
-
supplier
Helper function to cast parameter as aSupplier
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 ofObject
parameters, such as Log4J log methods (in particular the methods where an exception is passed as last argument).For example:
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:log.error("{} Error with message id [{}]", supplier(this::getLogPrefix), e);
public static Stream<Arguments> transactionManagers() { return Stream.of( Arguments.of(supplier(ReceiverTest::buildNarayanaTransactionManager)) ); }
- Type Parameters:
T
- Return type of theSupplier
function.- Parameters:
s
- Supplier lambda or function reference.- Returns:
- The same Supplier but now type verified by the compiler.
-
function
Helper function to cast parameter asFunction
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.
-