public final class IntSupplierUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static <T,U> java.util.function.IntSupplier |
intSupplier(java.util.function.ToIntBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
Builds an
IntSupplier from a passed BiFunction which takes parameters of type
<T> and <U>, and returns an Integer. |
static <T> java.util.function.IntSupplier |
intSupplier(java.util.function.ToIntFunction<T> function,
T value)
Builds an
IntSupplier from a passed ToIntFunction which takes a parameter of type
<T>. |
static java.util.function.IntSupplier |
lazyIntSupplier(java.util.function.IntSupplier supplier)
Takes an
IntSupplier and turns it into a lazy supplier. |
static <T,U> java.util.function.IntSupplier |
lazyIntSupplier(java.util.function.ToIntBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
First creates a supplier using the
intSupplier(ToIntBiFunction, ConstantValues) method, and turns it
into a lazy supplier. |
static <T> java.util.function.IntSupplier |
lazyIntSupplier(java.util.function.ToIntFunction<T> function,
T value)
First creates a supplier using the
intSupplier(ToIntFunction, Object) method, and turns it into a lazy
supplier. |
public static <T> java.util.function.IntSupplier intSupplier(java.util.function.ToIntFunction<T> function,
T value)
IntSupplier from a passed ToIntFunction 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
int 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 Integer, and an
IntSupplier to return a definite result when the passed Integer value is
null:
public final class IntegerUtils {
...
public static int getIntWithDefault(Integer value, IntSupplier defaultSupplier) {
return value == null ? defaultSupplier.getAsInt() : value.intValue();
}
...
}
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 (assume the discount is represented as an integral
number of cents):
public int getCustomerDiscount(String customerId) {
Map<String, Integer> discountByCustId = ...
IntSupplier discountSupplier = IntSupplierUtils.intSupplier(this::getCustomerDiscount, customerId);
return IntegerUtils.getIntWithDefault(discountByCustId.get(custId), discountSupplier);
}
private int 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 ToIntFunction, 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.IntSupplier intSupplier(java.util.function.ToIntBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
IntSupplier from a passed BiFunction which takes parameters of type
<T> and <U>, and returns an Integer. 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 int 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 an IntSupplier to return a
definite result when the passed Integer value is null (assume the discount is
represented as an integral number of cents):
public final class IntegerUtils {
...
public static int getIntWithDefault(Integer value, IntSupplier defaultSupplier) {
return value == null ? defaultSupplier.getAsInt() : value.intValue();
}
...
}
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 (assume the discount is represented as an integral
number of cents):
public int getCustomerDiscount(String customerId, String purchaseOrderId) {
Map<String, Integer> discountByCustId = ...
IntSupplier discountSupplier = IntSupplierUtils.intSupplier(this::getDiscount,
SupplierUtils.constantValues(customerId, purchaseOrderId));
return IntegerUtils.getIntWithDefault(discountByCustId.get(custId), discountSupplier);
}
private int getDiscount(String custId, String purchaseOrderId) {
...
}
Or, with static imports:
...
IntSupplier discountSupplier = intSupplier(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 ToIntBiFunction, taking parameters of type <T> and
<U>, and returning an int.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.IntSupplier lazyIntSupplier(java.util.function.IntSupplier supplier)
IntSupplier 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.IntSupplier lazyIntSupplier(java.util.function.ToIntFunction<T> function,
T value)
intSupplier(ToIntFunction, 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 ToIntFunction. Also the type of the constant value
passed to this method.function - A ToIntFunction to be used to build a supplier of a int value.value - A constant value of type <T> to be passed to the single invocation of the above
ToIntFunction.public static <T,U> java.util.function.IntSupplier lazyIntSupplier(java.util.function.ToIntBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
intSupplier(ToIntBiFunction, 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 ToIntBiFunction. 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 ToIntBiFunction. Also the type of the second
constant value in the constants pair passed to this method.function - A ToIntBiFunction taking arguments of type <T> and <U>, to be used to build a
lazy supplier of an int 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 ToIntBiFunction. This value should be supplied via a call
to the SupplierUtils.constantValues(Object, Object) method.