I have a nullable object and I'm trying to throw an exception if the object is not null and does not meet a condition.
I try this way with Optional:
Optional.ofNullable(nullableObject)
.filter(object -> "A".equals(object.getStatus()))
.orElseThrow(() -> new BusinessUncheckedException("exception message"));
When the object is not null, it works as I want, but otherwise, it throws the exception too (and I don't want that).
There is a way to do that with Optional or other ways not using if object != null?