public final class BooleanSupplierUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static <T,U> java.util.function.BooleanSupplier |
booleanSupplier(java.util.function.BiFunction<T,U,java.lang.Boolean> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
Builds a
BooleanSupplier from a passed BiFunction which takes parameters of type
<T> and <U>, and returns a Boolean. |
static <T> java.util.function.BooleanSupplier |
booleanSupplier(java.util.function.Function<T,java.lang.Boolean> function,
T value)
Builds a
BooleanSupplier from a passed Function which takes a parameter of type
<T>, and returns a Boolean. |
public static <T> java.util.function.BooleanSupplier booleanSupplier(java.util.function.Function<T,java.lang.Boolean> function,
T value)
BooleanSupplier from a passed Function which takes a parameter of type
<T>, and returns a Boolean. Everything said about the
SupplierUtils.supplier(Function, Object) method applies here. The difference is that instead the supplier
returning a result of type <R>, it would return a primitive boolean instead. Suppliers are
useful in a variety of situations such as lazy loading or evaluation, including in logging as well as in working
with the Optional class. This method might be useful in a situation where, let's say you have a
utility method that takes an object Boolean, and a BooleanSupplier to return a definite
result when the passed Boolean value is null:
public final class BooleanUtils {
...
public static boolean getFlagWithDefault(Boolean flag, BooleanSupplier defaultSupplier) {
return flag == null ? defaultSupplier.getAsBoolean() : flag.booleanValue();
}
...
}
Let's assume that, in order to get the default, you have to call a separate micro-service, or retrieve the flag
from the database. In that case, you would want to do that only if the value was actually null, so
you pass a supplier which only gets invoked when necessary:
public boolean hasCustomerDiscount(String customerId) {
Map<String, Boolean> discountFlagByCustId = ...
BooleanSupplier flagSupplier = BooleanSupplierUtils.booleanSupplier(this::getDiscountFlag, customerId);
return BooleanUtils.getFlagWithDefault(discountFlagByCustId.get(custId), flagSupplier);
}
private boolean getDiscountFlag(String customerId) {
...
}
A number of generic utility methods take suppliers, because of the lazy loading/evaluation features they provide.
An issue with their use is, that because they don't know how many parameters a passed method reference might take,
whether zero, one, or more, they just end up taking a supplier with some kind of generic return value. If the
method reference takes zero parameters, then there's no problem, but if it takes one parameter, then this method
can be used to convert a function into a supplier.T - The type of the constant value to be passed as the single parameter to each invocation of
function.function - A method reference which is a Function, taking a parameter of type <T>, and returning a
Boolean.value - A constant value, in that it will be passed to every invocation of the passed function as the
single parameter to it, and will have the same value for each of them.public static <T,U> java.util.function.BooleanSupplier booleanSupplier(java.util.function.BiFunction<T,U,java.lang.Boolean> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
BooleanSupplier from a passed BiFunction which takes parameters of type
<T> and <U>, and returns a Boolean. Everything said about the
SupplierUtils.supplier(BiFunction, ConstantValues) method applies here. The difference is that instead of
the supplier returning values of type <R>, it would return a primitive boolean instead.
Suppliers are useful in a variety of situations such as lazy loading or evaluation, including in logging as well
as in working with the Optional class. This method might be useful in a situation where, let's say
you have a utility method that takes an object Boolean, and a BooleanSupplier to return
a definite result when the passed Boolean value is null:
public final class BooleanUtils {
...
public static boolean getFlagWithDefault(Boolean flag, BooleanSupplier defaultSupplier) {
return flag == null ? defaultSupplier.getAsBoolean() : flag.booleanValue();
}
...
}
Let's assume that, in order to get the default, you have to call a separate micro-service, or retrieve the flag
from the database. In that case, you would want to do that only if the value was actually null, so
you pass a supplier which only gets invoked when necessary:
public boolean hasCustomerDiscount(String customerId, String purchaseOrderId) {
Map<String, Boolean> discountFlagByCustId = ...
BooleanSupplier flagSupplier = BooleanSupplierUtils.booleanSupplier(this::getDiscountFlag,
SupplierUtils.constantValues(customerId, purchaseOrderId));
return BooleanUtils.getFlagWithDefault(discountFlagByCustId.get(custId), flagSupplier);
}
private boolean getDiscountFlag(String custId, String purchaseOrderId) {
...
}
Or, with static imports:
...
BooleanSupplier flagSupplier = booleanSupplier(this::getDiscountFlag, constantValues(customerId, purchaseOrderId));
...
A number of generic utility methods take suppliers, because of the lazy loading/evaluation features they provide.
An issue with their use is, that because they don't know how many parameters a passed method reference might take,
whether zero, one, or more, they just end up taking a supplier with some kind of generic return value. If the
method reference takes zero parameters, then there's no problem, but if it takes two parameters, then this method
can be used to convert a bi-function into a supplier.T - The type of the first constant value to be passed as the first parameter to each invocation of
function.U - The type of the second constant value to be passed as the second parameter to each invocation of
function.function - A method reference which is a BiFunction, taking parameters of type <T> and <U>, and
returning a Boolean.constants - A pair of constant values, in that they will be passed to every invocation of the passed
function as the first and second parameters to it, and will have the same value for each of
them.