I\'m afraid that this is somewhat a silly question.
Is there anybody can tell me why there is no BooleanConsumer
opposite to BooleanSupplier?
Is the
As other answers indicate there is no great reason to avoid Consumer
, but then there's no great reason to avoid Supplier
either, so a different explanation is required for this.
A similar question is why can't you switch on a boolean
value. The answer is that there's no need because you could always use if
or if else
.
A BooleanConsumer
would really be nothing more than an if else
construct because the accept()
method for a BooleanConsumer
could always be written in this form:
if (v) {
// Do something
} else {
// Do something else
}
If you needed to pass such code around as data, you could just pass a pair of Runnable
s representing "do something" and "do something else". In many cases, you would only need one of the Runnable
s because one of the two blocks above would be empty.
In the same way, there is no need for a BooleanPredicate
because it would be nothing more than a pair of BooleanSupplier
s and there is no need for a a BooleanFunction
because it would be nothing more than a pair of Supplier
s.
In contrast to this, it is not possible to break a BooleanSupplier
into two simpler objects.