I can't seem to figure out a more concise way to do this
Optional<String> foo;
if (!foo.isPresent() || StringUtils.isBlank(foo.get())) {
// ...
}
There's this but it actually makes the logic more convoluted, IMO:
Optional<String> foo;
if (!foo.filter(StringUtils::isNotBlank).isPresent()) {
// ...
}
Feel like I'm missing something really obvious.
Maybe this is what you're looking for.
Optional.of("")
.filter(not(String::isBlank)))
.orElseThrow(() -> new MyException("..."));
You'll find not under Predicate.
Obviosuly this is less efficient than having an if statement, but it has its place.
Ah, ok, after your comment it is more clear. Use orElseThrow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With