public final class IntConsumerUtils
extends java.lang.Object
int types.| Modifier and Type | Method and Description |
|---|---|
static <U> java.util.function.IntConsumer |
intConsumer(java.util.function.BiConsumer<java.lang.Integer,? super U> biConsumer,
U value)
Builds an
IntConsumer from a passed BiConsumer. |
static java.util.function.IntConsumer |
intConsumer(java.util.function.IntConsumer consumer)
Simply casts a method reference, which takes a single parameter of type
int and returns void, to
an IntConsumer. |
static java.util.function.IntConsumer |
intConsumer(java.lang.Runnable runnable)
Simply casts a method reference, which takes no parameters and returns void, to an
IntConsumer. |
static <U> java.util.function.IntConsumer |
intMapAndConsume(java.util.function.IntFunction<? extends U> function,
java.util.function.Consumer<U> consumer)
Builds an
IntConsumer from a passed IntFunction and a Consumer. |
static <T> java.util.function.Consumer<T> |
intSetter(java.util.function.BiConsumer<? super T,java.lang.Integer> consumer,
java.util.function.ToIntFunction<T> function)
Builds a
Consumer from a passed BiConsumer. |
static <U> java.util.function.IntConsumer |
inverseIntConsumer(java.util.function.ObjIntConsumer<? super U> biConsumer,
U value)
Builds an
IntConsumer from a passed BiConsumer. |
static <T> java.util.function.Consumer<T> |
mapToIntAndConsume(java.util.function.ToIntFunction<? super T> function,
java.util.function.IntConsumer consumer)
Applies a
ToIntFunction to a target element, before passing its result to an
IntConsumer. |
public static java.util.function.IntConsumer intConsumer(java.util.function.IntConsumer consumer)
int and returns void, to
an IntConsumer. Everything said about the ConsumerUtils.consumer(Consumer) method applies
here. The difference is that instead of an element of type <T> being streamed through, it would be a
primitive int instead. It may be harder to think of a situation where this overload would be useful,
but this method is included for sake of completeness.consumer - A method reference to be cast to an IntConsumer.public static java.util.function.IntConsumer intConsumer(java.lang.Runnable runnable)
IntConsumer. This
could be useful in a situation where you have a method that takes no parameters, and has no return value, which
you would like to call in a stream, for example, in the IntStream.forEach(...) method. In the
following example, assume that processInt() takes a single int parameter and has no
return value, and logCurrentState() takes no parameters and has no return value:
int[] ints = ...
Arrays.stream(ints).forEach(IntConsumerUtils.intConsumer(this::processInt)
.andThen(IntConsumerUtils.intConsumer(this::logCurrentState)));
Or, with static imports:
Arrays.stream(ints).forEach(intConsumer(this::processInt)
.andThen(intConsumer(this::logCurrentState)));
Admittedly, the fact that we are using forEach(...) here, using object state for the logging, and
not returning any values, makes this code imperative, and not functional. However, casting a
Runnable to an IntConsumer does come in handy at times.
Note that this method can also be used to cast a Supplier method reference to an
IntConsumer, that is a reference to a method that takes no parameters, and returns an object of
any type.
runnable - A method reference taking no parameters and having a return value of any type, including no
return value, to be cast to an IntConsumer.public static <U> java.util.function.IntConsumer intConsumer(java.util.function.BiConsumer<java.lang.Integer,? super U> biConsumer,
U value)
IntConsumer from a passed BiConsumer. Everything said about the
ConsumerUtils.consumer(BiConsumer, Object) method applies here. The difference is that instead of an
element of type <T> being streamed through, it would be a primitive int instead. It may be
harder to think of a situation where this overload would be useful, but this method is included for sake of
completeness.
One note about using the Java IntConsumer interface, as it says in the Javadoc documentation for
it, "Unlike most other functional interfaces, IntConsumer is expected to operate via side-effects."
U - The type of the constant value to be passed as the second parameter to each invocation of
biConsumer.biConsumer - A method reference which is a BiConsumer, taking two parameters - the first of type int, and
the second of type <U>, which can be any type. The method reference will be converted by
this method to an IntConsumer, taking a single parameter of type int. Behind the scenes, this
BiConsumer will be called, passing the constant value to each invocation as the second
parameter.value - A constant value, in that it will be passed to every invocation of the passed biConsumer as the
second parameter to it, and will have the same value for each of them.public static <U> java.util.function.IntConsumer inverseIntConsumer(java.util.function.ObjIntConsumer<? super U> biConsumer,
U value)
IntConsumer from a passed BiConsumer. Everything said about the
ConsumerUtils.inverseConsumer(BiConsumer, Object) method applies here. The difference is that instead of
an element of type <T> being streamed through, it would be a primitive int instead. It may be
harder to think of a situation where this overload would be useful, but this method is included for sake of
completeness.
One note about using the Java IntConsumer interface, as it says in the Javadoc documentation for
it, "Unlike most other functional interfaces, IntConsumer is expected to operate via side-effects."
U - The type of the constant value to be passed as the first parameter to each invocation of
biConsumer.biConsumer - A method reference which is a BiConsumer, taking two parameters - the first of type <U>
which can be any type, and the second of type int. The method reference will be converted by
this method to an IntConsumer, taking a single parameter of type int. Behind the scenes, this
BiConsumer will be called, passing the constant value to each invocation as the first parameter.value - A constant value, in that it will be passed to every invocation of the passed biConsumer as the
first parameter to it, and will have the same value for each of them.public static <T> java.util.function.Consumer<T> intSetter(java.util.function.BiConsumer<? super T,java.lang.Integer> consumer,
java.util.function.ToIntFunction<T> function)
Consumer from a passed BiConsumer. Everything said about the
DblConsumerUtils.dblSetter(BiConsumer, ToDoubleFunction) applies here. The difference is simply that the
function parameter is of type ToIntFunction rather than
ToDoubleFunction.T - The type of the target input element.consumer - A setter method reference from the class of the target element. It is a BiConsumer because the
first parameter will be the element itself, and the second parameter is the value to be set on
it.function - A function that, given the target element, returns the int value to be set.public static <T> java.util.function.Consumer<T> mapToIntAndConsume(java.util.function.ToIntFunction<? super T> function,
java.util.function.IntConsumer consumer)
ToIntFunction to a target element, before passing its result to an
IntConsumer. For example, let's say that we have a collection of order line items, and we want to
call a validation method to make sure that the quantity for the current line item is appropriate for a given
customer (the OrderLineItem.getQuantity() method returns an int):
private void validateLineItems(Collection<OrderLineItem> lineItems, String customerId) {
lineItems.forEach(IntConsumerUtils.mapToIntAndConsume(OrderLineItem::getQuantity, IntConsumerUtils.intConsumer(this::validateQuantity, customerId)));
}
private String validateQuantity(int quantity, String customerId) {
...
}
Or, with static imports:
lineItems.forEach(mapToIntAndConsume(OrderLineItem::getQuantity, intConsumer(this::validateQuantity, customerId)));
Note that the same thing could be done like this:
lineItems.stream()
.map(OrderLineItem::getQuantity)
.forEach(intConsumer(this::validateQuantity, customerId));
Which of the above is more concise and readable is up to the individual developer, but this method provides an
alternative way of accomplishing the above validation.T - The type of the target input element.function - A ToIntFunction to be applied to a target element.consumer - An IntConsumer to be applied to the result of a ToIntFunction.public static <U> java.util.function.IntConsumer intMapAndConsume(java.util.function.IntFunction<? extends U> function,
java.util.function.Consumer<U> consumer)
IntConsumer from a passed IntFunction and a Consumer.
Everything said about the ConsumerUtils.mapAndConsume(Function, Consumer) method applies here. The
difference is that instead of an element of type <T> being streamed through, it would be a primitive
int instead. Also, this method takes an IntFunction rather than a generic
Function. It may be harder to think of a situation where this method would be useful, but it is
included for sake of completeness.
One note about using the Java IntConsumer interface, as it says in the Javadoc documentation for
it, "Unlike most other functional interfaces, IntConsumer is expected to operate via side-effects."
U - The type of the result of an IntFunction.function - A method reference which is an IntFunction, taking a single int parameter, and returning a value
of type <U>.consumer - A Consumer<U>, which will be passed the result of an IntFunction.