Package org.primefaces.util
Class CurrencyValidator
- java.lang.Object
-
- org.primefaces.util.CurrencyValidator
-
- All Implemented Interfaces:
Serializable
public class CurrencyValidator extends Object implements 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:
- Commons Validator, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description CurrencyValidator()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
determineScale(NumberFormat format)
Returns the multiplier of theNumberFormat
.DecimalFormat
getFormat(Locale locale)
Returns aNumberFormat
for the specified Locale.static CurrencyValidator
getInstance()
Return a singleton instance of this validator.String
getPattern(Locale locale)
Returns aString
representing the pattern for this currency.protected Object
parse(String value, DecimalFormat formatter)
Parse the value with the specifiedFormat
.protected Object
parse(String value, Locale locale)
Parse the value using the specified pattern.protected Object
parseFormat(String value, DecimalFormat formatter)
Parse the value with the specifiedFormat
.protected BigDecimal
processParsedValue(Number value, DecimalFormat formatter)
Convert the parsed value to aBigDecimal
.BigDecimal
validate(String value, Locale locale)
Validate/convert aBigDecimal
using the specifiedLocale
.
-
-
-
Method Detail
-
getInstance
public static CurrencyValidator getInstance()
Return a singleton instance of this validator.- Returns:
- A singleton instance of the CurrencyValidator.
-
validate
public BigDecimal validate(String value, Locale locale)
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.
-
parse
protected Object parse(String value, DecimalFormat formatter)
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.
-
parseFormat
protected Object parseFormat(String value, DecimalFormat formatter)
Parse the value with the specified
Format
.- 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.
-
processParsedValue
protected BigDecimal processParsedValue(Number value, DecimalFormat formatter)
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
protected int determineScale(NumberFormat format)
Returns the multiplier of the
NumberFormat
.- Parameters:
format
- TheNumberFormat
to determine the multiplier of.- Returns:
- The multiplying factor for the format..
-
getFormat
public DecimalFormat getFormat(Locale locale)
Returns a
NumberFormat
for the specified Locale.- Parameters:
locale
- The locale aNumberFormat
is required for, system default if null.- Returns:
- The
NumberFormat
to created.
-
getPattern
public String getPattern(Locale locale)
Returns a
String
representing the pattern for this currency.- Parameters:
locale
- The locale aNumberFormat
is required for, system default if null.- Returns:
- The
String
pattern.
-
-