Home » Java » Core Java » Fail-fast validations using Java 8 streams

About Steve Chaloner

Steve Chaloner

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.
(0 rating, 0 votes)
You need to be a registered member to rate this.
3 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

3
Leave a Reply

avatar
2 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
TkkJacob Zimmerman Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
Jacob Zimmerman
Guest

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 »

Tkk
Guest
Tkk

that awsome

Jacob Zimmerman
Guest

Also, your solution can’t be parallelized. Mine can.