|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sourceforge.argval.impl.ArgumentValidationImpl
public class ArgumentValidationImpl
The class for validating arguments of (public) methods and constructors (precondition checking).
For each argument that does not fulfill the required
conditions, a message is added. These messages are used to create an IllegalArgumentException
before the real work of the method / constructor should be started.
A simple example without pattern matching
First the incoming Strings are polished, which in fact means that they are trimmed (str.trim()) and when only a
empty string remains it's changed into null
.
There are several methods isValidXxxx
, which can be used to validate all kinds of conditions.
public class Address { private final static String PATTERN_DUTCH_POSTALCODE = "dutch_postalcode"; private static Map patternMap; private static Set countrySet; static { patternMap = new HashMap(); patternMap.put(PATTERN_DUTCH_POSTALCODE, "^[0-9]{4}[ ]?[A-Z]{2}$"); countrySet = new HashSet(); countrySet.add("The Netherlands"); } public Address(String street, Integer houseNumber, String postbox, String postalCode, String city, String country) { street = ArgumentValidation.polish(street); postalCode = ArgumentValidation.polish(postalCode); city = ArgumentValidation.polish(city); country = ArgumentValidation.polish(country); ArgumentValidation argVal = new ArgumentValidation(patternMap); if (argVal.isValidWhenNotNull("street", street)) { argVal.isValidWhenGreaterThen("houseNumber", houseNumber.intValue(), 0); if (postalCode != null) { argVal.addError("When argument 'street' is given, no value for argument 'postbox' is expected."); } } else if (argVal.isValidWhenNotNull("postbox", postbox)) { if (argVal.isValidWhenNotNull("street", street) || argVal.isValidWhenGreaterThen("houseNumber", houseNumber.intValue(), 0)) { argVal.addError("When argument 'postbox' is given, no values for argument 'street' and 'houseNumber' are expected."); } } argVal.isValidMatchingPattern("postalCode", postalCode, PATTERN_DUTCH_POSTALCODE); argVal.isValidWhenNotNull("city", city); argVal.isValidWhenInSet("country", country, countrySet); if (argVal.containsIllegalArgument()) throw argVal.createIllegalArgumentException(); } }
private final static String PATTERN_NUMBER = "number"; private final static String PATTERN_DUTCH_POSTALCODE = "dutch_postalcode"; private static Properties properties; // Inside the constructor (or static initialization) create a (static) Properties instance, or inject it! public Constructor() { // Creating a Properties instance, with two pattern names {'number', postalcode'}. // Add for each pattern name a regular expression. As property file it should look like: // argument_validation.patterns = number postalcode // argument_validation.number.regexp = ^[0-9]+$ // argument_validation.postalcode.regexp = ^[0-9]{4}[ ]?[A-Z]{2}$ properties = new Properties(); properties.setProperty(ConfigurationManager.PROP_ARG_VAL_PATTERNS, PATTERN_NUMBER + " " + PATTERN_DUTCH_POSTALCODE); properties.setProperty(PropertiesUtil.createKey( ConfigurationManager.PROP_ARG_VAL, PATTERN_NUMBER, ConfigurationManager.PROP_POSTFIX_REG_EXP), "^[0-9]+$"); properties.setProperty(PropertiesUtil.createKey( ConfigurationManager.PROP_ARG_VAL, PATTERN_DUTCH_POSTALCODE, ConfigurationManager.PROP_POSTFIX_REG_EXP), "^[0-9]{4}[ ]?[A-Z]{2}$"); } public void doSomething(String number, String postalcode) { postalcode = ArgumentValidation.polishToUpper(postalcode); ArgumentValidation argVal = new ArgumentValidation(properties); // Check the conditions by using regular expressions argVal.isValidMatchingPattern("number", number, PATTERN_NUMBER); argVal.isValidMatchingPattern("postalcode", postalcode, PATTERN_DUTCH_POSTALCODE); // Create an IllegalArgumentException if not all argument conditions are met. if (argVal.containsIllegalArgument()) throw argVal.createIllegalArgumentException(); // Doing the real work for this method ..... }
The patterns can also be added through a properties file. Each pattern must have a name, through which the
method isValidMatchingPattern(<arg as string>, <arg>, <pattern name>)
will
access the regular expression. Each pattern name has to be named under the key
argument_validation.patterns
. To assign each pattern name a regular expression, the key
combination of argument_validation.<pattern name>.regexp
is used.
argument_validation.patterns = number postalcode argument_validation.number.regexp = ^[0-9]+$ argument_validation.postalcode.regexp = ^[0-9]{4}[ ]?[A-Z]{2}$
Field Summary |
---|
Fields inherited from interface net.sourceforge.argval.ArgumentValidation |
---|
VALIDATION_TYPE_NAME_ARGUMENT, VALIDATION_TYPE_NAME_PROPERTY_KEY |
Constructor Summary | |
---|---|
ArgumentValidationImpl()
The default constructor. |
|
ArgumentValidationImpl(Map<String,String> patternMap)
Construct an ArgumentValidation instance, which can also validate
against regular expressions. |
|
ArgumentValidationImpl(Map<String,String> patternMap,
Map<String,String> dateFormatMap)
|
|
ArgumentValidationImpl(Map<String,String> patternMap,
Map<String,String> dateFormatMap,
String validationTypeName)
|
|
ArgumentValidationImpl(Map<String,String> patternMap,
org.apache.oro.text.regex.PatternCompiler compiler,
org.apache.oro.text.regex.PatternMatcher matcher)
Construct an ArgumentValidation instance, which can also validate
against regular expressions. |
|
ArgumentValidationImpl(Map<String,String> patternMap,
org.apache.oro.text.regex.PatternCompiler compiler,
org.apache.oro.text.regex.PatternMatcher matcher,
Map<String,String> dateFormatMap)
|
|
ArgumentValidationImpl(Map<String,String> patternMap,
org.apache.oro.text.regex.PatternCompiler compiler,
org.apache.oro.text.regex.PatternMatcher matcher,
Map<String,String> dateFormatMap,
String validationTypeName)
|
|
ArgumentValidationImpl(Map<String,String> patternMap,
org.apache.oro.text.regex.PatternCompiler compiler,
org.apache.oro.text.regex.PatternMatcher matcher,
String validationTypeName)
|
|
ArgumentValidationImpl(Map<String,String> patternMap,
String validationTypeName)
|
|
ArgumentValidationImpl(Properties properties,
org.apache.oro.text.regex.PatternCompiler compiler,
org.apache.oro.text.regex.PatternMatcher matcher)
TODO [2007.07.18 tv] update with date pattern Construct an ArgumentValidation instance, which can also validate
against regular expressions. |
|
ArgumentValidationImpl(String validationTypeName)
The default constructor. |
Method Summary | |
---|---|
void |
addError(String error)
Add an error message to the list of error messages. |
void |
addError(String argumentName,
String message)
Add an error message, for the given argumentName, to the list of error messages. |
void |
addError(String argumentName,
String key,
String message)
Add an error message, for when the argument is a Map instance, to the list of error messages, for the given key. |
boolean |
containsIllegalArgument()
Returns true if there are arguments checked (through one of the methods
isValidXxxxx ), that did not full fill the condition of that validation check,
otherwise false . |
IllegalArgumentException |
createIllegalArgumentException()
Creates an IllegalArgumentException with a detailed message about which
arguments did not satisfy the required conditions. |
String |
getMessage()
Creates a message about all the validated conditions which are not satisfied. |
Map<String,String> |
getPattern()
Returns the Map of pattern names and their regular expression. |
org.apache.oro.text.regex.Pattern |
getPattern(String patternName)
Returns the Pattern found under the patternName , or throws
an IllegalArgumentException if the patternName is not available. |
Set<String> |
getPatternNames()
Returns the Set of pattern names. |
String |
getRegularExpression(String patternName)
Returns the regular expression found under the patternName , or throws
an IllegalArgumentException if the patternName is not available. |
boolean |
isValidCollectionWhenMinElements(String argumentName,
Collection<?> argumentValue,
int minElements)
Validates if the Collection argumentValue is at least containing the
minimal number of elements (minElements ). |
boolean |
isValidCollectionWhenNoNulls(String argumentName,
Collection<?> argumentValue)
Validates if the Collection argumentValue is not null and that its
containing elements are also not null. |
boolean |
isValidMatchingDateFormat(String argumentName,
String argumentValue,
DateFormat dateFormat)
Validates if the String argumentValue matches the dateFormat . |
boolean |
isValidMatchingDateFormat(String argumentName,
String argumentValue,
String dateFormatName)
Validates if the String argumentValue matches the date format registered under the
dateFormatName . |
boolean |
isValidMatchingPattern(String argumentName,
String argumentValue,
String patternName)
Validates if the String argumentValue matches the pattern regisstered under the
patternName . |
boolean |
isValidNotNullForKey(String argumentName,
Map<String,String> argumentValueMap,
String key)
|
boolean |
isValidStringLength(String argumentName,
String argumentValue,
int length)
Validates if the String argumentValue has an exact length of length . |
boolean |
isValidStringMaxLength(String argumentName,
String argumentValue,
int maxLength)
Validates if the String argumentValue has a maximum length of maxLength . |
boolean |
isValidStringMinAndMaxLength(String argumentName,
String argumentValue,
int minLength,
int maxLength)
Validates if the String argumentValue has a minimal length of minLength and
a maximum length of maxLength . |
boolean |
isValidStringMinLength(String argumentName,
String argumentValue,
int minLength)
Validates if the String argumentValue has a minimal length of minLength . |
boolean |
isValidWhenBoolean(String argumentName,
String argumentValue)
Validates if the String argumentValue contains the text true or
false . |
boolean |
isValidWhenDirectory(String argumentName,
File argumentValue)
|
boolean |
isValidWhenDirectory(String argumentName,
String argumentValue)
Validates if the String argumentValue is a directory. |
boolean |
isValidWhenFile(String argumentName,
File argumentValue)
|
boolean |
isValidWhenFile(String argumentName,
String argumentValue)
Validates if the String argumentValue is a file. |
boolean |
isValidWhenGreaterThen(String argumentName,
int argumentValue,
int value)
Validates if the Integer argumentValue is greate then Integer value . |
boolean |
isValidWhenInSet(String argumentName,
String argumentValue,
Set<String> argumentSet)
Validates if the String argumentValue is an element contained by
the Set argumentSet . |
boolean |
isValidWhenInteger(String argumentName,
String argumentValue)
Validates if the String argumentValue is an Integer value. |
boolean |
isValidWhenLong(String argumentName,
String argumentValue)
Validates if the String argumentValue is a Long value. |
boolean |
isValidWhenNotInSet(String argumentName,
String argumentValue,
Set<String> argumentSet)
Validates if the String argumentValue is not already contained by
the Set argumentSet . |
boolean |
isValidWhenNotNull(String argumentName,
Object argumentValue)
Validates if the String argumentValue contains an instance reference
(is not null ). |
boolean |
isValidWhenNotNullInCollection(String argumentName,
Collection<?> argumentValue)
|
boolean |
isValidWhenUri(String argumentName,
String argumentValue)
Validates if the String argumentValue is an valid uri. |
boolean |
isValidWhenUrl(String argumentName,
String argumentValue)
Validates if the String argumentValue is an valid url. |
static String |
polish(String text)
Returns the incoming text, but stripes leading and trailing spaces and returns null
when the (remaining) text is an empty String instance. |
static String |
polishToLower(String text)
Returns the incoming text polished (see polish(String) and converted into lower case. |
static String |
polishToUpper(String text)
Returns the incoming text polished (see polish(String) and converted into upper case. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ArgumentValidationImpl()
public ArgumentValidationImpl(String validationTypeName)
public ArgumentValidationImpl(Map<String,String> patternMap)
ArgumentValidation
instance, which can also validate
against regular expressions. It will use the org.apache.oro.text.regex.Perl5Compiler
and org.apache.oro.text.regex.Perl5Matcher
. See for detailed information on
creating regular expressions the package description.
patternMap
- A Map
instance, containing pattern names and their regular expressions.public ArgumentValidationImpl(Map<String,String> patternMap, String validationTypeName)
public ArgumentValidationImpl(Map<String,String> patternMap, Map<String,String> dateFormatMap)
public ArgumentValidationImpl(Map<String,String> patternMap, Map<String,String> dateFormatMap, String validationTypeName)
public ArgumentValidationImpl(Map<String,String> patternMap, org.apache.oro.text.regex.PatternCompiler compiler, org.apache.oro.text.regex.PatternMatcher matcher)
ArgumentValidation
instance, which can also validate
against regular expressions. See for detailed information on
creating regular expressions the package description.
patternMap
- A Map
instance, containing pattern names and their regular expressions.compiler
- A org.apache.oro.text.regex.PatternCompiler
compiler.matcher
- A org.apache.oro.text.regex.PatternMatcher
matcher.public ArgumentValidationImpl(Map<String,String> patternMap, org.apache.oro.text.regex.PatternCompiler compiler, org.apache.oro.text.regex.PatternMatcher matcher, String validationTypeName)
public ArgumentValidationImpl(Map<String,String> patternMap, org.apache.oro.text.regex.PatternCompiler compiler, org.apache.oro.text.regex.PatternMatcher matcher, Map<String,String> dateFormatMap)
public ArgumentValidationImpl(Map<String,String> patternMap, org.apache.oro.text.regex.PatternCompiler compiler, org.apache.oro.text.regex.PatternMatcher matcher, Map<String,String> dateFormatMap, String validationTypeName)
patternMap
- compiler
- matcher
- dateFormatMap
- validationTypeName
- - The type of validation (default 'Argument'), but use this
for checking other type of things like 'Property-key's.public ArgumentValidationImpl(Properties properties, org.apache.oro.text.regex.PatternCompiler compiler, org.apache.oro.text.regex.PatternMatcher matcher)
ArgumentValidation
instance, which can also validate
against regular expressions. See for detailed information on
creating regular expressions the package description.
properties
- A properties instance, containing pattern names and their regular expressions.compiler
- A org.apache.oro.text.regex.PatternCompiler
compiler.matcher
- A org.apache.oro.text.regex.PatternMatcher
matcher.Method Detail |
---|
public String getMessage()
ArgumentValidation
getMessage
in interface ArgumentValidation
public boolean containsIllegalArgument()
ArgumentValidation
true
if there are arguments checked (through one of the methods
isValidXxxxx
), that did not full fill the condition of that validation check,
otherwise false
.
containsIllegalArgument
in interface ArgumentValidation
true
if not all validated conditions are satisfied, otherwise
false
.public IllegalArgumentException createIllegalArgumentException()
ArgumentValidation
IllegalArgumentException
with a detailed message about which
arguments did not satisfy the required conditions.
createIllegalArgumentException
in interface ArgumentValidation
IllegalArgumentException
ArgumentValidation.getMessage()
public boolean isValidWhenNotNull(String argumentName, Object argumentValue)
ArgumentValidation
argumentValue
contains an instance reference
(is not null
).
isValidWhenNotNull
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.public boolean isValidWhenNotNullInCollection(String argumentName, Collection<?> argumentValue)
isValidWhenNotNullInCollection
in interface ArgumentValidation
public void addError(String argumentName, String message)
addError
in interface ArgumentValidation
argumentName
- - the argument (or property-key) in which the problem is found.message
- - the message that describes the problem.public void addError(String argumentName, String key, String message)
ArgumentValidation
addError
in interface ArgumentValidation
argumentName
- - the argument Map (or property-key).key
- - the key under which the problem is found.message
- - the message that describes the problem.public boolean isValidWhenBoolean(String argumentName, String argumentValue)
ArgumentValidation
argumentValue
contains the text true
or
false
.
isValidWhenBoolean
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.public boolean isValidWhenInteger(String argumentName, String argumentValue)
ArgumentValidation
argumentValue
is an Integer value.
isValidWhenInteger
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.public boolean isValidWhenLong(String argumentName, String argumentValue)
ArgumentValidation
argumentValue
is a Long value.
isValidWhenLong
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.public boolean isValidWhenDirectory(String argumentName, String argumentValue)
ArgumentValidation
argumentValue
is a directory.
isValidWhenDirectory
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.public boolean isValidWhenDirectory(String argumentName, File argumentValue)
isValidWhenDirectory
in interface ArgumentValidation
public boolean isValidWhenFile(String argumentName, String argumentValue)
ArgumentValidation
argumentValue
is a file.
isValidWhenFile
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.public boolean isValidWhenFile(String argumentName, File argumentValue)
isValidWhenFile
in interface ArgumentValidation
public boolean isValidWhenUrl(String argumentName, String argumentValue)
ArgumentValidation
argumentValue
is an valid url.
isValidWhenUrl
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.public boolean isValidWhenUri(String argumentName, String argumentValue)
ArgumentValidation
argumentValue
is an valid uri.
isValidWhenUri
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.public boolean isValidWhenInSet(String argumentName, String argumentValue, Set<String> argumentSet)
ArgumentValidation
argumentValue
is an element contained by
the Set argumentSet
.
isValidWhenInSet
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.argumentSet
- The Set
to validate against.
true
if the condition is met, otherwise false
.public boolean isValidWhenNotInSet(String argumentName, String argumentValue, Set<String> argumentSet)
ArgumentValidation
argumentValue
is not already contained by
the Set argumentSet
.
isValidWhenNotInSet
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.argumentSet
- The Set
to validate against.
true
if the condition is met, otherwise false
.public boolean isValidCollectionWhenNoNulls(String argumentName, Collection<?> argumentValue)
ArgumentValidation
argumentValue
is not null and that its
containing elements are also not null. Meaning the Collection may be empty. And when not
empty, each entry should contain a instance.
isValidCollectionWhenNoNulls
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.public boolean isValidCollectionWhenMinElements(String argumentName, Collection<?> argumentValue, int minElements)
ArgumentValidation
argumentValue
is at least containing the
minimal number of elements (minElements
).
isValidCollectionWhenMinElements
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.minElements
- The minimal number of arguments, the collection should contain.
true
if the condition is met, otherwise false
.public boolean isValidWhenGreaterThen(String argumentName, int argumentValue, int value)
ArgumentValidation
argumentValue
is greate then Integer value
.
isValidWhenGreaterThen
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.value
- The value to validate against.
true
if the condition is met, otherwise false
.public boolean isValidStringMinLength(String argumentName, String argumentValue, int minLength)
ArgumentValidation
argumentValue
has a minimal length of minLength
.
isValidStringMinLength
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.minLength
- The length to validate against.
true
if the condition is met, otherwise false
.public boolean isValidStringMaxLength(String argumentName, String argumentValue, int maxLength)
ArgumentValidation
argumentValue
has a maximum length of maxLength
.
isValidStringMaxLength
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.maxLength
- The length to validate against.
true
if the condition is met, otherwise false
.public boolean isValidStringLength(String argumentName, String argumentValue, int length)
ArgumentValidation
argumentValue
has an exact length of length
.
isValidStringLength
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.length
- The length to validate against.
true
if the condition is met, otherwise false
.public boolean isValidStringMinAndMaxLength(String argumentName, String argumentValue, int minLength, int maxLength)
ArgumentValidation
argumentValue
has a minimal length of minLength
and
a maximum length of maxLength
.
isValidStringMinAndMaxLength
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.minLength
- The minimal length to validate against.maxLength
- The maximum length to validate against.
true
if the condition is met, otherwise false
.public boolean isValidMatchingDateFormat(String argumentName, String argumentValue, DateFormat dateFormat)
ArgumentValidation
argumentValue
matches the dateFormat
.
isValidMatchingDateFormat
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.dateFormat
- The date format which is used for matching.
true
if the condition is met, otherwise false
.public boolean isValidMatchingDateFormat(String argumentName, String argumentValue, String dateFormatName)
ArgumentValidation
argumentValue
matches the date format registered under the
dateFormatName
.
isValidMatchingDateFormat
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.
true
if the condition is met, otherwise false
.ArgumentValidation.isValidMatchingDateFormat(String, String, DateFormat)
public boolean isValidMatchingPattern(String argumentName, String argumentValue, String patternName)
ArgumentValidation
argumentValue
matches the pattern regisstered under the
patternName
.
isValidMatchingPattern
in interface ArgumentValidation
argumentName
- The name of the argument, which is validated.argumentValue
- The value of the argument, which is validated.patternName
- The name of the pattern, which is used for matching.
true
if the condition is met, otherwise false
.public void addError(String error)
ArgumentValidation
IllegalArgumentException
message.
addError
in interface ArgumentValidation
error
- The error message to add.ArgumentValidation.addError(String, String)
public Set<String> getPatternNames()
ArgumentValidation
Set
of pattern names.
getPatternNames
in interface ArgumentValidation
public Map<String,String> getPattern()
ArgumentValidation
Map
of pattern names and their regular expression.
getPattern
in interface ArgumentValidation
String
).public org.apache.oro.text.regex.Pattern getPattern(String patternName)
ArgumentValidation
Pattern
found under the patternName
, or throws
an IllegalArgumentException
if the patternName
is not available.
getPattern
in interface ArgumentValidation
patternName
- The name of the pattern.
Pattern
.public String getRegularExpression(String patternName)
ArgumentValidation
patternName
, or throws
an IllegalArgumentException
if the patternName
is not available.
getRegularExpression
in interface ArgumentValidation
patternName
- The name of the pattern.
public static String polish(String text)
null
when the (remaining) text is an empty String instance.
text
- The text to polish.
null
, when no characters are left.public static String polishToLower(String text)
polish(String)
and converted into lower case.
text
- The text to polish and convert.
polish(String)
public static String polishToUpper(String text)
polish(String)
and converted into upper case.
text
- The text to polish and convert.
polish(String)
public boolean isValidNotNullForKey(String argumentName, Map<String,String> argumentValueMap, String key)
isValidNotNullForKey
in interface ArgumentValidation
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |