public final class SupplierUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static <T,U> org.perro.functions.supplier.ConstantValues<T,U> |
constantValues(T left,
U right)
Builds an object representing a pair of constant values of type <T> and <U> to be used as the second
parameter to the above
supplier(BiFunction, ConstantValues) method. |
static <T,U,R> java.util.function.Supplier<R> |
lazySupplier(java.util.function.BiFunction<T,U,R> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
First creates a supplier using the
supplier(BiFunction, ConstantValues) method, and turns it into a lazy
supplier. |
static <T,R> java.util.function.Supplier<R> |
lazySupplier(java.util.function.Function<T,R> function,
T value)
First creates a supplier using the
supplier(Function, Object) method, and turns it into a lazy supplier. |
static <T> java.util.function.Supplier<T> |
lazySupplier(java.util.function.Supplier<T> supplier)
Takes a
Supplier and turns it into a lazy supplier. |
static <T,U,R> java.util.function.Supplier<R> |
supplier(java.util.function.BiFunction<T,U,R> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
Builds a
Supplier of objects of type <R> from a passed BiFunction taking
parameters of type <T> and <U>. |
static <T,R> java.util.function.Supplier<R> |
supplier(java.util.function.Function<T,R> function,
T value)
Builds a
Supplier of objects of type <R> from a passed Function taking a
parameter of type <T>. |
public static <T,R> java.util.function.Supplier<R> supplier(java.util.function.Function<T,R> function,
T value)
Supplier of objects of type <R> from a passed Function taking a
parameter of type <T>. This can be very useful in the situation where you have a reference to a method that
takes an object of type <T>, and an object to be passed to that method reference, which will be used for
every invocation of it. In the following example we want to find the first order line item in an order, with a
quantity greater than zero (assume there are static imports for the classes in this library):
private OrderLineItem getFirstItemWithQuantity(Order order) {
String errorMessage = "Order must contain a line item with non-zero quantity";
return defaultStream(order.getLineItems())
.filter(intGt(OrderLineItem::getQuantity))
.findFirst()
.orElseThrow(supplier(IllegalArgumentException::new, errorMessage));
}
T - The type of the parameter to the passed Function. Also the type of the constant value passed to
this method.R - The return type of the passed Function, and the Supplier built by this method.function - A Function to be used to build a supplier of values of type <R>.value - A constant value of type <T> to be passed to every invocation of the above Function.public static <T,U,R> java.util.function.Supplier<R> supplier(java.util.function.BiFunction<T,U,R> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
Supplier of objects of type <R> from a passed BiFunction taking
parameters of type <T> and <U>. This can be very useful in the situation where you have a reference
to a method that takes parameters of type <T> and <U>, and an object representing a pair of constant
values to be passed to every invocation of that method reference. In the following example we want to find the
first order line item in an order, with a quantity greater than zero (assume there are static imports for the
classes in this library):
private OrderLineItem getFirstItemWithQuantity(Order order, String customerId) {
return defaultStream(order.getLineItems())
.filter(intGt(OrderLineItem::getQuantity))
.findFirst()
.orElseThrow(supplier(this::buildNonZeroQuantityException, order.getId(), customerId));
}
private RuntimeException buildNonZeroQuantityException(String orderId, String customerId) {
String errorMessage = "Order must contain a line item with non-zero quantity - orderId: %s, customerId: %s";
return new IllegalArgumentException(String.format(errorMessage, orderId, customerId));
}
T - The type of the first parameter to the passed BiFunction. 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 BiFunction. Also the type of the second constant
value in the constants pair passed to this method.R - The return type of the passed Function, and the Supplier built by this method.function - A BiFunction taking arguments of type <T> and <U>, to be used to build a supplier of
values of type <R>.constants - An object representing a pair of constant values of type <T> and <U>, to be passed
to every invocation of the above BiFunction. This value should be supplied via a call to the
constantValues(Object, Object) method.public static <T> java.util.function.Supplier<T> lazySupplier(java.util.function.Supplier<T> supplier)
Supplier 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 object to be retrieved lazily.supplier - A supplier to be called only once, and whose result will be cached.public static <T,R> java.util.function.Supplier<R> lazySupplier(java.util.function.Function<T,R> function,
T value)
supplier(Function, 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 Function. Also the type of the constant value passed to
this method.R - The return type of the passed Function, and the lazy Supplier built by this method.function - A Function to be used to build a supplier of a value of type <R>.value - A constant value of type <T> to be passed to the single invocation of the above Function.public static <T,U,R> java.util.function.Supplier<R> lazySupplier(java.util.function.BiFunction<T,U,R> function,
org.perro.functions.supplier.ConstantValues<T,U> constants)
supplier(BiFunction, ConstantValues) method, and turns it into a lazy
supplier.T - The type of the first parameter to the passed BiFunction. 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 BiFunction. Also the type of the second constant
value in the constants pair passed to this method.R - The return type of the passed Function, and the lazy Supplier built by this method.function - A BiFunction taking arguments of type <T> and <U>, to be used to build a lazy
supplier of a value of type <R>.constants - An object representing a pair of constant values of type <T> and <U>, to be passed
to the single invocation of the above BiFunction. This value should be supplied via a call to
the constantValues(Object, Object) method.public static <T,U> org.perro.functions.supplier.ConstantValues<T,U> constantValues(T left,
U right)
supplier(BiFunction, ConstantValues) method.T - The type of the first constant in the pair built by this method.U - The type of the second constant in the pair built by this method.left - A value of type <T> representing the first constant in the pair built by this method.right - A value of type <U> representing the second constant in the pair built by this method.