Class DefaultValidationResult

  • All Implemented Interfaces:
    ValidationResult

    public final class DefaultValidationResult
    extends Object
    implements ValidationResult

    A default implementation of the ValidationResult interface.

    Instances of this class store a collection of validation error messages. If no messages exist with a ValidationMessageLevel of ERROR, the validation result is considered to be valid. It may contain other messages which are only warning messages.

    Clients do not create instances directly through the constructor, but use the nested Builder class. Once a builder is created, an arbitrary number of validation error messages can be added. If all messages have been added, the build() method creates an immutable instance of DefaultValidationResult. This has the advantage that all fields of DefaultValidationResult can be made final. Therefore objects of this class are thread-safe (note that is not true for the Builder objects).

    There is also a constant representing a validation result for a successful validation. This field can be used in custom validator implementations rather than creating a new instance of this class.

    Version:
    $Id: DefaultValidationResult.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Field Detail

      • VALID

        public static final ValidationResult VALID
        Constant for a validation result object for a successful validation.
    • Method Detail

      • isValid

        public boolean isValid()
        Returns a flag whether the validation was successful. This implementation tests if error messages have been added to this object.
        Specified by:
        isValid in interface ValidationResult
        Returns:
        a flag if validation was successful
      • hasMessages

        public boolean hasMessages​(ValidationMessageLevel level)
        Returns a flag whether messages of the specified level are available. These flags are initialized at construction time, so this method is pretty efficient.
        Specified by:
        hasMessages in interface ValidationResult
        Parameters:
        level - the ValidationMessageLevel in question
        Returns:
        a flag whether messages with this level are available
      • equals

        public boolean equals​(Object obj)
        Compares this object to another one. Two validation result objects are considered equal if and only if they contain the same error messages.
        Overrides:
        equals in class Object
        Parameters:
        obj - the object to compare to
        Returns:
        a flag whether the objects are equal
      • hashCode

        public int hashCode()
        Returns a hash code for this object.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code
      • toString

        public String toString()
        Returns a string representation for this object. This string will also contain the string representations for all contained error messages.
        Overrides:
        toString in class Object
        Returns:
        a string for this object
      • merge

        public static ValidationResult merge​(ValidationResult vr1,
                                             ValidationResult vr2)
        Combines two validation result objects to a combined result. If one of the passed in result objects is invalid, the resulting object will also be invalid. It will contain the union of all error messages. A new ValidationResult object is created only if necessary: If one of the arguments is valid and has no warning messages, the other argument is returned. The same is true for null arguments. This method returns null only if both arguments are null.
        Parameters:
        vr1 - the first validation result object
        vr2 - the second validation result object
        Returns:
        a combined validation result
      • createValidationMessage

        public static ValidationMessage createValidationMessage​(TransformerContext context,
                                                                String key,
                                                                Object... params)
        Creates a ValidationMessage object for the specified error message. This is a convenience method that obtains the ValidationMessageHandler from the TransformerContext and obtains a ValidationMessage for the specified parameters.
        Parameters:
        context - the TransformerContext (must not be null)
        key - the key of the error message
        params - optional parameters for the error message
        Returns:
        a ValidationResult object with the specified message
        Throws:
        IllegalArgumentException - if the TransformerContext is null
      • createValidationErrorResult

        public static ValidationResult createValidationErrorResult​(TransformerContext context,
                                                                   String key,
                                                                   Object... params)
        Creates a ValidationResult object with the specified error message. This is a convenience method for the frequent case that a ValidationResult object with exactly one error message has to be created. It obtains the ValidationMessageHandler from the TransformerContext, obtains a ValidationMessage for the specified parameters, and creates a ValidationResult object with exactly this message.
        Parameters:
        context - the TransformerContext (must not be null)
        key - the key of the error message
        params - optional parameters for the error message
        Returns:
        a ValidationResult object with this error message
        Throws:
        IllegalArgumentException - if the TransformerContext is null