org.srplib.validation.ValidationErrors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of srp-validation Show documentation
Show all versions of srp-validation Show documentation
Single Responsibility Principle (SRP) libraries collection
package org.srplib.validation;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* Validation error collector object.
*
* Is used by binding code to collect validation errors.
*
* Normally ValidationErrors is created by auto-binding code, but it can be created implicitly by throwing
* validation exception:
*
* throw new ValidationException("error message");
* throw new ValidationException(Validators.newError("Error message", formField));
*
*
* or explicitly:
*
* ValidationErrors validationErrors = new ValidationErrors();
* ...
* validationErrors.add(ValidationError.newError("Value should be positive.", ageField))
* ...
* if (validationErrorCollector.hasErrors()) {
* // take some action
* }
*
*
*
* @author Anton Pechinsky
*/
public class ValidationErrors {
/**
* Empty instance of ValidationErrors
*/
public static final ValidationErrors EMPTY = new ValidationErrors();
private List validationErrors = new LinkedList();
public ValidationErrors(List validationErrors) {
if (validationErrors == null) {
throw new IllegalArgumentException("Validation errors list is null");
}
this.validationErrors.addAll(validationErrors);
}
public ValidationErrors(ValidationError... errors) {
this(Arrays.asList(errors));
}
/**
* Clears collected validation errors.
*/
public void clear() {
validationErrors.clear();
}
/**
* Return all collected validation errors.
*
* @return List list of validation erros.
*/
public List getErrors() {
return Collections.unmodifiableList(validationErrors);
}
/**
* Tests if this collector contains at least one validation error.
*
* @return true if this collection has validation errors, false
otherwise.
*/
public boolean hasErrors() {
return !validationErrors.isEmpty();
}
/**
* Adds validation error to this object.
*
* @param validationError ValidationError to add.
*/
public void add(ValidationError... validationError) {
validationErrors.addAll(Arrays.asList(validationError));
}
/**
* Adds all validation errors to this object.
*
* @param validationErrors a List of ValidationError to add.
*/
public void add(List validationErrors) {
this.validationErrors.addAll(validationErrors);
}
/**
* Returns form fields with validation errors.
*
* @return Set of FormField
*/
public Set