Assume I have this method
public Optional<String> validateAndReturnErrorMessage(SomeEcommerceSale s) {
Optional<String> errorWithItem = validateItem(s.getItem());
if (errorWithItem.isPresent()) return errorWithItem;
Optional<String> errorWithPayment = validatePayment(s.getPayment());
if (errorWithPayment.isPresent()) return errorWithPayment;
// assume I have several other cases like the 2 above
return Optional.empty(); // meaning there were no errors
My problem is, since OrElse and OrElseGet
return <T>
and not Optional<T>
, is there a native way to do rewrite this into a more functional way without having several loosely coupled return statements?
EDIT
I would like to check validations one by one, since they are external and heavy. This way, I would call only the necessary ones