Return first result matching predicate in a Java stream or all non-matching results

Rik :

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 Validators (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.

Holger :

There is no perfect solution that handles all cases with the same efficiency. Even your loop variant, which fulfills the criteria of being short-circuiting and processing the validators only once, has the disadvantage of creating and filling a collection that might turn out to be unnecessary if just one validation succeeds.

The choice depends on the actual costs associated with the operations and the likelihood of having at least one successful validation. If the common case gets handled with the best performance, it may outweigh the solution’s penalties on the handling of the uncommon case.

So

// you may use this if the likelihood of a success is high; assumes
// reasonable costs for the validation and consists (repeatable) results
public ValidationResult isValid(final Thing thing) {
    return validators.stream()
      .map(v -> v.isValid(thing))
      .filter(ValidationResult::isValid)
      .findFirst()
      .orElseGet(() -> new ValidationResult(false, "All validators failed"
        + validators.stream().map(v -> v.isValid(thing)).collect(Collectors.toSet())));
}
// you may use this if the likelihood of a success is
// very low and/or you intent to utilize parallel processing
public ValidationResult isValid(final Thing thing) {
    Map<Boolean,Set<ValidationResult>> results = validators.stream()
        .map(v -> v.isValid(thing))
        .collect(Collectors.partitioningBy(ValidationResult::isValid, Collectors.toSet()));
    return results.get(true).stream().findAny()
        .orElseGet(() -> new ValidationResult(false,
                             "No successful validator: "+results.get(false)));
}
// if chances of successful validation are mixed or unpredictable
// or validation is so expensive that everything else doesn't matter
// stay with the loop
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);
}

Consider sorting the list so that validators with a higher chance of success are at the beginning…

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=435151&siteId=1