Class CurrencyValidator
- All Implemented Interfaces:
Serializable
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 presence of the currency symbol
- It converts the currency to a
java.math.BigDecimal
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final char
DecimalFormat's currency symbolstatic final String
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected int
determineScale
(NumberFormat format) Returns the multiplier of theNumberFormat
.getExcelPattern
(Locale locale) Returns aString
representing the Excel pattern for this currency.Returns aNumberFormat
for the specified Locale.static CurrencyValidator
Return a singleton instance of this validator.protected Number
parse
(String value, DecimalFormat formatter) Parse the value with the specifiedFormat
.protected Object
Parse the value using the specified pattern.protected BigDecimal
processParsedValue
(Number value, DecimalFormat formatter) Convert the parsed value to aBigDecimal
.validate
(String value, DecimalFormat format) Validate/convert aBigDecimal
using the specifiedDecimalFormat
.Validate/convert aBigDecimal
using the specifiedLocale
.
-
Field Details
-
CURRENCY_SYMBOL
public static final char CURRENCY_SYMBOLDecimalFormat's currency symbol- See Also:
-
CURRENCY_SYMBOL_STR
-
-
Constructor Details
-
CurrencyValidator
public CurrencyValidator()
-
-
Method Details
-
getInstance
Return a singleton instance of this validator.- Returns:
- A singleton instance of the CurrencyValidator.
-
validate
Validate/convert a
BigDecimal
using the specifiedLocale
.- Parameters:
value
- The value validation is being performed on.locale
- The locale to use for the number format, system default if null.- Returns:
- The parsed
BigDecimal
if valid ornull
if invalid.
-
getExcelPattern
Returns a
String
representing the Excel pattern for this currency.- Parameters:
locale
- The locale aNumberFormat
is required for, system default if null.- Returns:
- The
String
pattern for using in Excel format.
-
parse
Parse the value using the specified pattern.
- Parameters:
value
- The value validation is being performed on.locale
- The locale to use for the date format, system default if null.- Returns:
- The parsed value if valid or
null
if invalid.
-
getFormat
Returns a
NumberFormat
for the specified Locale.- Parameters:
locale
- The locale aNumberFormat
is required for, system default if null.- Returns:
- The
NumberFormat
to created.
-
parse
Parse the value with the specified
Format
.This implementation is lenient whether the currency symbol is present or not. The default
NumberFormat
behavior 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.- Parameters:
value
- The value to be parsed.formatter
- The Format to parse the value with.- Returns:
- The parsed value if valid or
null
if invalid.
-
validate
Validate/convert a
BigDecimal
using the specifiedDecimalFormat
.- Parameters:
value
- The value validation is being performed on.format
- The format used to validate the value against.- Returns:
- The parsed
BigDecimal
if valid ornull
if invalid.
-
processParsedValue
Convert the parsed value to aBigDecimal
.- Parameters:
value
- The parsedNumber
object created.formatter
- The Format used to parse the value with.- Returns:
- The parsed
Number
converted to aBigDecimal
.
-
determineScale
Returns the multiplier of the
NumberFormat
.- Parameters:
format
- TheNumberFormat
to determine the multiplier of.- Returns:
- The multiplying factor for the format.
-