Class Validators

java.lang.Object
cdc.validation.Validators

public class Validators extends Object
Registry of validators.
Author:
Damien Carbonne
  • Field Details

    • PRINTER

      public static final cdc.util.debug.Printable PRINTER
  • Method Details

    • register

      public static void register(Validator<?> validator, String name)
      Register a validator with a name.
      Parameters:
      validator - The validator.
      name - The name.
      Throws:
      IllegalArgumentException - When validator or name is null, or when a validator with that name is already registered.
    • getNames

      public static Set<String> getNames()
      Returns:
      A set of registered validators names.
    • hasValidator

      public static boolean hasValidator(String name)
      Returns true if a validator with a name exists.
      Parameters:
      name - The name.
      Returns:
      true if a validator named name exists.
    • getValidator

      public static Validator<?> getValidator(String name, cdc.util.lang.FailureReaction reaction)
      Returns the validator that has a name.
      Parameters:
      name - The name.
      reaction - The reaction to adopt if no matching validator is found.
      Returns:
      The validator named name or null or an exception, depending on reaction.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found and reaction is FailureReaction.FAIL.
    • getValidator

      public static Validator<?> getValidator(String name)
      Returns the validator that has a name.
      Parameters:
      name - The name.
      Returns:
      The validator named name.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found.
    • getValidator

      public static <T> Validator<? super T> getValidator(Class<T> valueClass, String name, cdc.util.lang.FailureReaction reaction)
      Returns the validator that has a name and a value type.
      Type Parameters:
      T - The validator value type.
      Parameters:
      valueClass - The validator value class.
      name - The name.
      reaction - The reaction to adopt if no matching validator is found.
      Returns:
      the validator named name or null or an exception, depending on reaction.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found and reaction is FailureReaction.FAIL.
      IllegalArgumentException - When a validator is found but its value type does not match valueClass.
    • getValidator

      public static <T> Validator<? super T> getValidator(Class<T> valueClass, String name)
      Returns the validator that has a name and a value type.
      Type Parameters:
      T - The validator value type.
      Parameters:
      valueClass - The validator value class.
      name - The name.
      Returns:
      the validator named name or null or an exception.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found.
      IllegalArgumentException - When a validator is found but its value type does not match valueClass.
    • getStringValidator

      public static Validator<? super String> getStringValidator(String name, cdc.util.lang.FailureReaction reaction)
    • getStringValidator

      public static Validator<? super String> getStringValidator(String name)
    • validate

      public static <T> void validate(Class<T> valueClass, String validatorName, cdc.util.lang.FailureReaction reaction, T value, String valueName, ValidationHandler handler)
      Validates a value with a validator.
      Type Parameters:
      T - The value type.
      Parameters:
      valueClass - The value class.
      validatorName - The validator name.
      reaction - The reaction to adopt if no matching validator is found.
      value - The value.
      valueName - The name that must be given to value in messages.
      handler - The validation messages handler.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found and reaction is FailureReaction.FAIL.
      IllegalArgumentException - When a validator is found but its value type does not match valueClass.
    • validate

      public static <T> void validate(Class<T> valueClass, String validatorName, cdc.util.lang.FailureReaction reaction, T value, String valueName, List<ValidationRecord> records)
      Validates a value with a validator and fills a list of validation records.
      Type Parameters:
      T - The value type.
      Parameters:
      valueClass - The value class.
      validatorName - The validator name.
      reaction - The reaction to adopt if no matching validator is found.
      value - The value.
      valueName - The name that must be given to value in messages.
      records - The list of validation records that must be filled.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found and reaction is FailureReaction.FAIL.
      IllegalArgumentException - When a validator is found but its value type does not match valueClass.
    • validate

      public static <T> List<ValidationRecord> validate(Class<T> valueClass, String validatorName, cdc.util.lang.FailureReaction reaction, T value, String valueName)
      Validates a value with a validator and returns a list of validation records.
      Type Parameters:
      T - The value type.
      Parameters:
      valueClass - The value class.
      validatorName - The validator name.
      reaction - The reaction to adopt if no matching validator is found.
      value - The value.
      valueName - The name that must be given to value in messages.
      Returns:
      A list of validation records, possibly empty.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found and reaction is FailureReaction.FAIL.
      IllegalArgumentException - When a validator is found but its value type does not match valueClass.
    • validateRaw

      public static void validateRaw(String validatorName, cdc.util.lang.FailureReaction reaction, Object value, String valueName, ValidationHandler handler)
      Validates a raw value with a validator.
      Parameters:
      validatorName - The validator name.
      reaction - The reaction to adopt if no matching validator is found.
      value - The value.
      valueName - The name that must be given to value in messages.
      handler - The validation messages handler.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found and reaction is FailureReaction.FAIL.
      ClassCastException - If value is not null and cannot be assigned to value type of the found validator.
    • validateRaw

      public static void validateRaw(String validatorName, cdc.util.lang.FailureReaction reaction, Object value, String valueName, List<ValidationRecord> records)
      Validates a raw value with a validator and fills a list of validation records.
      Parameters:
      validatorName - The validator name.
      reaction - The reaction to adopt if no matching validator is found.
      value - The value.
      valueName - The name that must be given to value in messages.
      records - The list of validation records that must be filled.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found and reaction is FailureReaction.FAIL.
      ClassCastException - If value is not null and cannot be assigned to value type of the found validator.
    • validatRaw

      public static List<ValidationRecord> validatRaw(String validatorName, cdc.util.lang.FailureReaction reaction, Object value, String valueName)
      Validates a raw value with a validator and returns a list of validation records.
      Parameters:
      validatorName - The validator name.
      reaction - The reaction to adopt if no matching validator is found.
      value - The value.
      valueName - The name that must be given to value in messages.
      Returns:
      A list of validation records, possibly empty.
      Throws:
      cdc.util.lang.NotFoundException - When no validator is found and reaction is FailureReaction.FAIL.
      IllegalArgumentException - When a validator is found but its value type does not match valueClass.