Class AbstractValidator<T>

    • Constructor Detail

      • AbstractValidator

        protected AbstractValidator​(String errorMessage)
        Constructs a validator with the given error message. The substring "{0}" is replaced by the value that failed validation.
        Parameters:
        errorMessage - the message to be included in a failed result, not null
    • Method Detail

      • getMessage

        protected String getMessage​(T value)
        Returns the error message for the given value.
        Parameters:
        value - an invalid value
        Returns:
        the formatted error message
      • toResult

        protected ValidationResult toResult​(T value,
                                            boolean isValid)
        A helper method for creating a Result from a value and a validity flag. If the flag is true, returns Result.ok, otherwise yields Result.error bearing the error message returned by getMessage(Object).

        For instance, the following apply method only accepts even numbers:

         @Override
         public Result<T> apply(Integer value) {
             return toResult(value, value % 2 == 0);
         }
         
        Parameters:
        value - the validated value
        isValid - whether the value is valid or not
        Returns:
        the validation result