/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package io.spine.validate;

import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import io.spine.string.Stringifiers;

import javax.annotation.Nullable;
import java.util.List;

import static com.google.common.base.Joiner.on;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.transform;
import static java.util.Collections.unmodifiableList;

/**
 * An exception, thrown if a {@code Message} does not pass the validation.
 *
 * @author Illia Shepilov
 * @author Alex Tymchenko
 */
public class ValidationException extends RuntimeException {

    private static final long serialVersionUID = 0L;

    private static final Function<ConstraintViolation, String> TO_STRING_FN = new ToStringFn();

    /**
     * List of the constraint violations, that were found during the validation.
     */
    private final List<ConstraintViolation> constraintViolations;

    public ValidationException(List<ConstraintViolation> constraintViolations) {
        super();
        this.constraintViolations = constraintViolations;
    }

    public List<ConstraintViolation> getConstraintViolations() {
        return unmodifiableList(constraintViolations);
    }

    @Override
    public String toString() {
        final ToStringHelper helper = MoreObjects.toStringHelper(this);

        final String violationContent = constraintViolations.isEmpty()
                ? "[]"
                : on(", ").join(transform(constraintViolations, TO_STRING_FN));

        return helper.add("constraintViolations", violationContent)
                     .toString();
    }

    /**
     * A function, transforming a {@linkplain ConstraintViolation constraint violation}
     * into a {@code String}.
     */
    private static final class ToStringFn implements Function<ConstraintViolation, String> {
        @Nullable
        @Override
        public String apply(@Nullable ConstraintViolation input) {
            checkNotNull(input);
            return Stringifiers.toString(input);
        }
    }
}

@@ -1178,16 +1178,64 @@
 Helper;%0A
+import com.google.common.collect.ImmutableList;%0A
 import i
@@ -1499,62 +1499,8 @@
 orm;
-%0Aimport static java.util.Collections.unmodifiableList;
 %0A%0A/*
@@ -2042,20 +2042,24 @@
 ception(
-List
+Iterable
 %3CConstra
@@ -2068,35 +2068,25 @@
 tViolation%3E 
-constraintV
+v
 iolations) %7B
@@ -2139,27 +2139,38 @@
 tions = 
-constraintV
+ImmutableList.copyOf(v
 iolation
@@ -2170,16 +2170,17 @@
 olations
+)
 ;%0A    %7D%0A
@@ -2176,24 +2176,198 @@
 ns);%0A    %7D%0A%0A
+    @SuppressWarnings(%7B%22AssignmentOrReturnOfFieldWithMutableType%22 /* returns immutable impl. */,%0A                       %22unused%22 /* part of public API of the exception. */%7D)%0A
     public L
@@ -2438,25 +2438,8 @@
 urn 
-unmodifiableList(
 cons
@@ -2446,33 +2446,32 @@
 traintViolations
-)
 ;%0A    %7D%0A%0A    @Ov

Add defensive copy