1 /*
2  * Hibernate Validator, declare and validate application constraints
3  *
4  * License: Apache License, Version 2.0
5  * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6  */

7 package org.hibernate.validator.internal.constraintvalidators.bv;
8
9 import javax.validation.ConstraintValidator;
10 import javax.validation.ConstraintValidatorContext;
11 import javax.validation.constraints.NotBlank;
12
13 /**
14  * Check that a character sequence is not {@code null} nor empty after removing any leading or trailing whitespace.
15  *
16  * @author Guillaume Smet
17  */

18 public class NotBlankValidator implements ConstraintValidator<NotBlank, CharSequence> {
19
20     /**
21      * Checks that the character sequence is not {@code null} nor empty after removing any leading or trailing
22      * whitespace.
23      *
24      * @param charSequence the character sequence to validate
25      * @param constraintValidatorContext context in which the constraint is evaluated
26      * @return returns {@code trueif the string is not {@code null} and the length of the trimmed
27      * {@code charSequence} is strictly superior to 0, {@code false} otherwise
28      */

29     @Override
30     public boolean isValid(CharSequence charSequence, ConstraintValidatorContext constraintValidatorContext) {
31         if ( charSequence == null ) {
32             return false;
33         }
34
35         return charSequence.toString().trim().length() > 0;
36     }
37 }
38