Class CurrencyValidator

java.lang.Object
org.primefaces.util.CurrencyValidator
All Implemented Interfaces:
Serializable

public class CurrencyValidator extends Object

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
Modified from Commons Validator to fit PF needs
See Also:
  • Field Details

    • CURRENCY_SYMBOL

      public static final char CURRENCY_SYMBOL
      DecimalFormat's currency symbol
      See Also:
    • CURRENCY_SYMBOL_STR

      public static final String CURRENCY_SYMBOL_STR
  • Constructor Details

    • CurrencyValidator

      public CurrencyValidator()
  • Method Details

    • 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 specified Locale.

      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 or null if invalid.
    • getExcelPattern

      public String getExcelPattern(Locale locale)

      Returns a String representing the Excel pattern for this currency.

      Parameters:
      locale - The locale a NumberFormat is required for, system default if null.
      Returns:
      The String pattern for using in Excel format.
    • parse

      protected Object parse(String value, Locale locale)

      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

      public DecimalFormat getFormat(Locale locale)

      Returns a NumberFormat for the specified Locale.

      Parameters:
      locale - The locale a NumberFormat is required for, system default if null.
      Returns:
      The NumberFormat to created.
    • parse

      protected Number 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.
    • validate

      public BigDecimal validate(String value, DecimalFormat format)

      Validate/convert a BigDecimal using the specified DecimalFormat.

      Parameters:
      value - The value validation is being performed on.
      format - The format used to validate the value against.
      Returns:
      The parsed BigDecimal if valid or null if invalid.
    • processParsedValue

      protected BigDecimal processParsedValue(Number value, DecimalFormat formatter)
      Convert the parsed value to a BigDecimal.
      Parameters:
      value - The parsed Number object created.
      formatter - The Format used to parse the value with.
      Returns:
      The parsed Number converted to a BigDecimal.
    • determineScale

      protected int determineScale(NumberFormat format)

      Returns the multiplier of the NumberFormat.

      Parameters:
      format - The NumberFormat to determine the multiplier of.
      Returns:
      The multiplying factor for the format.