Class Validator
- java.lang.Object
-
- net.sf.eBus.util.Validator
-
public class Validator extends Object
This class is used to collect invalid builder settings, combining the invalid field names with text explaining why the field is invalid. Helper methods are provided to perform the most common checks (not null, not null or empty string, and equals value). If the check fails, then the given field name and appropriate reason are added to the errors list.If validation requires more sophisticated validation,
requireTrue(Predicate, Object, String, String)andrequireTrue(BiPredicate, Object, Object, String, String, String)are provided. The user provides a predicate which tests either a single field or two fields in combination. Ifpredicate.testreturnsfalse, then the field or fields are marked as invalid. TheBiPredicatemethod may be used to test two fields which are mutually dependent. This means that setting one field to a given value limits the value which may be assigned to the other. If this bi-predicate fails, then both fields are in error.If the provided
requiresmethods are not sufficient (such as testing the mutual validity of three or more fields), then the user must use custom validation code and, if the validation fails, useaddError(MultiKey2)to enter the error into the validation error list.- Author:
- Charles W. Rapp
-
-
Field Summary
Fields Modifier and Type Field Description static intMESSAGE_INDEXThe message explaining why the field is invalid is stored in index 1.static intNAME_INDEXThe field name is stored in index 0.static StringNEWLINEOS-specific end-of-line string for quick reference.static StringNOT_SETStandard reason for a required message field not being set.
-
Constructor Summary
Constructors Constructor Description Validator()Creates a new validator instance containing no problems.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ValidatoraddError(MultiKey2<String,String> error)Adds the given error to the validator error list.List<MultiKey2<String,String>>errors()Returns an unmodifiable copy of the field errors list.booleanisEmpty()Returnstrueif there are no validation errors andfalseotherwise.ValidatorrequireEquals(Object value, Object expected, String fieldName)Check if the field value equals the expected value and, if not, adds the appropriate error against the field name.ValidatorrequireNotNull(Object value, String fieldName)Checks if the given value isnulland, if so, adds the appropriate error against the field name.ValidatorrequireNotNullOrEmpty(String value, String fieldName)Checks if the givenStringvalue is eithernullor empty and, if so, adds the appropriate error against the field name.ValidatorrequireTrue(boolean flag, String fieldName, String message)Ifflagisfalse, then an error is raised for the given field name and message.<V1,V2>
ValidatorrequireTrue(BiPredicate<V1,V2> predicate, V1 value1, V2 value2, String fieldName1, String fieldName2, String message)Ifpredicate.test(value)returnsfalse, then the given field name and message are added to the field errors list.<V> ValidatorrequireTrue(Predicate<V> predicate, V value, String fieldName, String message)Ifpredicate.test(value)returnsfalse, then the given field name and message are added to the field errors list.intsize()Returns error count.voidthrowException(Class<?> tc)Throws aValidationExceptionif this validator contains any problems.voidthrowException(Class<?> tc, Throwable cause)Throws aValidationExceptionif this validator contains any problems.StringtoString()Returns the error list as text which each error output as "<field name>: <message>" on a separate line.
-
-
-
Field Detail
-
NEWLINE
public static final String NEWLINE
OS-specific end-of-line string for quick reference.
-
NOT_SET
public static final String NOT_SET
Standard reason for a required message field not being set.- See Also:
- Constant Field Values
-
NAME_INDEX
public static final int NAME_INDEX
The field name is stored in index 0.- See Also:
- Constant Field Values
-
MESSAGE_INDEX
public static final int MESSAGE_INDEX
The message explaining why the field is invalid is stored in index 1.- See Also:
- Constant Field Values
-
-
Method Detail
-
toString
public String toString()
Returns the error list as text which each error output as "<field name>: <message>" on a separate line. If there are no errors, then returns an empty string.
-
isEmpty
public boolean isEmpty()
Returnstrueif there are no validation errors andfalseotherwise.- Returns:
trueif there are no validation errors.
-
size
public int size()
Returns error count.- Returns:
- error count.
-
errors
public List<MultiKey2<String,String>> errors()
Returns an unmodifiable copy of the field errors list.- Returns:
- field errors list.
-
addError
public Validator addError(MultiKey2<String,String> error)
Adds the given error to the validator error list. Iferrorisnull, then it is ignored and nothing is added to the errors list. This is done to allow this method to be used in aValidatormethod chain- Parameters:
error- invalid field error. Ignored ifnull.- Returns:
this Validatorinstance.
-
requireNotNull
public Validator requireNotNull(Object value, String fieldName)
Checks if the given value isnulland, if so, adds the appropriate error against the field name.- Parameters:
value- field value.fieldName- name of field containing value.- Returns:
this Validatorinstance.- See Also:
requireNotNullOrEmpty(String, String)
-
requireNotNullOrEmpty
public Validator requireNotNullOrEmpty(String value, String fieldName)
Checks if the givenStringvalue is eithernullor empty and, if so, adds the appropriate error against the field name.- Parameters:
value- string field value.fieldName- name of field containing value.- Returns:
this Validatorinstance.- See Also:
requireNotNull(Object, String)
-
requireEquals
public Validator requireEquals(Object value, Object expected, String fieldName)
Check if the field value equals the expected value and, if not, adds the appropriate error against the field name.- Parameters:
value- field value.expected- expected value.fieldName- name of field containing value.- Returns:
this Validatorinstance.
-
requireTrue
public <V> Validator requireTrue(Predicate<V> predicate, V value, String fieldName, String message)
Ifpredicate.test(value)returnsfalse, then the given field name and message are added to the field errors list. This method allows for more sophisticated test beyond check for anullvalue or equality.Please note that
predicate.test(value)should returntrueifvalueis acceptable. Do not write the predicate test method to check ifvalueis not valid.- Type Parameters:
V-valuetype.- Parameters:
predicate-testmethod checks ifvalueis acceptable.value- field value.fieldName- name of field containing value.message- error message ifpredicate.test(value)returnsfalse.- Returns:
this Validatorinstance.- See Also:
requireTrue(BiPredicate, Object, Object, String, String, String),requireTrue(boolean, String, String)
-
requireTrue
public <V1,V2> Validator requireTrue(BiPredicate<V1,V2> predicate, V1 value1, V2 value2, String fieldName1, String fieldName2, String message)
Ifpredicate.test(value)returnsfalse, then the given field name and message are added to the field errors list. This method allows for tests which require checking two fields against each other when their individual validity is dependent on the fields being mutually consistent. That is, setting the first field to a given value limits what value may be set to the second field.Please note that
predicate.test(value)should returntrueifvalueis acceptable. Do not write the predicate test method to check ifvalueis not valid.- Type Parameters:
V1-value1type.V2-value2type.- Parameters:
predicate-testmethod checks ifvalue1andvalue2are mutually acceptable.value1- first field's value.value2- second field's value.fieldName1- first field's name.fieldName2- second field's name.message- message applied to both fields ifpredicate.test(value1, value2)returnsfalse.- Returns:
this Validatorinstance.- See Also:
requireTrue(Predicate, Object, String, String),requireTrue(boolean, String, String)
-
requireTrue
public Validator requireTrue(boolean flag, String fieldName, String message)
Ifflagisfalse, then an error is raised for the given field name and message.- Parameters:
flag- set totrueif the field is valid andfalseis invalid.fieldName- validating this field name.message- message explaining invalidation.- Returns:
this Validatorinstance.- See Also:
requireTrue(Predicate, Object, String, String),requireTrue(BiPredicate, Object, Object, String, String, String)
-
throwException
public void throwException(Class<?> tc)
Throws aValidationExceptionif this validator contains any problems. Otherwise does nothing.- Parameters:
tc- validation exception applies to building this class.- Throws:
ValidationException- if this validator has any listed problems.
-
throwException
public void throwException(Class<?> tc, Throwable cause)
Throws aValidationExceptionif this validator contains any problems. Otherwise does nothing.- Parameters:
tc- validation exception applies to building this class.cause- underlying exception causing this validation exception.- Throws:
ValidationException- if this validator has any listed problems.
-
-