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

io.dropwizard.validation.ConstraintViolations Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
package io.dropwizard.validation;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;

import javax.validation.ConstraintViolation;
import java.util.HashSet;
import java.util.Set;

public class ConstraintViolations {
    private ConstraintViolations() { /* singleton */ }

    public static  String format(ConstraintViolation v) {
        if (v.getConstraintDescriptor().getAnnotation() instanceof ValidationMethod) {
            return v.getMessage();
        } else {
            return String.format("%s %s", v.getPropertyPath(), v.getMessage());
        }
    }

    public static  ImmutableList format(Set> violations) {
        final Set errors = new HashSet<>();
        for (ConstraintViolation v : violations) {
            errors.add(format(v));
        }
        return ImmutableList.copyOf(Ordering.natural().sortedCopy(errors));
    }

    public static ImmutableList formatUntyped(Set> violations) {
        final Set errors = new HashSet<>();
        for (ConstraintViolation v : violations) {
            errors.add(format(v));
        }
        return ImmutableList.copyOf(Ordering.natural().sortedCopy(errors));
    }

    public static  ImmutableSet> copyOf(Set> violations) {
        final ImmutableSet.Builder> builder = ImmutableSet.builder();
        for (ConstraintViolation violation : violations) {
            builder.add(violation);
        }
        return builder.build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy