I'm moving some code to java8, trying (sometimes forcing myself) to use of streams and lambdas, and I'm not comfortable yet with them.
I have some methods in a class that validate a business object. Every method looks like
Optional<Fail> validate1(BusinessObject bo)
where Fail is an enum that somehow describes the error, and if there's no error the method returns Optional.empty(). I don't need to collect all errors, but return the first error, without executing following validations.
What I'm doing is
//first convert methods to suppliers
Supplier<Optional<Fail>> validate1= () -> validate1(bo);
Supplier<Optional<Fail>> validate2= () -> validate2(bo);
Supplier<Optional<Fail>> validate3= () -> validate3(bo);
//then some stream magic
return Stream.of(validate1, validate2, validate3)
.map(Supplier::get)
.filter(f -> f.isPresent())
.findFirst()
.orElse(Optional.empty()); //without the orElse, no error would return
// Optional(Optional.empty())
// instead of Optional.empty()
It works, it does the job, it does not execute unnecessary methods, it's legible (it'd be more legible if Optional.orElse were named getOrElse, but that's out of my reach). What I'm trying to find out is if this is a reasonable way to do what I want, if this code would be considered 'good style' or 'idiomatic java8', or am I misusing Stream or Optional, or missing something obvious.
The idea of returning the first non-empty Optional or an empty Optional if they are all empty looks general enough to think there is an official way to do it, something in the back of my head is yelling 'Monads!', but my ignorance of Haskell is almost perfect, so I don't know.