public final class LongConsumerUtils
extends java.lang.Object
long types.| Modifier and Type | Method and Description |
|---|---|
static <U> java.util.function.LongConsumer |
inverseLongConsumer(java.util.function.ObjLongConsumer<? super U> biConsumer,
U value)
Builds a
LongConsumer from a passed BiConsumer. |
static <U> java.util.function.LongConsumer |
longConsumer(java.util.function.BiConsumer<java.lang.Long,? super U> biConsumer,
U value)
Builds a
LongConsumer from a passed BiConsumer. |
static java.util.function.LongConsumer |
longConsumer(java.util.function.LongConsumer consumer)
Simply casts a method reference, which takes a single parameter of type
long and returns void, to
a LongConsumer. |
static java.util.function.LongConsumer |
longConsumer(java.lang.Runnable runnable)
Simply casts a method reference, which takes no parameters and returns void, to a
LongConsumer. |
static <U> java.util.function.LongConsumer |
longMapAndConsume(java.util.function.LongFunction<? extends U> function,
java.util.function.Consumer<U> consumer)
Builds a
LongConsumer from a passed LongFunction and a Consumer. |
static <T> java.util.function.Consumer<T> |
longSetter(java.util.function.BiConsumer<? super T,java.lang.Long> consumer,
java.util.function.ToLongFunction<T> extractor)
Builds a
Consumer from a passed BiConsumer. |
static <T> java.util.function.Consumer<T> |
mapToLongAndConsume(java.util.function.ToLongFunction<? super T> function,
java.util.function.LongConsumer consumer)
Applies a
ToLongFunction to a target element, before passing its result to a
LongConsumer. |
public static java.util.function.LongConsumer longConsumer(java.util.function.LongConsumer consumer)
long and returns void, to
a LongConsumer. 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 long 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 a LongConsumer.public static java.util.function.LongConsumer longConsumer(java.lang.Runnable runnable)
LongConsumer. 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 LongStream.forEach(...) method. In the
following example, assume that processLong() takes a single long parameter and has no
return value, and logCurrentState() takes no parameter and has no return value:
long[] longs = ...
Arrays.stream(longs).forEach(LongConsumerUtils.longConsumer(this::processLong)
.andThen(LongConsumerUtils.longConsumer(this::logCurrentState)));
Or, with static imports:
Arrays.stream(longs).forEach(longConsumer(this::processLong)
.andThen(longConsumer(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 a LongConsumer does come in handy at times.
Note that this method can also be used to cast a Supplier method reference to a
LongConsumer, 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 a LongConsumer.public static <U> java.util.function.LongConsumer longConsumer(java.util.function.BiConsumer<java.lang.Long,? super U> biConsumer,
U value)
LongConsumer 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 long 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 LongConsumer interface, as it says in the Javadoc documentation for
it, "Unlike most other functional interfaces, LongConsumer 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 long, and
the second of type <U>, which can be any type. The method reference will be converted by
this method to a LongConsumer, taking a single parameter of type long. 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.LongConsumer inverseLongConsumer(java.util.function.ObjLongConsumer<? super U> biConsumer,
U value)
LongConsumer 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 long 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 LongConsumer interface, as it says in the Javadoc documentation for
it, "Unlike most other functional interfaces, LongConsumer 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 long. The method reference will be converted by
this method to a LongConsumer, taking a single parameter of type long. 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> longSetter(java.util.function.BiConsumer<? super T,java.lang.Long> consumer,
java.util.function.ToLongFunction<T> extractor)
Consumer from a passed BiConsumer. Everything said about the
DblConsumerUtils.dblSetter(BiConsumer, ToDoubleFunction) applies here. The difference is simply that the
extractor function parameter is of type ToLongFunction 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.extractor - An extractor function that, given the target element, returns the long value to be set.public static <T> java.util.function.Consumer<T> mapToLongAndConsume(java.util.function.ToLongFunction<? super T> function,
java.util.function.LongConsumer consumer)
ToLongFunction to a target element, before passing its result to a
LongConsumer. 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 a long):
private void validateLineItems(Collection<OrderLineItem> lineItems, String customerId) {
lineItems.forEach(LongConsumerUtils.mapToLongAndConsume(OrderLineItem::getQuantity, LongConsumerUtils.longConsumer(this::validateQuantity, customerId)));
}
private String validateQuantity(long quantity, String customerId) {
...
}
Or, with static imports:
lineItems.forEach(mapToLongAndConsume(OrderLineItem::getQuantity, longConsumer(this::validateQuantity, customerId)));
Note that the same thing could be done like this:
lineItems.stream()
.map(OrderLineItem::getQuantity)
.forEach(longConsumer(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 ToLongFunction to be applied to a target element.consumer - A LongConsumer to be applied to the result of a ToLongFunction.public static <U> java.util.function.LongConsumer longMapAndConsume(java.util.function.LongFunction<? extends U> function,
java.util.function.Consumer<U> consumer)
LongConsumer from a passed LongFunction 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
long instead. Also, this method takes a LongFunction 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 LongConsumer interface, as it says in the Javadoc documentation for
it, "Unlike most other functional interfaces, LongConsumer is expected to operate via side-effects."
U - The type of the result of an LongFunction.function - A method reference which is a LongFunction, taking a single long parameter, and returning a value
of type <U>.consumer - A Consumer<U>, which will be passed the result of a LongFunction.