Class PercentValidator
- All Implemented Interfaces:
Serializable
java.math.BigDecimal).
This is one implementation of a percent validator that has the following features:
- It is lenient about the presence of the percent symbol
- It converts the percent to a
java.math.BigDecimal
However any of the number validators can be used for percent validation.
For example, if you wanted a percent validator that converts to a
{Float then you can simply instantiate an
FloatValidator with the appropriate format type:
... = new FloatValidator(false, FloatValidator.PERCENT_FORMAT);
Pick the appropriate validator, depending on the type (that is, Float, Double or BigDecimal)
you want the percent converted to. Please note, it makes no sense to use
one of the validators that doesn't handle fractions (such as byte, short, integer, long
and BigInteger) since percentages are converted to fractions (for example, 50% is
converted to 0.5).
- Since:
- 1.3.0
- See Also:
-
Field Summary
Fields inherited from class AbstractNumberValidator
CURRENCY_FORMAT, PERCENT_FORMAT, STANDARD_FORMATModifier and TypeFieldDescriptionstatic final intCurrencyNumberFormattype.static final intPercentNumberFormattype.static final intStandardNumberFormattype. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a strict instance.PercentValidator(boolean strict) Constructs an instance with the specified strict setting. -
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimalValidatorGets the singleton instance of this validator.protected ObjectParse the value with the specifiedFormat.Methods inherited from class BigDecimalValidator
getFormat, isInRange, maxValue, maxValue, minValue, minValue, processParsedValue, validate, validate, validate, validateModifier and TypeMethodDescriptionprotected FormatReturns aFormatthat parses to aBigDecimalso the exact value of the input is preserved.booleanisInRange(BigDecimal value, double min, double max) Tests if the value is within a specified range.booleanTests if the value is less than or equal to a maximum, comparing the exact values.booleanmaxValue(BigDecimal value, double max) Tests if the value is less than or equal to a maximum.booleanTests if the value is greater than or equal to a minimum, comparing the exact values.booleanminValue(BigDecimal value, double min) Tests if the value is greater than or equal to a minimum.protected ObjectprocessParsedValue(Object value, Format formatter) Converts the parsed value to aBigDecimal.Validates and converts aBigDecimalusing the defaultLocale.Validates and converts aBigDecimalusing the specified pattern.Validates and converts aBigDecimalusing the specified pattern and/ orLocale.Validates and converts aBigDecimalusing the specifiedLocale.Methods inherited from class AbstractNumberValidator
determineScale, getFormat, getFormatType, isAllowFractions, isInRange, isValid, parseModifier and TypeMethodDescriptionprotected intdetermineScale(NumberFormat format) Returns the multiplier of theNumberFormat.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.protected ObjectParses the value using the specified pattern.Methods inherited from class AbstractFormatValidator
format, format, format, format, format, isStrict, isValid, isValid, isValidModifier 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.
-
Constructor Details
-
PercentValidator
public PercentValidator()Constructs a strict instance. -
PercentValidator
Constructs an instance with the specified strict setting.- Parameters:
strict-trueif strictFormatparsing should be used.
-
-
Method Details
-
getInstance
Gets the singleton instance of this validator.- Returns:
- A singleton instance of the PercentValidator.
-
parse
Parse the value with the specifiedFormat.This implementation is lenient whether the currency symbol is present or not. The default
NumberFormatbehavior is for the parsing to "fail" if the currency symbol is missing. This method re-parses with a format without the currency symbol if it fails initially.- Overrides:
parsein classAbstractFormatValidator- Parameters:
value- The value to be parsed.formatter- The Format to parse the value with.- Returns:
- The parsed value if valid or
nullif invalid.
-