Class 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
    Modified from Commons Validator to fit PF needs
    See Also:
    Commons Validator, Serialized Form
    • Constructor Detail

      • CurrencyValidator

        public CurrencyValidator()
    • 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 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.
      • 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 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..
      • 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.
      • getPattern

        public String getPattern​(Locale locale)

        Returns a String representing the pattern for this currency.

        Parameters:
        locale - The locale a NumberFormat is required for, system default if null.
        Returns:
        The String pattern.
      • 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.