package hibergui.verify;

import java.util.LinkedHashMap;
import java.util.Map.Entry;

public class Verifier {

    LinkedHashMap<Constraint, Object[]> constraints = new LinkedHashMap<Constraint, Object[]>();
    int failureSeverity;
    String failureMessage;
    
    private boolean failed = false;
    
    public void addConstraint(Constraint c, int severity, String failureMessage) {
        constraints.put(c, new Object[]{severity, failureMessage});
    }
    
    public void removeConstraint(Constraint c) {
        constraints.remove(c);
    }
    
    public boolean verify(Object o) {
        for (Entry<Constraint, Object[]> e: constraints.entrySet())
            if (! e.getKey().check(o)) {
                Object[] failure = e.getValue();
                failureSeverity = (Integer) failure[0];
                failureMessage = (String) failure[1];
                failed = true;
                return false;
            }
        failed = false;
        return true;
    }

    public String getFailureMessage() {
        return failureMessage;
    }

    public int getFailureSeverity() {
        return failureSeverity;
    }
    
    public boolean hasFailed() {
        return failed;
    }

}
