Class AbstractNumberValidator
- java.lang.Object
-
- org.apache.commons.validator.routines.AbstractFormatValidator
-
- org.apache.commons.validator.routines.AbstractNumberValidator
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
BigDecimalValidator,BigIntegerValidator,ByteValidator,DoubleValidator,FloatValidator,IntegerValidator,LongValidator,ShortValidator
public abstract class AbstractNumberValidator extends AbstractFormatValidator
Abstract class for Number Validation.
This is a base class for building Number Validators using format parsing.
- Since:
- Validator 1.3.0
- Version:
- $Revision: 1649191 $
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private booleanallowFractionsstatic intCURRENCY_FORMATCurrencyNumberFormattypeprivate intformatTypestatic intPERCENT_FORMATPercentNumberFormattypeprivate static longserialVersionUIDstatic intSTANDARD_FORMATStandardNumberFormattype
-
Constructor Summary
Constructors Constructor Description AbstractNumberValidator(boolean strict, int formatType, boolean allowFractions)Construct an instance with specified strict and decimal parameters.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected intdetermineScale(java.text.NumberFormat format)Returns the multiplier of theNumberFormat.protected java.text.FormatgetFormat(java.lang.String pattern, java.util.Locale locale)Returns aNumberFormatfor the specified pattern and/orLocale.protected java.text.FormatgetFormat(java.util.Locale locale)Returns aNumberFormatfor the specified Locale.intgetFormatType()Indicates the type ofNumberFormatcreated by this validator instance.booleanisAllowFractions()Indicates whether the number being validated is a decimal or integer.booleanisInRange(java.lang.Number value, java.lang.Number min, java.lang.Number max)Check if the value is within a specified range.booleanisValid(java.lang.String value, java.lang.String pattern, java.util.Locale locale)Validate using the specifiedLocale.booleanmaxValue(java.lang.Number value, java.lang.Number max)Check if the value is less than or equal to a maximum.booleanminValue(java.lang.Number value, java.lang.Number min)Check if the value is greater than or equal to a minimum.protected java.lang.Objectparse(java.lang.String value, java.lang.String pattern, java.util.Locale locale)Parse the value using the specified pattern.protected abstract java.lang.ObjectprocessParsedValue(java.lang.Object value, java.text.Format formatter)Process the parsed value, performing any further validation and type conversion required.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
STANDARD_FORMAT
public static final int STANDARD_FORMAT
StandardNumberFormattype- See Also:
- Constant Field Values
-
CURRENCY_FORMAT
public static final int CURRENCY_FORMAT
CurrencyNumberFormattype- See Also:
- Constant Field Values
-
PERCENT_FORMAT
public static final int PERCENT_FORMAT
PercentNumberFormattype- See Also:
- Constant Field Values
-
allowFractions
private final boolean allowFractions
-
formatType
private final int formatType
-
-
Constructor Detail
-
AbstractNumberValidator
public AbstractNumberValidator(boolean strict, int formatType, boolean allowFractions)Construct an instance with specified strict and decimal parameters.- Parameters:
strict-trueif strictFormatparsing should be used.formatType- TheNumberFormattype to create for validation, default is STANDARD_FORMAT.allowFractions-trueif fractions are allowed orfalseif integers only.
-
-
Method Detail
-
isAllowFractions
public boolean isAllowFractions()
Indicates whether the number being validated is a decimal or integer.
- Returns:
trueif decimals are allowed orfalseif the number is an integer.
-
getFormatType
public int getFormatType()
Indicates the type of
NumberFormatcreated by this validator instance.- Returns:
- the format type created.
-
isValid
public boolean isValid(java.lang.String value, java.lang.String pattern, java.util.Locale locale)Validate using the specified
Locale.- Specified by:
isValidin classAbstractFormatValidator- Parameters:
value- The value validation is being performed on.pattern- The pattern used to validate the value against, or the default for theLocaleifnull.locale- The locale to use for the date format, system default if null.- Returns:
trueif the value is valid.
-
isInRange
public boolean isInRange(java.lang.Number value, java.lang.Number min, java.lang.Number max)Check if the value is within a specified range.- Parameters:
value- The value validation is being performed on.min- The minimum value of the range.max- The maximum value of the range.- Returns:
trueif the value is within the specified range.
-
minValue
public boolean minValue(java.lang.Number value, java.lang.Number min)Check if the value is greater than or equal to a minimum.- Parameters:
value- The value validation is being performed on.min- The minimum value.- Returns:
trueif the value is greater than or equal to the minimum.
-
maxValue
public boolean maxValue(java.lang.Number value, java.lang.Number max)Check if the value is less than or equal to a maximum.- Parameters:
value- The value validation is being performed on.max- The maximum value.- Returns:
trueif the value is less than or equal to the maximum.
-
parse
protected java.lang.Object parse(java.lang.String value, java.lang.String pattern, java.util.Locale locale)Parse the value using the specified pattern.
- Parameters:
value- The value validation is being performed on.pattern- The pattern used to validate the value against, or the default for theLocaleifnull.locale- The locale to use for the date format, system default if null.- Returns:
- The parsed value if valid or
nullif invalid.
-
processParsedValue
protected abstract java.lang.Object processParsedValue(java.lang.Object value, java.text.Format formatter)Process the parsed value, performing any further validation and type conversion required.
- Specified by:
processParsedValuein classAbstractFormatValidator- Parameters:
value- The parsed object created.formatter- The Format used to parse the value with.- Returns:
- The parsed value converted to the appropriate type
if valid or
nullif invalid.
-
getFormat
protected java.text.Format getFormat(java.lang.String pattern, java.util.Locale locale)Returns a
NumberFormatfor the specified pattern and/orLocale.- Specified by:
getFormatin classAbstractFormatValidator- Parameters:
pattern- The pattern used to validate the value against ornullto use the default for theLocale.locale- The locale to use for the currency format, system default if null.- Returns:
- The
NumberFormatto created.
-
determineScale
protected int determineScale(java.text.NumberFormat format)
Returns the multiplier of the
NumberFormat.- Parameters:
format- TheNumberFormatto determine the multiplier of.- Returns:
- The multiplying factor for the format..
-
getFormat
protected java.text.Format getFormat(java.util.Locale locale)
Returns a
NumberFormatfor the specified Locale.- Parameters:
locale- The locale aNumberFormatis required for, system default if null.- Returns:
- The
NumberFormatto created.
-
-