Class AbstractNumberValidator
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
BigDecimalValidator, BigIntegerValidator, ByteValidator, DoubleValidator, FloatValidator, IntegerValidator, LongValidator, ShortValidator
This is a base class for building Number Validators using format parsing.
- Since:
- 1.3.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intCurrencyNumberFormattype.static final intPercentNumberFormattype.static final intStandardNumberFormattype. -
Constructor Summary
ConstructorsConstructorDescriptionAbstractNumberValidator(boolean strict, int formatType, boolean allowFractions) Constructs an instance with specified strict and decimal parameters. -
Method Summary
Modifier and TypeMethodDescriptionprotected intdetermineScale(NumberFormat format) Returns the multiplier of theNumberFormat.protected FormatGets aNumberFormatfor the specified pattern and/orLocale.protected FormatGets theNumberFormatfor the specified Locale.intGets the type ofNumberFormatcreated by this validator instance.booleanTests whether the number being validated is a decimal or integer.booleanTests if the value is within a specified range.booleanTests validity using the specifiedLocale.booleanTests if the value is less than or equal to a maximum.booleanTests if the value is greater than or equal to a minimum.protected ObjectParses the value using the specified pattern.protected abstract ObjectprocessParsedValue(Object value, Format formatter) Processes the parsed value, performing any further validation and type conversion required.Methods inherited from class AbstractFormatValidator
format, format, format, format, format, isStrict, isValid, isValid, isValid, parseModifier and TypeMethodDescriptionFormats an object into aStringusing the default Locale.Formats an object into aStringusing the specified pattern.Formats an object using the specified pattern and/orLocale.protected StringFormats a value with the specifiedFormat.Formats an object into aStringusing the specified Locale.booleanisStrict()Tests whether validated values should adhere strictly to theFormatused.booleanValidates using the defaultLocale.booleanValidates using the specified pattern.booleanValidates using the specifiedLocale.protected ObjectParses the value with the specifiedFormat.
-
Field Details
-
STANDARD_FORMAT
-
CURRENCY_FORMAT
-
PERCENT_FORMAT
-
-
Constructor Details
-
AbstractNumberValidator
Constructs 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 Details
-
determineScale
Returns the multiplier of theNumberFormat.- Parameters:
format- TheNumberFormatto determine the multiplier of.- Returns:
- The multiplying factor for the format.
-
getFormat
-
getFormat
Gets aNumberFormatfor 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.
-
getFormatType
Gets the type ofNumberFormatcreated by this validator instance.- Returns:
- The format type created.
-
isAllowFractions
Tests whether the number being validated is a decimal or integer.- Returns:
trueif decimals are allowed orfalseif the number is an integer.
-
isInRange
Tests 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.
-
isValid
Tests validity using the specifiedLocale.- 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.
-
maxValue
Tests if the value is less than or equal to a maximum.For an integer validator the bound is compared exactly rather than narrowed to a
long, so aBigIntegerorBigDecimalbound outside the long range keeps its magnitude and a fractional bound keeps its fractional part. A non-finiteDoubleorFloatoperand keeps thedoubleValue()comparison, sinceBigDecimalcannot representNaNor an infinity.- 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.
-
minValue
Tests if the value is greater than or equal to a minimum.For an integer validator the bound is compared exactly rather than narrowed to a
long, so aBigIntegerorBigDecimalbound outside the long range keeps its magnitude and a fractional bound keeps its fractional part. A non-finiteDoubleorFloatoperand keeps thedoubleValue()comparison, sinceBigDecimalcannot representNaNor an infinity.- 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.
-
parse
Parses 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
Processes 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.
-