I have a Validator
interface which provides a isValid(Thing)
method, returning a ValidationResult
which contains a boolean
and a reason message.
I want to create a ValidatorAggregator
implementation of this interface which performs an OR across multiple Validator
s (if any Validator
returns a positive result, then the result is positive). If any validator succeeds, I'd like to short-circuit and just return its result. If no validator succeeds, I want to return all of the failure messages.
I can do this succinctly using a stream and findFirst().orElse(...)
but using this pattern I lose all the intermediate results if findFirst
returns empty:
public ValidationResult isValid(final Thing thing) {
return validators.stream()
.map(v -> validator.isValid(thing))
.filter(ValidationResult::isValid)
.findFirst()
.orElseGet(() -> new ValidationResult(false, "All validators failed'));
}
Is there any way to capture the failed results using a stream, or indeed just more succinctly than the below?
public ValidationResult isValid(final Thing thing) {
final Set<ValidationResult> failedResults = new HashSet<>();
for (Validator validator : validators) {
final ValidationResult result = validator.isValid(thing);
if (result.isValid()) {
return result;
}
failedResults.add(result);
}
return new ValidationResult(false, "No successful validator: " + failedResults);
// (assume failedResults stringifies nicely)
}
Edit: based on comments, I agree what I'm trying to do is premature optimisation (particularly as these validators are very lightweight). I'll probably go with something similar to Holger's solution of computing all validations and partitioning into successful/unsuccessful results.
This was marked as a dupe of Can you split a stream into two streams? and the partitioningBy
answer sort-of is, but I think this question is asking, and the discussion answering, a different problem.