public final class DblConsumerUtils
extends java.lang.Object
double types.| Modifier and Type | Method and Description |
|---|---|
static <U> java.util.function.DoubleConsumer |
dblConsumer(java.util.function.BiConsumer<java.lang.Double,? super U> biConsumer,
U value)
Builds a
DoubleConsumer from a passed BiConsumer. |
static java.util.function.DoubleConsumer |
dblConsumer(java.util.function.DoubleConsumer consumer)
Simply casts a method reference, which takes a single parameter of type
double and returns void, to
a DoubleConsumer. |
static java.util.function.DoubleConsumer |
dblConsumer(java.lang.Runnable runnable)
Simply casts a method reference, which takes no parameters and returns void, to a
DoubleConsumer. |
static <U> java.util.function.DoubleConsumer |
dblMapAndConsume(java.util.function.DoubleFunction<? extends U> function,
java.util.function.Consumer<U> consumer)
Builds a
DoubleConsumer from a passed DoubleFunction and a Consumer. |
static <T> java.util.function.Consumer<T> |
dblSetter(java.util.function.BiConsumer<? super T,java.lang.Double> consumer,
java.util.function.ToDoubleFunction<T> function)
This method is a variation of the
consumer(...) methods that has some special properties. |
static <U> java.util.function.DoubleConsumer |
inverseDblConsumer(java.util.function.ObjDoubleConsumer<? super U> biConsumer,
U value)
Builds a
DoubleConsumer from a passed BiConsumer. |
static <T> java.util.function.Consumer<T> |
mapToDblAndConsume(java.util.function.ToDoubleFunction<? super T> function,
java.util.function.DoubleConsumer consumer)
Applies a
ToDoubleFunction to a target element, before passing its result to a
DoubleConsumer. |
public static java.util.function.DoubleConsumer dblConsumer(java.util.function.DoubleConsumer consumer)
double and returns void, to
a DoubleConsumer. 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 double 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 DoubleConsumer.public static java.util.function.DoubleConsumer dblConsumer(java.lang.Runnable runnable)
DoubleConsumer.
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 DoubleStream.forEach(...) method. In
the following example, assume that processDouble() takes a single double parameter and
has no return value, and logCurrentState() takes no parameter and has no return value:
double[] doubles = ...
Arrays.stream(doubles).forEach(DblConsumerUtils.dblConsumer(this::processDouble)
.andThen(DblConsumerUtils.dblConsumer(this::logCurrentState)));
Or, with static imports:
Arrays.stream(doubles).forEach(dblConsumer(this::processDouble)
.andThen(dblConsumer(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 DoubleConsumer does come in handy at times.
Note that this method can also be used to cast a Supplier method reference to a
DoubleConsumer, 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 DoubleConsumer.public static <U> java.util.function.DoubleConsumer dblConsumer(java.util.function.BiConsumer<java.lang.Double,? super U> biConsumer,
U value)
DoubleConsumer 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 double 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 DoubleConsumer interface, as it says in the Javadoc documentation for
it, "Unlike most other functional interfaces, DoubleConsumer 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 double, and
the second of type <U>, which can be any type. The method reference will be converted by
this method to a DoubleConsumer, taking a single parameter of type double. 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.DoubleConsumer inverseDblConsumer(java.util.function.ObjDoubleConsumer<? super U> biConsumer,
U value)
DoubleConsumer 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 double 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 DoubleConsumer interface, as it says in the Javadoc documentation for
it, "Unlike most other functional interfaces, DoubleConsumer 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 double. The method reference will be converted by
this method to a DoubleConsumer, taking a single parameter of type double. 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> dblSetter(java.util.function.BiConsumer<? super T,java.lang.Double> consumer,
java.util.function.ToDoubleFunction<T> function)
consumer(...) methods that has some special properties. First, it
is assumed that a setter method is being called on the target element, so there is a null check to make sure that
it is an object instance. If not, the Consumer built by this method simply returns. Second, it takes
a function that, given the target element, it returns the value to be passed to the setter. This means that any
of the methods of MapperUtils are fair game to be used to build that function. For example, suppose we
want to write a method that, given a collection of Widget instances, it will calculate and set a
price for each of them. We could use this method to help accomplish that in the following contrived example
(also note that is generally not a good idea to use a double for currency amounts):
private void buildWidgetPrices(Collection<Widget> widgets, String customerId) {
double discount = getCustomerDiscount(customerId);
widgets.forEach(ConsumerUtils.setter(Widget::setPrice, MapperUtils.mapper(this::calculatePrice, discount)));
}
private double calculatePrice(Widget widget, double discount) {
...
}
Or, with static imports:
widgets.forEach(setter(Widget::setPrice, mapper(this::calculatePrice, discount)));
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 double value to be set.public static <T> java.util.function.Consumer<T> mapToDblAndConsume(java.util.function.ToDoubleFunction<? super T> function,
java.util.function.DoubleConsumer consumer)
ToDoubleFunction to a target element, before passing its result to a
DoubleConsumer. 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 discount for the current line item is appropriate for a given
customer (the OrderLineItem.getDiscount() method returns a double):
private void validateLineItems(Collection<OrderLineItem> lineItems, String customerId) {
lineItems.forEach(DblConsumerUtils.mapToDblAndConsume(OrderLineItem::getDiscount, DblConsumerUtils.dblConsumer(this::validateDiscount, customerId)));
}
private String validateDiscount(double discount, String customerId) {
...
}
Or, with static imports:
lineItems.forEach(mapToDblAndConsume(OrderLineItem::getDiscount, dblConsumer(this::validateDiscount, customerId)));
Note that the same thing could be done like this:
lineItems.stream()
.map(OrderLineItem::getDiscount)
.forEach(dblConsumer(this::validateDiscount, 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 ToDoubleFunction to be applied to a target element.consumer - A DoubleConsumer to be applied to the result of a ToDoubleFunction.public static <U> java.util.function.DoubleConsumer dblMapAndConsume(java.util.function.DoubleFunction<? extends U> function,
java.util.function.Consumer<U> consumer)
DoubleConsumer from a passed DoubleFunction 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
double instead. Also, this method takes a DoubleFunction 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 DoubleConsumer interface, as it says in the Javadoc documentation for
it, "Unlike most other functional interfaces, DoubleConsumer is expected to operate via side-effects."
U - The type of the result of a DoubleFunction.function - A method reference which is a DoubleFunction, taking a single double parameter, and returning a
value of type <U>.consumer - A Consumer<U>, which will be passed the result of a DoubleFunction.