public class FunctionalUtil extends Object
Modifier and Type | Method and Description |
---|---|
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. |
static <T> org.apache.logging.log4j.util.Supplier<T> |
logValue(T value) |
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. |
static <T> Supplier<T> |
supply(T value)
Helper function to create a
Supplier to supply the single constant argument value. |
public static <T> Supplier<T> supply(T value)
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);
T
- Type of the value to be supplied.value
- Value to be supplied. NB: This should be a constant, otherwise its value is instantly
computed instead of being delayed on-demand!Supplier
that will return the value
parameter.public static <T> org.apache.logging.log4j.util.Supplier<T> logValue(T value)
public static <T> Supplier<T> supplier(Supplier<T> s)
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), supply(messageId), 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)),
Arguments.of(supplier(ReceiverTest::buildBtmTransactionManager))
);
}
T
- Return type of the Supplier
function.s
- Supplier lambda or function reference.public static <T,R> Function<T,R> function(Function<T,R> f)
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)),
Arguments.of(function(ReceiverTest::buildBtmTransactionManager))
);
}
T
- Type of the argument to the function parameter.R
- Return type of the function parameter.f
- Lambda or function reference that takes a single parameter and returns a value.Copyright © 2023 Frank!Framework. All rights reserved.