Interface Validator<T>

Type Parameters:
T - the type of the value being validated
All Known Implementing Classes:
Logging, Throwing
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Validator<T>
A functional interface representing a strategy for handling invalid values detected during validation.

Implementations may choose to throw an exception, log a message, or perform some other side-effect based on the invalid value.

Used in conjunction with ValidationUtility.validate(Object, Predicate, Validator).

  • Field Details

  • Method Details

    • validate

      static <T> Optional<T> validate(T value, Predicate<T> predicate, Validator<T> handler)
      Validates a value using the given predicate and handler.

      If the predicate returns true, the handler is invoked to process the invalid value (e.g., throw or log). Otherwise, the valid value is returned as an Optional.

      Type Parameters:
      T - the type of the value
      Parameters:
      value - the value to validate
      predicate - the condition indicating invalidity
      handler - the strategy for handling invalid values
      Returns:
      an Optional containing the valid value, or empty if handled
    • formatMessage

      static String formatMessage(ErrorMessageKey errorMessageKey, Parameter parameter)
      Formats a localized error message using the specified key and parameter.

      Message templates are retrieved from the exception.messages resource bundle, and parameter placeholders are replaced using MessageFormat.

      Parameters:
      errorMessageKey - the structured error key
      parameter - the parameter involved in the validation
      Returns:
      the formatted, localized message
    • handleInvalid

      Optional<T> handleInvalid(T value)
      Handles an invalid value detected during validation.
      Parameters:
      value - the invalid value
      Returns:
      an empty optional if handled, or a fallback value (if applicable)
    • throwing

      static <T> Validator<T> throwing(ErrorMessageKey messageKey, Parameter parameter)
      Returns a handler that throws a ValidationException using the given error message key and parameter.
      Type Parameters:
      T - the type of the value
      Parameters:
      messageKey - the structured message key
      parameter - the parameter that failed validation
      Returns:
      a throwing validation handler
    • logging

      static <T> Validator<T> logging(LogLevel level, ErrorMessageKey messageKey, Parameter parameter)
      Returns a handler that logs a validation warning or error using the specified log level, message key, and parameter.
      Type Parameters:
      T - the type of the value
      Parameters:
      level - the severity level to log
      messageKey - the structured message key
      parameter - the parameter that failed validation
      Returns:
      a logging validation handler