
/*
 * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
 *
 * $Id$
 */

package org.dellroad.stuff.validation;

import java.util.Set;

import javax.validation.ConstraintViolation;

/**
 * Validation utility methods.
 */
public final class ValidationUtil {

    private ValidationUtil() {
    }

    /**
     * Describe the validation errors in a friendly format.
     *
     * @param violations validation violations
     * @return description of the validation errors
     * @throws NullPointerException if {@code violations} is null
     */
    public static String describe(Set<? extends ConstraintViolation<?>> violations) {
        if (violations.isEmpty())
            return "  (no violations)";
        StringBuilder buf = new StringBuilder(violations.size() * 32);
        for (ConstraintViolation<?> violation : violations) {
            String violationPath = violation.getPropertyPath().toString();
            buf.append("  ");
            if (violationPath.length() > 0)
                buf.append("[").append(violationPath).append("]: ");
            buf.append(violation.getMessage()).append('\n');
        }
        return buf.toString();
    }
}


@@ -296,16 +296,360 @@
 %0A    %7D%0A%0A
+    /**%0A     * Validate the given object.%0A     *%0A     * %3Cp%3E%0A     * This method simply creates a %7B@link ValidationContext%7D with the given root and invokes %7B@link ValidationContext#validate()%7D.%0A     * %3C/p%3E%0A     */%0A    public static %3CT%3E Set%3CConstraintViolation%3CT%3E%3E validate(T obj) %7B%0A        return new ValidationContext%3CT%3E(obj).validate();%0A    %7D%0A%0A
     /**%0A

Add validate() convenience method.