Fail-fast validations using Java 8 streams
I’ve lost count of the number of times I’ve seen code which fail-fast validates the state of something, using an approach like:
public class PersonValidator { public boolean validate(Person person) { boolean valid = person != null; if (valid) valid = person.givenName != null; if (valid) valid = person.familyName != null; if (valid) valid = person.age != null; if (valid) valid = person.gender != null; // ...and many more } }
It works, but it’s a brute force approach that’s filled with repetition due to the valid check. If your code style enforces braces for if statements (+1 for that), your method is also three times longer and growing every time a new check is added to the validator.
Using Java 8’s new stream API, we can improve this by taking the guard condition of if (valid)
and making a generic validator that handles the plumbing for you.
import java.util.LinkedList; import java.util.List; import java.util.function.Function; public class GenericValidator implements Function { private final List> validators = new LinkedList<>(); public GenericValidator(List> validators) { this.validators.addAll(validators); } @Override public Boolean apply(final T toValidate) { // a final array allows us to change the boolean value within a lambda final boolean[] guard = {true}; return validators.stream() // only send the validator downstream if // previous validations were successful .filter(validator -> guard[0]) .map(validator -> validator.apply(toValidate)) // update the guard condition .map(result -> { guard[0] = result; return result; }) // Logically AND the results of the applied validators .reduce(guard[0], (b1, b2) -> b1 && b2); } }
Using this, we can rewrite the Person validator to be a specification of the required validations.
public class PersonValidator extends GenericValidator { private static final List> VALIDATORS = new LinkedList<>(); static { VALIDATORS.add(person -> person.givenName != null); VALIDATORS.add(person -> person.familyName != null); VALIDATORS.add(person -> person.age != null); VALIDATORS.add(person -> person.gender != null); // ...and many more } public PersonValidator() { super(VALIDATORS); } }
PersonValidator
, and all your other validators, can now focus completely on validation. The behaviour hasn’t changed – the validation still fails fast. There’s no boiler plate, which is A Good Thing.
This one’s going in the toolbox.
Reference: | Fail-fast validations using Java 8 streams from our JCG partner Steve Chaloner at the Objectify blog. |
Holy cow, this needs some work. First off, if the all the conditions are going to be hard-coded, the “old-fashioned” validator can simply be rewritten as return person != null && person.givenName != null && person.familyName != null && person.age != null && person.gender != null && // …and many more since the boolean operators already fail fast If you’re trying to make it so you can add or remove individual checks during runtime, then your class is worthwhile, but still poorly implemented. Here are a few reasons why: 1) Your validator implements Function. Since you plan to return a… Read more »
that awsome
Also, your solution can’t be parallelized. Mine can.