org.netbeans.validation.api.builtin
Enum Validators

java.lang.Object
  extended by java.lang.Enum<Validators>
      extended by org.netbeans.validation.api.builtin.Validators
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Validators>, Validator<java.lang.String>

public enum Validators
extends java.lang.Enum<Validators>
implements Validator<java.lang.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 Summary
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).
EMAIL_ADDRESS
          Factory for validators that validate standard internet email addresses (name + @ + valid hostname or ip).
FILE_MUST_BE_DIRECTORY
          Factory for validators that require that a string represent a file which is a directory, not a data file
FILE_MUST_BE_FILE
          Factory for validators that require that a string represent a file which is a file, not a directory
FILE_MUST_EXIST
          Factory for validators that require that a string represent a file which exists on disk
FILE_MUST_NOT_EXIST
          Validator which only passes non-existent file names
HOST_NAME
          Factory for validators that check the validity of an host name (may contain port info)
HOST_NAME_OR_IP_ADDRESS
          Factory for validators that check the validity of an IP address or host name (may contain port info)
IP_ADDRESS
          Factory for validators that check the validity of an IP address (may contain port info)
JAVA_PACKAGE_NAME
          Factory for validators that validate a java package name.
MAY_NOT_START_WITH_DIGIT
          Factory for validators that do not allow strings which start with a digit
NO_WHITESPACE
          Factory for validators that require that strings not contain whitespace
REQUIRE_JAVA_IDENTIFIER
          Factory for validators which require that strings not be a Java keyword
REQUIRE_NON_EMPTY_STRING
          Factory for validators which require non-zero length text.
REQUIRE_NON_NEGATIVE_NUMBER
          Factory for validators which require a non-negative number (may be floating point or int)
REQUIRE_VALID_FILENAME
          Factory for validators which require a legal filename
REQUIRE_VALID_INTEGER
          Factory for validators which require a valid integer
REQUIRE_VALID_NUMBER
          Factory for validators which require a valid number of some sort
URL_MUST_BE_VALID
          Factory for validators that require that a string represent a valid URL
VALID_HEXADECIMAL_NUMBER
          Factory for validators that require that strings be a valid hexadecimal number
 
Method Summary
static Validator<java.lang.String> disallowChars(char[] chars, boolean trim)
          Get a validator which does fails if any of the characters in the passed char array are contained in the evaluated text
static Validator<java.lang.String> encodableInCharset(java.lang.String charsetName)
          Get a validator which determines if the passed string can be encoded in the specified encoding.
static Validator<javax.swing.ComboBoxModel> forComboBox(boolean trim, Validator<java.lang.String>... validators)
          Get a validator for ComboBoxModels which logically OR's together all of the passed built-in validators.
 Validator<javax.swing.ComboBoxModel> forComboBoxModel(boolean trim)
          Get a validator which operates against ComboBoxModel objects.
 Validator<javax.swing.text.Document> forDocument(boolean trim)
          Get a validator which operates against Document objects.
static Validator<javax.swing.text.Document> forDocument(boolean trim, Validator<java.lang.String>... validators)
          Get a validator for documents which logically OR's together all of the passed built-in validators.
static Validator<java.lang.String> forFormat(java.text.Format fmt)
          Get a validator that uses a specific Format (e.g.
 Validator<java.lang.String> forString(boolean trim)
          Get a validator of strings.
<T> Validator<T>
forType(java.lang.Class<T> type, boolean trim)
          Get a validator for objects of the requested type.
static Validator<java.lang.String> maxLength(int length)
          Validator that enforces maximum input length
static Validator<java.lang.String> merge(boolean trim, Validator<java.lang.String>... validators)
          Get a validator for strings which logically OR's together all of the passed built-in validators.
static
<T> Validator<T>
merge(Validator<T>... validators)
          Logically OR together multiple validators which work against the same type.
static Validator<java.lang.String> minLength(int length)
          Validator that enforces minimum input length
static Validator<java.lang.String> numberRange(java.lang.Number min, java.lang.Number max)
          Get a validator that guarantees that a number is within a certain range (inclusive)
static Validator<javax.swing.ButtonModel[]> oneButtonMustBeSelected(java.lang.String msg)
          Get a validator for button models which fails if at least one of the button models is not selected.
static Validator<java.lang.String> regexp(java.lang.String regexp, java.lang.String message, boolean acceptPartialMatches)
          Get a validator which fails if the text to validate does not match a passed regular expression.
static Validator<java.lang.String> splitString(java.lang.String regexp, Validator<java.lang.String> other)
          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.
 Validator<java.lang.String> trim()
           
static Validator<java.lang.String> trimString(Validator<java.lang.String>... others)
          Wrap a String validator in one which first calls String.trim() on the strings that will be evaluated.
 boolean validate(Problems problems, java.lang.String compName, java.lang.String model)
          Validate the passed model.
static Validator<java.lang.String> validNumber(java.util.Locale l)
          Create a number validator that uses a specific locale.
static Validators valueOf(java.lang.String name)
          Returns the enum constant of this type with the specified name.
static Validators[] values()
          Returns an array containing the constants of this enum type, in the order they are declared.
 
Methods inherited from class java.lang.Enum
clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Enum Constant Detail

REQUIRE_NON_EMPTY_STRING

public static final Validators REQUIRE_NON_EMPTY_STRING
Factory for validators which require non-zero length text.


REQUIRE_VALID_FILENAME

public static final Validators REQUIRE_VALID_FILENAME
Factory for validators which require a legal filename


REQUIRE_VALID_INTEGER

public static final Validators REQUIRE_VALID_INTEGER
Factory for validators which require a valid integer


REQUIRE_NON_NEGATIVE_NUMBER

public static final Validators 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 Validators REQUIRE_VALID_NUMBER
Factory for validators which require a valid number of some sort


REQUIRE_JAVA_IDENTIFIER

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


VALID_HEXADECIMAL_NUMBER

public static final Validators VALID_HEXADECIMAL_NUMBER
Factory for validators that require that strings be a valid hexadecimal number


NO_WHITESPACE

public static final Validators NO_WHITESPACE
Factory for validators that require that strings not contain whitespace


FILE_MUST_EXIST

public static final Validators 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 Validators 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 Validators 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 Validators URL_MUST_BE_VALID
Factory for validators that require that a string represent a valid URL


IP_ADDRESS

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


HOST_NAME

public static final Validators 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 Validators 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 Validators MAY_NOT_START_WITH_DIGIT
Factory for validators that do not allow strings which start with a digit


EMAIL_ADDRESS

public static final Validators 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 Validators 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 Validators 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 Validators FILE_MUST_NOT_EXIST
Validator which only passes non-existent file names

Method Detail

values

public static Validators[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
for (Validators c : Validators.values())
    System.out.println(c);

Returns:
an array containing the constants of this enum type, in the order they are declared

valueOf

public static Validators valueOf(java.lang.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:
java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
java.lang.NullPointerException - if the argument is null

forString

public Validator<java.lang.String> forString(boolean trim)
Get a validator of strings.

Parameters:
trim - If true, String.trim() is called before passing the value to the actual validator
Returns:
A validator for strings

forType

public <T> Validator<T> forType(java.lang.Class<T> type,
                                boolean trim)
Get a validator for objects of the requested type. Note that depending on calls to

Type Parameters:
T - The type of object you need a validator for
Parameters:
type - The class of type T
trim - Whether or not the returned validator should trim strings before doing anything
Returns:
A validator
Throws:
java.lang.IllegalArgumentException - if no converter of objects of type type to strings has been registered

forDocument

public Validator<javax.swing.text.Document> forDocument(boolean trim)
Get a validator which operates against Document objects.

Parameters:
trim - If true, the text should be trimmed before validation
Returns:
A validator of this enum kind which operates on Documents

forComboBoxModel

public Validator<javax.swing.ComboBoxModel> forComboBoxModel(boolean trim)
Get a validator which operates against ComboBoxModel objects.

Parameters:
trim - If true, the text should be trimmed before validation
Returns:
A validator of this enum type which operates on ComboBoxModels.

disallowChars

public static Validator<java.lang.String> disallowChars(char[] chars,
                                                        boolean trim)
Get a validator which does fails if any of the characters in the passed char array are contained in the evaluated text

Parameters:
chars - The array of illegal characters
trim - If true, the text should be trimmed before validation
Returns:
A validator which will show an error if any of the passed characters are in the String it encounters

forDocument

public static Validator<javax.swing.text.Document> forDocument(boolean trim,
                                                               Validator<java.lang.String>... validators)
Get a validator for documents which logically OR's together all of the passed built-in validators.

Parameters:
trim - If true, the text should be trimmed before validation
validators - one or more Validators enum constants
Returns:
A compound validator for documents

merge

public static Validator<java.lang.String> merge(boolean trim,
                                                Validator<java.lang.String>... validators)
Get a validator for strings which logically OR's together all of the passed built-in validators.

Parameters:
trim - If true, the text should be trimmed before validation
validators - one or more Validators enum constants
Returns:
A compound validator for strings

forComboBox

public static Validator<javax.swing.ComboBoxModel> forComboBox(boolean trim,
                                                               Validator<java.lang.String>... validators)
Get a validator for ComboBoxModels which logically OR's together all of the passed built-in validators.

Parameters:
trim - If true, the text should be trimmed before validation
validators - one or more Validators enum constants
Returns:
A compound validator for combo box models

oneButtonMustBeSelected

public static Validator<javax.swing.ButtonModel[]> oneButtonMustBeSelected(java.lang.String msg)
Get a validator for button models which fails if at least one of the button models is not selected.

Parameters:
msg - The error message if nothing is selected
Returns:
A validator for an array of button models

trimString

public static Validator<java.lang.String> trimString(Validator<java.lang.String>... others)
Wrap a String validator in one which first calls String.trim() on the strings that will be evaluated.

Parameters:
others - Another validator of strings
Returns:
A validator which first trims the String being validated, then calls the passed one

splitString

public static Validator<java.lang.String> splitString(java.lang.String regexp,
                                                      Validator<java.lang.String> other)
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 pattern to use to split the string
other - the validator the returned one should delegate to to validate each component of the split string
Returns:
A validator which evaluates each of component of the split string using the passed Validator

regexp

public static Validator<java.lang.String> regexp(java.lang.String regexp,
                                                 java.lang.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<java.lang.String> validNumber(java.util.Locale l)
Create a number validator that uses a specific locale. For the default locale, use Validators.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<java.lang.String> forFormat(java.text.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<java.lang.String> encodableInCharset(java.lang.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<java.lang.String> numberRange(java.lang.Number min,
                                                      java.lang.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<java.lang.String> minLength(int length)
Validator that enforces minimum input length

Parameters:
length -
Returns:
A validator for string lengths

maxLength

public static Validator<java.lang.String> maxLength(int length)
Validator that enforces maximum input length

Parameters:
length -
Returns:
A validator for string lengths

merge

public static <T> Validator<T> merge(Validator<T>... validators)
Logically OR together multiple validators which work against the same type.

Type Parameters:
T - The type of model (Document, String, etc.) you want to work with
Parameters:
validators - Any number of validators which should be logically OR'd together, merged into a single validator
Returns:
a single validator which delegates to all of the passed ones

validate

public boolean validate(Problems problems,
                        java.lang.String compName,
                        java.lang.String model)
Description copied from interface: Validator
Validate the passed model. If the component is invalid, this method should add problems to the passed list.

Specified by:
validate in interface Validator<java.lang.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
Returns:
true if no problems were found during validation

trim

public Validator<java.lang.String> trim()