I wanted to leverage java 8 optional to validate values of the an object received (as a response). I'm curious to know, if it is a bad practice to do as below.
Optional.ofNullable(response)
.map(Response::getStatus)
.filter(status -> {
if (status == Status.REJECTED)
throw new RequestRejectedException("some exception");
else if (status == Status.LOCKED)
throw new ResourceLockedException("some other exception");
return true;
})
.orElse(Status.UNAVAILABLE);
Wanted to know, if this is acceptable to write something like above or if there is a better way to do it, please suggest.