Class Validator<T>

java.lang.Object
org.moeaframework.util.validate.Validator<T>
Type Parameters:
T - the value type
Direct Known Subclasses:
NumberValidator, ObjectValidator

public abstract class Validator<T> extends Object
Abstract class for validators. This class captures the name of the property, argument, or input along with its value. Subclasses are expected to define specific methods for validating the value based on its type.
  • Constructor Details

    • Validator

      public Validator(String propertyName, T propertyValue)
      Constructs a new validator.
      Parameters:
      propertyName - the property name
      propertyValue - the property value
  • Method Details

    • getPropertyName

      protected String getPropertyName()
      Returns the property name. This method is protected to simplify the API.
      Returns:
      the property name
    • getPropertyValue

      protected T getPropertyValue()
      Returns the property value. This method is protected to simplify the API.
      Returns:
      the property value
    • fails

      public <R> R fails(String message)
      Throws an IllegalArgumentException with the given message. This method does define a return type so callers can return the result to avoid "not all code paths return a value" compilation errors. However, this method always throws and never returns.
      Type Parameters:
      R - the return type, typically determined automatically by the type system
      Parameters:
      message - the message describing the validation failure
      Returns:
      this method will never return
      Throws:
      IllegalArgumentException - with the given message
    • failUnsupportedOption

      @SafeVarargs public final <S, R> R failUnsupportedOption(S... supportedValues)
      Always fails to indicate a user-provided value does not match the supported list of options. The argument is a list of supported values that are displayed in the error message. If none are provided, will attempt to determine the valid values from the type.
      Type Parameters:
      S - the type of supported values, which might differ from the property value if conversion is required
      R - the return type
      Parameters:
      supportedValues - a list of supported values
      Returns:
      this method never returns
    • failUnsupportedOption

      public final <R> R failUnsupportedOption(Class<? extends Enum<?>> enumType)
      Always fails to indicate a user-provided value does not match the supported list of enum constants. The property value does not necessarily need to be the enum type, as we often convert from strings.
      Type Parameters:
      R - the return type
      Parameters:
      enumType - the enum type
      Returns:
      this method never returns