Class CurrencyValidator
- java.lang.Object
-
- org.apache.commons.validator.routines.AbstractFormatValidator
-
- org.apache.commons.validator.routines.AbstractNumberValidator
-
- org.apache.commons.validator.routines.BigDecimalValidator
-
- org.apache.commons.validator.routines.CurrencyValidator
-
- All Implemented Interfaces:
java.io.Serializable
public class CurrencyValidator extends BigDecimalValidator
Currency Validation and Conversion routines (
java.math.BigDecimal).This is one implementation of a currency validator that has the following features:
- It is lenient about the the presence of the currency symbol
- It converts the currency to a
java.math.BigDecimal
However any of the number validators can be used for currency validation. For example, if you wanted a currency validator that converts to a
java.lang.Integerthen you can simply instantiate anIntegerValidatorwith the appropriate format type:... = new IntegerValidator(false, IntegerValidator.CURRENCY_FORMAT);Pick the appropriate validator, depending on the type (e.g Float, Double, Integer, Long etc) you want the currency converted to. One thing to note - only the CurrencyValidator implements lenient behaviour regarding the currency symbol.
- Since:
- Validator 1.3.0
- Version:
- $Revision: 1713331 $
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static charCURRENCY_SYMBOLDecimalFormat's currency symbolprivate static longserialVersionUIDprivate static CurrencyValidatorVALIDATOR-
Fields inherited from class org.apache.commons.validator.routines.AbstractNumberValidator
CURRENCY_FORMAT, PERCENT_FORMAT, STANDARD_FORMAT
-
-
Constructor Summary
Constructors Constructor Description CurrencyValidator()Construct a strict instance.CurrencyValidator(boolean strict, boolean allowFractions)Construct an instance with the specified strict setting.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static BigDecimalValidatorgetInstance()Return a singleton instance of this validator.protected java.lang.Objectparse(java.lang.String value, java.text.Format formatter)Parse the value with the specifiedFormat.-
Methods inherited from class org.apache.commons.validator.routines.BigDecimalValidator
isInRange, maxValue, minValue, processParsedValue, validate, validate, validate, validate
-
Methods inherited from class org.apache.commons.validator.routines.AbstractNumberValidator
determineScale, getFormat, getFormat, getFormatType, isAllowFractions, isInRange, isValid, maxValue, minValue, parse
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
VALIDATOR
private static final CurrencyValidator VALIDATOR
-
CURRENCY_SYMBOL
private static final char CURRENCY_SYMBOL
DecimalFormat's currency symbol- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CurrencyValidator
public CurrencyValidator()
Construct a strict instance.
-
CurrencyValidator
public CurrencyValidator(boolean strict, boolean allowFractions)Construct an instance with the specified strict setting.- Parameters:
strict-trueif strictFormatparsing should be used.allowFractions-trueif fractions are allowed orfalseif integers only.
-
-
Method Detail
-
getInstance
public static BigDecimalValidator getInstance()
Return a singleton instance of this validator.- Returns:
- A singleton instance of the CurrencyValidator.
-
parse
protected java.lang.Object parse(java.lang.String value, java.text.Format formatter)Parse the value with the specified
Format.This implementation is lenient whether the currency symbol is present or not. The default
NumberFormatbehaviour 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.
-
-