All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.thiagogarbazza.domainvalidation.ViolationContext Maven / Gradle / Ivy

There is a newer version: 0.1.0
Show newest version
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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy