Enum StringValidators

java.lang.Object
java.lang.Enum<StringValidators>
org.netbeans.validation.api.builtin.stringvalidation.StringValidators
All Implemented Interfaces:
Serializable, Comparable<StringValidators>, java.lang.constant.Constable, Validator<String>

public enum StringValidators extends Enum<StringValidators> implements Validator<String>
An enumeration of validator factories for commonly needed forms of validation such as non-empty strings, valid file names and URLs and so forth.

Also contains static factory methods for validators which do things like match regexp's and split strings and run another validator over the components.

Author:
Tim Boudreau
  • Enum Constant Details

    • REQUIRE_NON_EMPTY_STRING

      public static final StringValidators REQUIRE_NON_EMPTY_STRING
      Factory for validators which require non-zero length text.
    • REQUIRE_VALID_FILENAME

      public static final StringValidators REQUIRE_VALID_FILENAME
      Factory for validators which require a legal filename
    • REQUIRE_VALID_INTEGER

      public static final StringValidators REQUIRE_VALID_INTEGER
      Factory for validators which require a valid integer
    • REQUIRE_NON_NEGATIVE_NUMBER

      public static final StringValidators REQUIRE_NON_NEGATIVE_NUMBER
      Factory for validators which require a non-negative number (may be floating point or int)
    • REQUIRE_VALID_NUMBER

      public static final StringValidators REQUIRE_VALID_NUMBER
      Factory for validators which require a valid number of some sort
    • REQUIRE_JAVA_IDENTIFIER

      public static final StringValidators REQUIRE_JAVA_IDENTIFIER
      Factory for validators which require that strings not be a Java keyword
    • VALID_HEXADECIMAL_NUMBER

      public static final StringValidators VALID_HEXADECIMAL_NUMBER
      Factory for validators that require that strings be a valid hexadecimal number
    • NO_WHITESPACE

      public static final StringValidators NO_WHITESPACE
      Factory for validators that require that strings not contain whitespace
    • FILE_MUST_EXIST

      public static final StringValidators FILE_MUST_EXIST
      Factory for validators that require that a string represent a file which exists on disk
    • FILE_MUST_BE_FILE

      public static final StringValidators FILE_MUST_BE_FILE
      Factory for validators that require that a string represent a file which is a file, not a directory
    • FILE_MUST_BE_DIRECTORY

      public static final StringValidators FILE_MUST_BE_DIRECTORY
      Factory for validators that require that a string represent a file which is a directory, not a data file
    • URL_MUST_BE_VALID

      public static final StringValidators URL_MUST_BE_VALID
      Factory for validators that require that a string represent a valid URL
    • IP_ADDRESS

      public static final StringValidators IP_ADDRESS
      Factory for validators that check the validity of an IP address (may contain port info)
    • HOST_NAME

      public static final StringValidators HOST_NAME
      Factory for validators that check the validity of an host name (may contain port info)
    • HOST_NAME_OR_IP_ADDRESS

      public static final StringValidators HOST_NAME_OR_IP_ADDRESS
      Factory for validators that check the validity of an IP address or host name (may contain port info)
    • MAY_NOT_START_WITH_DIGIT

      public static final StringValidators MAY_NOT_START_WITH_DIGIT
      Factory for validators that do not allow strings which start with a digit
    • EMAIL_ADDRESS

      public static final StringValidators EMAIL_ADDRESS
      Factory for validators that validate standard internet email addresses (name + @ + valid hostname or ip).

      Note: This validator is not useful for all legal email addresses - for example, "root" with is a legal email address on a Unix machine. Do not use this validator where users might legitimately expect to be able to enter such unqualified email addresses.

    • CHARACTER_SET_NAME

      public static final StringValidators CHARACTER_SET_NAME
      Factory for validators that require the passed string to be a valid character set name according to the specification of java.nio.Charset.forName(String).
    • JAVA_PACKAGE_NAME

      public static final StringValidators JAVA_PACKAGE_NAME
      Factory for validators that validate a java package name. Note that this does not mean the package name actually exists anywhere, just that it does not contain java keywords.
    • FILE_MUST_NOT_EXIST

      public static final StringValidators FILE_MUST_NOT_EXIST
      Validator which only passes non-existent file names
    • MAY_NOT_END_WITH_PERIOD

      public static final StringValidators MAY_NOT_END_WITH_PERIOD
      Validator which fails any string that ends with a .
  • Method Details

    • values

      public static StringValidators[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      Returns:
      an array containing the constants of this enum type, in the order they are declared
    • valueOf

      public static StringValidators valueOf(String name)
      Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum type has no constant with the specified name
      NullPointerException - if the argument is null
    • validate

      public void validate(Problems problems, String compName, String model)
      Description copied from interface: Validator
      Validate the passed model. If the component is invalid, this method shall add problems to the passed list.
      Specified by:
      validate in interface Validator<String>
      Parameters:
      problems - A list of problems.
      compName - The name of the component in question (may be null in some cases)
      model - The model in question
    • trim

      public Validator<String> trim()
      Returns a Validator that will first call trim() on the String to be validated, and then passes the resulting (trimmed) String to this instance of StringValidators.

      See also the static trimString(org.netbeans.validation.api.Validator[]) that does a similar thing for any Validator<String> (or a chain thereof) that need not be an instance of the StringValidators enum

    • trimString

      @SafeVarargs public static Validator<String> trimString(Validator<String>... validators)
      Creates a Validator<String> that will first call trim() on the String to be validated, and then passes the resulting (trimmed) String to the passed Validator<String> (or chain thereof).

      See also ValidatorUtils.merge(org.netbeans.validation.api.Validator[]) which merges validators (any Validator, not just Validator<String>) without wrapping the result in one that does String trimming.

      Parameters:
      validators - a chain of String validators
    • trimString

      public static Validator<String> trimString(Validator<String> validator)
      Creates a Validator<String> that will first call trim() on the String to be validated, and then passes the resulting (trimmed) String to the passed Validator<String>.

      Unlike trimString(Validator...), calling this method does not trigger warnings under -Xlint:unchecked. If you wish to trim more than one validator, simply trim the result of ValidatorUtils.merge(Validator,Validator).

      Parameters:
      validator - a String validator
    • splitString

      @SafeVarargs public static Validator<String> splitString(String regexp, Validator<String>... validators)
      Returns a validator which first splits the string to be evaluated according to the passed regexp, then passes each component of the split string to the passed validator.
      Parameters:
      regexp - The regular expression pattern to use to split the string
      validators - the validator (or chain of validators) that the returned one should delegate to validate each component of the split string
      Returns:
      A validator which evaluates each of component of the split string using the passed Validator (or chain of validators)
    • splitString

      public static Validator<String> splitString(String regexp, Validator<String> validator)
      Returns a validator which first splits the string to be evaluated according to the passed regexp, then passes each component of the split string to the passed validator.

      Unlike splitString(String,Validator...), calling this method does not trigger warnings under -Xlint:unchecked. If you wish to split more than one validator, simply split the result of ValidatorUtils.merge(Validator,Validator).

      Parameters:
      regexp - The regular expression pattern to use to split the string
      validator - the validator that the returned one should delegate to validate each component of the split string
      Returns:
      A validator which evaluates each of component of the split string using the passed Validator
    • mayNotEndWith

      public static Validator<String> mayNotEndWith(char ch)
      Create a validator which does not allow you to terminate a string with a particular character
      Parameters:
      ch - The character
      Returns:
      A validator
    • disallowChars

      public static Validator<String> disallowChars(char[] chars)
      Get a validator which fails if any of the characters in the passed char array are contained in the evaluated text
      Parameters:
      chars - The array of illegal characters
      Returns:
      A validator which will show an error if any of the passed characters are in the String it encounters
    • regexp

      public static Validator<String> regexp(String regexp, String message, boolean acceptPartialMatches)
      Get a validator which fails if the text to validate does not match a passed regular expression.
      Parameters:
      regexp - The regular expression
      message - The output message if there is a problem. The message may refer to the component name as {0} and the text that has not matched as {1} if desired
      acceptPartialMatches - if true, will use Matcher.lookingAt() rather than Matcher.matches()
      Returns:
      A validator
    • validNumber

      public static Validator<String> validNumber(Locale l)
      Create a number validator that uses a specific locale. For the default locale, use StringValidators.REQUIRE_VALID_NUMBER. Use this if you specifically want validation for another locale.
      Parameters:
      l - The locale to use
      Returns:
      A string validator for numbers
    • forFormat

      public static Validator<String> forFormat(Format fmt)
      Get a validator that uses a specific Format (e.g. NumberFormat) instance and fails if fmt.parseObject() throws a ParseException
      Parameters:
      fmt - A java.text.Format
      Returns:
      A string validator that uses the provided NumberFormat
    • encodableInCharset

      public static Validator<String> encodableInCharset(String charsetName)
      Get a validator which determines if the passed string can be encoded in the specified encoding. Useful, for example, for validating strings that are specified to be in a particular encoding (e.g. email addresses and US-ASCII)
      Parameters:
      charsetName - The name of a character set recognized by java.nio.Charset
      Returns:
      A validator of character set names
      Throws:
      UnsupportedCharsetException - if the character set is unsupported
      IllegalCharsetNameException - if the character set is illegal
    • numberRange

      public static Validator<String> numberRange(Number min, Number max)
      Get a validator that guarantees that a number is within a certain range (inclusive)
      Parameters:
      min - The minimum value
      max - The maximum value
      Returns:
      A validator for number ranges
    • minLength

      public static Validator<String> minLength(int length)
      Validator that enforces minimum input length
      Parameters:
      length -
      Returns:
      A validator for string lengths
    • maxLength

      public static Validator<String> maxLength(int length)
      Validator that enforces maximum input length
      Parameters:
      length -
      Returns:
      A validator for string lengths
    • greaterThan

      public static Validator<String> greaterThan(int amount)
      Validate that a string evaluates to a number greater than an amount
      Parameters:
      amount - The amount
      Returns:
      A validator
    • lessThan

      public static Validator<String> lessThan(int amount)
      Validate that a string evaluates to a number less than an amount
      Parameters:
      amount - The amount
      Returns:
      A validator
    • between

      public static Validator<String> between(int min, int max)
    • modelType

      public Class<String> modelType()
      Description copied from interface: Validator
      The type of the model object which can be validated. Necessary due to limitations of the Java implementation of generics, so that model conversions can be done at runtime, and declaratively registered ValidationListeners can be matched with Validator types.

      The return value of this method is expected to remain constant throughout the life of this validator.

      Specified by:
      modelType in interface Validator<String>
      Returns:
      The type of the model object expected. Note that a validator may be passed a subclass of this type.