public final class LongSupplierUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static java.util.function.LongSupplier |
lazyLongSupplier(java.util.function.LongSupplier supplier)
Takes a
LongSupplier and turns it into a lazy supplier. |
static <T,U> java.util.function.LongSupplier |
lazyLongSupplier(java.util.function.ToLongBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
First creates a supplier using the
longSupplier(ToLongBiFunction, ConstantValues) method, and turns it
into a lazy supplier. |
static <T> java.util.function.LongSupplier |
lazyLongSupplier(java.util.function.ToLongFunction<T> function,
T value)
First creates a supplier using the
longSupplier(ToLongFunction, Object) method, and turns it into a
lazy supplier. |
static <T,U> java.util.function.LongSupplier |
longSupplier(java.util.function.ToLongBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
Builds a
LongSupplier from a passed BiFunction which takes parameters of type
<T> and <U>, and returns a Long. |
static <T> java.util.function.LongSupplier |
longSupplier(java.util.function.ToLongFunction<T> function,
T value)
Builds a
LongSupplier from a passed ToLongFunction which takes a parameter of type
<T>. |
public static <T> java.util.function.LongSupplier longSupplier(java.util.function.ToLongFunction<T> function,
T value)
LongSupplier from a passed ToLongFunction 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
long 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 Long, and a
LongSupplier to return a definite result when the passed Long value is
null:
public final class LongUtils {
...
public static long getLongWithDefault(Long value, LongSupplier defaultSupplier) {
return value == null ? defaultSupplier.getAsLong() : value.longValue();
}
...
}
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 long getCustomerDiscount(String customerId) {
Map<String, Long> discountByCustId = ...
LongSupplier discountSupplier = LongSupplierUtils.longSupplier(this::getCustomerDiscount, customerId);
return LongUtils.getLongWithDefault(discountByCustId.get(custId), discountSupplier);
}
private long 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 ToLongFunction, 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.LongSupplier longSupplier(java.util.function.ToLongBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
LongSupplier from a passed BiFunction which takes parameters of type
<T> and <U>, and returns a Long. 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 long 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 Long, and a LongSupplier to return a
definite result when the passed Long value is null:
public final class LongUtils {
...
public static long getLongWithDefault(Long value, LongSupplier defaultSupplier) {
return value == null ? defaultSupplier.getAsLong() : value.longValue();
}
...
}
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 long getCustomerDiscount(String customerId, String purchaseOrderId) {
Map<String, Long> discountByCustId = ...
LongSupplier discountSupplier = LongSupplierUtils.longSupplier(this::getDiscount,
SupplierUtils.constantValues(customerId, purchaseOrderId));
return LongUtils.getLongWithDefault(discountByCustId.get(custId), discountSupplier);
}
private long getDiscount(String custId, String purchaseOrderId) {
...
}
Or, with static imports:
...
LongSupplier discountSupplier = longSupplier(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 ToLongBiFunction, taking parameters of type <T> and
<U>, and returning a long.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.LongSupplier lazyLongSupplier(java.util.function.LongSupplier supplier)
LongSupplier 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.LongSupplier lazyLongSupplier(java.util.function.ToLongFunction<T> function,
T value)
longSupplier(ToLongFunction, 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 ToLongFunction. Also the type of the constant value
passed to this method.function - A ToLongFunction to be used to build a supplier of a long value.value - A constant value of type <T> to be passed to the single invocation of the above
ToLongFunction.public static <T,U> java.util.function.LongSupplier lazyLongSupplier(java.util.function.ToLongBiFunction<T,U> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
longSupplier(ToLongBiFunction, 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 ToLongBiFunction. 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 ToLongBiFunction. Also the type of the second
constant value in the constants pair passed to this method.function - A ToLongBiFunction taking arguments of type <T> and <U>, to be used to build a
lazy supplier of a long 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 ToLongBiFunction. This value should be supplied via a call
to the SupplierUtils.constantValues(Object, Object) method.