Class ViolationCollector


  • public class ViolationCollector
    extends Object
    This class is a simple wrapper around the ConstraintValidatorContext of hibernate validation. It collects all the violations of the SelfValidation methods of an object.
    • Constructor Detail

      • ViolationCollector

        public ViolationCollector​(javax.validation.ConstraintValidatorContext constraintValidatorContext)
    • Method Detail

      • addViolation

        public void addViolation​(String message)
        Adds a new violation to this collector. This also sets violationOccurred to true.

        Prefer the method with explicit message parameters if you want to interpolate the message.

        Parameters:
        message - the message of the violation
        See Also:
        addViolation(String, Map)
      • addViolation

        public void addViolation​(String message,
                                 Map<String,​Object> messageParameters)
        Adds a new violation to this collector. This also sets violationOccurred to true.
        Parameters:
        message - the message of the violation
        messageParameters - a map of message parameters which can be interpolated in the violation message
        Since:
        2.0.3
      • addViolation

        public void addViolation​(String propertyName,
                                 String message)
        Adds a new violation to this collector. This also sets violationOccurred to true.

        Prefer the method with explicit message parameters if you want to interpolate the message.

        Parameters:
        propertyName - the name of the property
        message - the message of the violation
        Since:
        2.0.2
        See Also:
        addViolation(String, String, Map)
      • addViolation

        public void addViolation​(String propertyName,
                                 String message,
                                 Map<String,​Object> messageParameters)
        Adds a new violation to this collector. This also sets violationOccurred to true.
        Parameters:
        propertyName - the name of the property
        message - the message of the violation
        messageParameters - a map of message parameters which can be interpolated in the violation message
        Since:
        2.0.3
      • addViolation

        public void addViolation​(String propertyName,
                                 Integer index,
                                 String message)
        Adds a new violation to this collector. This also sets violationOccurred to true. Prefer the method with explicit message parameters if you want to interpolate the message.
        Parameters:
        propertyName - the name of the property with the violation
        index - the index of the element with the violation
        message - the message of the violation (any EL expression will be escaped and not parsed)
        Since:
        2.0.2
        See Also:
        addViolation(String, Integer, String, Map)
      • addViolation

        public void addViolation​(String propertyName,
                                 Integer index,
                                 String message,
                                 Map<String,​Object> messageParameters)
        Adds a new violation to this collector. This also sets violationOccurred to true.
        Parameters:
        propertyName - the name of the property with the violation
        index - the index of the element with the violation
        message - the message of the violation
        messageParameters - a map of message parameters which can be interpolated in the violation message
        Since:
        2.0.3
      • addViolation

        public void addViolation​(String propertyName,
                                 String key,
                                 String message)
        Adds a new violation to this collector. This also sets violationOccurred to true.
        Parameters:
        propertyName - the name of the property with the violation
        key - the key of the element with the violation
        message - the message of the violation
        Since:
        2.0.2
      • addViolation

        public void addViolation​(String propertyName,
                                 String key,
                                 String message,
                                 Map<String,​Object> messageParameters)
        Adds a new violation to this collector. This also sets violationOccurred to true.
        Parameters:
        propertyName - the name of the property with the violation
        key - the key of the element with the violation
        message - the message of the violation
        messageParameters - a map of message parameters which can be interpolated in the violation message
        Since:
        2.0.3
      • getContext

        public javax.validation.ConstraintValidatorContext getContext()
        This method returns the wrapped context for raw access to the validation framework. If you use the context to add violations make sure to call setViolationOccurred(true).
        Returns:
        the wrapped Hibernate ConstraintValidatorContext
      • hasViolationOccurred

        public boolean hasViolationOccurred()
        Returns:
        if any violation was collected
      • setViolationOccurred

        public void setViolationOccurred​(boolean violationOccurred)
        Manually sets if a violation occurred. This is automatically set if addViolation is called.
        Parameters:
        violationOccurred - if any violation was collected