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

package org.dellroad.stuff.pobj;

import java.util.Set;

import javax.validation.ConstraintViolation;

/**
 * Runtime exception thrown during {@link PersistentObject} operations.
 */
@SuppressWarnings("serial")
public class PersistentObjectValidationException extends PersistentObjectException {

    private final Set<ConstraintViolation<?>> violations;

    /**
     * Constructor.
     *
     * @param violations set of violations
     * @throws IllegalArgumentException if {@code violations} is null
     */
    public PersistentObjectValidationException(Set<ConstraintViolation<?>> violations) {
        super("object failed to validate: " + violations);
        if (violations == null)
            throw new IllegalArgumentException("null violations");
        this.violations = violations;
    }

    /**
     * Get the set of constraint violations.
     */
    public Set<ConstraintViolation<?>> getViolations() {
        return this.violations;
    }
}



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

package org.dellroad.stuff.pobj;

import java.util.Set;

import javax.validation.ConstraintViolation;

/**
 * Runtime exception thrown during {@link PersistentObject} operations.
 */
@SuppressWarnings("serial")
public class PersistentObjectValidationException extends PersistentObjectException {

    private final Set<ConstraintViolation<?>> violations;

    /**
     * Constructor.
     *
     * @param violations set of violations
     * @throws IllegalArgumentException if {@code violations} is null
     */
    public PersistentObjectValidationException(Set<ConstraintViolation<?>> violations) {
        this.violations = violations;
    }

    /**
     * Get the set of constraint violations.
     */
    public Set<ConstraintViolation<?>> getViolations() {
        return this.violations;
    }

    private static String generateMessage(Set<ConstraintViolation<?>> violations) {
        if (violations == null)
            throw new IllegalArgumentException("null violations");
        StringBuilder buf = new StringBuilder("object failed to validate with " + violations.size() + " violation(s): ");
        boolean first = true;
        for (ConstraintViolation<?> violation : violations) {
            if (first)
                first = false;
            else
                buf.append("; ");
            buf.append("[" + violation.getPropertyPath() + "]: " + violation.getMessage());
        }
        return buf.toString();
    }
}


Improve exception message from previous commit.
Improve exception message from previous commit.
