public final class DblSupplierUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static <T,U> java.util.function.DoubleSupplier |
dblSupplier(java.util.function.ToDoubleBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
Builds a
DoubleSupplier from a passed BiFunction which takes parameters of type
<T> and <U>, and returns a Double. |
static <T> java.util.function.DoubleSupplier |
dblSupplier(java.util.function.ToDoubleFunction<T> function,
T value)
Builds a
DoubleSupplier from a passed ToDoubleFunction which takes a parameter of type
<T>. |
static java.util.function.DoubleSupplier |
lazyDblSupplier(java.util.function.DoubleSupplier supplier)
Takes a
DoubleSupplier and turns it into a lazy supplier. |
static <T,U> java.util.function.DoubleSupplier |
lazyDblSupplier(java.util.function.ToDoubleBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
First creates a supplier using the
dblSupplier(ToDoubleBiFunction, ConstantValues) method, and turns it
into a lazy supplier. |
static <T> java.util.function.DoubleSupplier |
lazyDblSupplier(java.util.function.ToDoubleFunction<T> function,
T value)
First creates a supplier using the
dblSupplier(ToDoubleFunction, Object) method, and turns it into a
lazy supplier. |
public static <T> java.util.function.DoubleSupplier dblSupplier(java.util.function.ToDoubleFunction<T> function,
T value)
DoubleSupplier from a passed ToDoubleFunction which takes a parameter of type
<T>. Everything said about the SupplierUtils.supplier(Function, Object) method applies here. The
difference is that instead of the supplier returning a result of type <R>, it returns a primitive
double 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 also be useful
in a situation where, let's say you have a utility method that takes an object Double, and a
DoubleSupplier to return a definite result when the passed Double value is
null:
public final class DoubleUtils {
...
public static double getDoubleWithDefault(Double value, DoubleSupplier defaultSupplier) {
return value == null ? defaultSupplier.getAsDouble() : value.doubleValue();
}
...
}
Let's assume that, in order to get the default, you have to call a separate micro-service, or retrieve the value
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 double getCustomerDiscount(String customerId) {
Map<String, Double> discountByCustId = ...
DoubleSupplier discountSupplier = DblSupplierUtils.dblSupplier(this::getCustomerDiscount, customerId);
return DoubleUtils.getDoubleWithDefault(discountByCustId.get(custId), discountSupplier);
}
private double getCustomerDiscount(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 to 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 ToDoubleFunction, taking a parameter of type <T>.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.DoubleSupplier dblSupplier(java.util.function.ToDoubleBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
DoubleSupplier from a passed BiFunction which takes parameters of type
<T> and <U>, and returns a Double. 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 double 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 Double, and a DoubleSupplier to return
a definite result when the passed Double value is null:
public final class DoubleUtils {
...
public static double getDoubleWithDefault(Double value, DoubleSupplier defaultSupplier) {
return value == null ? defaultSupplier.getAsDouble() : value.doubleValue();
}
...
}
Let's assume that, in order to get the default, you have to call a separate micro-service, or retrieve the value
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 double getCustomerDiscount(String customerId, String purchaseOrderId) {
Map<String, Double> discountByCustId = ...
DoubleSupplier discountSupplier = DoubleSupplierUtils.dblSupplier(this::getDiscount,
SupplierUtils.constantValues(customerId, purchaseOrderId));
return DoubleUtils.getDoubleWithDefault(discountByCustId.get(custId), discountSupplier);
}
private double getDiscount(String custId, String purchaseOrderId) {
...
}
Or, with static imports:
...
DoubleSupplier discountSupplier = dblSupplier(this::getDiscount, 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 ToDoubleBiFunction, taking parameters of type <T> and
<U>, and returning a double.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.public static java.util.function.DoubleSupplier lazyDblSupplier(java.util.function.DoubleSupplier supplier)
DoubleSupplier and turns it into a lazy supplier. The behavior is that the passed supplier
is called only the first time a value is retrieved, and caches its result for subsequent invocations. Useful in
many situations where retrieving a value is an expensive operation, such as retrieving an object from a database,
or calling a micro-service.supplier - A supplier to be called only once, and whose result will be cached.public static <T> java.util.function.DoubleSupplier lazyDblSupplier(java.util.function.ToDoubleFunction<T> function,
T value)
dblSupplier(ToDoubleFunction, Object) method, and turns it into a
lazy supplier. The behavior is that the passed supplier is called only the first time a value is retrieved, and
caches its result for subsequent invocations. Useful in many situations where retrieving a value is an expensive
operation, such as retrieving an object from a database, or calling a micro-service.T - The type of the parameter to the passed ToDoubleFunction. Also the type of the constant value
passed to this method.function - A ToDoubleFunction to be used to build a supplier of a double value.value - A constant value of type <T> to be passed to the single invocation of the above
ToDoubleFunction.public static <T,U> java.util.function.DoubleSupplier lazyDblSupplier(java.util.function.ToDoubleBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
dblSupplier(ToDoubleBiFunction, ConstantValues) method, and turns it
into a lazy supplier. The behavior is that the passed supplier is called only the first time a value is
retrieved, and caches its result for subsequent invocations. Useful in many situations where retrieving a value
is an expensive operation, such as retrieving an object from a database, or calling a micro-service.T - The type of the first parameter to the passed ToDoubleBiFunction. Also the type of the first
constant value in the constants pair passed to this method.U - The type of the second parameter to the passed ToDoubleBiFunction. Also the type of the second
constant value in the constants pair passed to this method.function - A ToDoubleBiFunction taking arguments of type <T> and <U>, to be used to build a
lazy supplier of a double value.constants - An object representing a pair of constant values of type <T> and <U>, to be passed
to the single invocation of the above ToDoubleBiFunction. This value should be supplied via a
call to the SupplierUtils.constantValues(Object, Object) method.