/** * Factory method to create a validator, which if value exists in the provided collection. * @param values text of a message to be created if value is not found * @param severity severity of a message to be created if value is found * @return new validator */ public static <T> Validator<T> createEqualsValidator(final String message, final Severity severity, final Collection<T> values) { return (c, value) -> ValidationResult.fromMessageIf(c,message,severity, !values.contains(value)); }
/** * Factory method to create a validator, which evaluates the value validity with a given predicate. * Error is created if the evaluation is <code>false</code>. * @param message text of a message to be created if value is invalid * @param predicate the predicate to be used for the value validity evaluation. * @param severity severity of a message to be created if value is invalid * @return new validator */ static <T> Validator<T> createPredicateValidator(Predicate<T> predicate, String message, Severity severity) { return (control, value) -> ValidationResult.fromMessageIf( control, message, severity, predicate.test(value) == false); }
/** * Factory method to create a validator, which checks the value against a given regular expression. * Error is created if the value is <code>null</code> or the value does not match the pattern. * @param message text of a message to be created if value is invalid * @param regex the regular expression the value has to match * @param severity severity of a message to be created if value is invalid * @return new validator */ public static Validator<String> createRegexValidator(final String message, final String regex, final Severity severity) { return (c, value) -> { boolean condition = value == null ? true : !Pattern.matches(regex, value); return ValidationResult.fromMessageIf(c, message, severity, condition); }; }
/** * Factory method to create a validator, which checks if value exists. * @param message text of a message to be created if value is invalid * @param severity severity of a message to be created if value is invalid * @return new validator */ public static <T> Validator<T> createEmptyValidator(final String message, final Severity severity) { return (c, value) -> { boolean condition = value instanceof String ? value.toString().trim().isEmpty() : value == null; return ValidationResult.fromMessageIf(c, message, severity, condition); }; }
/** * Factory method to create a validator, which checks the value against a given regular expression. * Error is created if the value is <code>null</code> or the value does not match the pattern. * @param message text of a message to be created if value is invalid * @param regex the regular expression the value has to match * @param severity severity of a message to be created if value is invalid * @return new validator */ public static Validator<String> createRegexValidator(final String message, final Pattern regex, final Severity severity) { return (c, value) -> { boolean condition = value == null ? true : !regex.matcher(value).matches(); return ValidationResult.fromMessageIf(c, message, severity, condition); }; } }