com.github.thiagogarbazza.domainvalidation.ViolationContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of domain-validation Show documentation
Show all versions of domain-validation Show documentation
A simple framework to write domain validation.
package com.github.thiagogarbazza.domainvalidation;
import java.util.ArrayList;
import java.util.Collection;
import com.github.thiagogarbazza.domainvalidation.util.PropertieUtil;
import ch.lambdaj.function.argument.Argument;
public class ViolationContext implements Cloneable {
private Violations violations = new Violations();
public ViolationContext add(Violation violation) {
final String violationGlobalName = PropertieUtil.getValue("violation-global");
return add(violationGlobalName, violation);
}
public ViolationContext add(String field, Violation violation) {
mapInitializer(field);
get(field).add(violation);
return this;
}
private void mapInitializer(String field) {
if (!violations.containsKey(field)) {
violations.put(field, new ArrayList());
}
}
private Collection get(String field) {
return violations.get(field);
}
public ViolationContext add(Argument field, Violation violation) {
final String fieldName = getFieldName(field);
return add(fieldName, violation);
}
private String getFieldName(final Argument field) {
return field.getInkvokedPropertyName();
}
public void reset() {
violations.clear();
}
public void validate() {
if (!violations.isEmpty()) {
throw new ViolationException(violations);
}
}
}