public final class CodeValidator extends Object implements Serializable
CheckDigit
validations.
Performs the following validations on a code:
CheckDigit
validation on the parsed code (if specified).validate(String)
method returns the trimmed, parsed input (or null if validation failed)
Note
The isValid(String)
method will return true if the input passes validation.
Since this includes trimming as well as potentially dropping parts of the input,
it is possible for a String to pass validation
but fail the checkdigit test if passed directly to it (the check digit routines generally don't trim input
nor do they generally check the format/length).
To be sure that you are passing valid input to a method use validate(String)
as follows:
Object valid = validator.validate(input); if (valid != null) { some_method(valid.toString()); }
Configure the validator with the appropriate regular expression, minimum/maximum length
and CheckDigit
validator and then call one of the two validation
methods provided:
boolean isValid(code)
String validate(code)
Codes often include format characters - such as hyphens - to make them
more easily human readable. These can be removed prior to length and check digit
validation by specifying them as a non-capturing group in the regular
expression (i.e. use the (?: )
notation).
Or just avoid using parentheses except for the parts you want to capture
Constructor and Description |
---|
CodeValidator(RegexValidator regexValidator,
CheckDigit checkdigit)
Construct a code validator with a specified regular expression,
validator and
CheckDigit validation. |
CodeValidator(RegexValidator regexValidator,
int length,
CheckDigit checkdigit)
Construct a code validator with a specified regular expression,
validator, length and
CheckDigit validation. |
CodeValidator(RegexValidator regexValidator,
int minLength,
int maxLength,
CheckDigit checkdigit)
Construct a code validator with a specified regular expression
validator, minimum/maximum length and
CheckDigit validation. |
CodeValidator(String regex,
CheckDigit checkdigit)
Construct a code validator with a specified regular
expression and
CheckDigit . |
CodeValidator(String regex,
int length,
CheckDigit checkdigit)
Construct a code validator with a specified regular
expression, length and
CheckDigit . |
CodeValidator(String regex,
int minLength,
int maxLength,
CheckDigit checkdigit)
Construct a code validator with a specified regular
expression, minimum/maximum length and
CheckDigit validation. |
Modifier and Type | Method and Description |
---|---|
CheckDigit |
getCheckDigit()
Return the check digit validation routine.
|
int |
getMaxLength()
Return the maximum length of the code.
|
int |
getMinLength()
Return the minimum length of the code.
|
RegexValidator |
getRegexValidator()
Return the regular expression validator.
|
boolean |
isValid(String input)
Validate the code returning either
true
or false . |
Object |
validate(String input)
Validate the code returning either the valid code or
null if invalid. |
public CodeValidator(String regex, CheckDigit checkdigit)
CheckDigit
.
The RegexValidator validator is created to be case-sensitiveregex
- The format regular expressioncheckdigit
- The check digit validation routinepublic CodeValidator(String regex, int length, CheckDigit checkdigit)
CheckDigit
.
The RegexValidator validator is created to be case-sensitiveregex
- The format regular expression.length
- The length of the code
(sets the mimimum/maximum to the same)checkdigit
- The check digit validation routinepublic CodeValidator(String regex, int minLength, int maxLength, CheckDigit checkdigit)
CheckDigit
validation.
The RegexValidator validator is created to be case-sensitiveregex
- The regular expressionminLength
- The minimum length of the codemaxLength
- The maximum length of the codecheckdigit
- The check digit validation routinepublic CodeValidator(RegexValidator regexValidator, CheckDigit checkdigit)
CheckDigit
validation.regexValidator
- The format regular expression validatorcheckdigit
- The check digit validation routine.public CodeValidator(RegexValidator regexValidator, int length, CheckDigit checkdigit)
CheckDigit
validation.regexValidator
- The format regular expression validatorlength
- The length of the code
(sets the mimimum/maximum to the same value)checkdigit
- The check digit validation routinepublic CodeValidator(RegexValidator regexValidator, int minLength, int maxLength, CheckDigit checkdigit)
CheckDigit
validation.regexValidator
- The format regular expression validatorminLength
- The minimum length of the codemaxLength
- The maximum length of the codecheckdigit
- The check digit validation routinepublic CheckDigit getCheckDigit()
N.B. Optional, if not set no Check Digit validation will be performed on the code.
public int getMinLength()
N.B. Optional, if less than zero the minimum length will not be checked.
-1
if the code has no minimum lengthpublic int getMaxLength()
N.B. Optional, if less than zero the maximum length will not be checked.
-1
if the code has no maximum lengthpublic RegexValidator getRegexValidator()
N.B. Optional, if not set no regular expression validation will be performed on the code.
public boolean isValid(String input)
true
or false
.
This calls validate(String)
and returns false
if the return value is null, true otherwise.
Note that validate(String)
trims the input
and if there is a RegexValidator
it may also
change the input as part of the validation.
input
- The code to validatetrue
if valid, otherwise
false
public Object validate(String input)
null
if invalid.
Note that this method trims the input
and if there is a RegexValidator
it may also
change the input as part of the validation.
input
- The code to validatenull
if invalidCopyright © 2002–2020 The Apache Software Foundation. All rights reserved.