org.apache.commons.validator.routines

Class CurrencyValidator

Implemented Interfaces:
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:

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.Integer then you can simply instantiate an IntegerValidator with 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.

Version:
$Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
Since:
Validator 1.3.0

Field Summary

private static char
CURRENCY_SYMBOL
DecimalFormat's currency symbol
private static CurrencyValidator
VALIDATOR

Fields inherited from class org.apache.commons.validator.routines.BigDecimalValidator

VALIDATOR

Fields inherited from class org.apache.commons.validator.routines.AbstractNumberValidator

CURRENCY_FORMAT, PERCENT_FORMAT, STANDARD_FORMAT, allowFractions, formatType

Fields inherited from class org.apache.commons.validator.routines.AbstractFormatValidator

strict

Constructor Summary

CurrencyValidator()
Construct a strict instance.
CurrencyValidator(boolean strict, boolean allowFractions)
Construct an instance with the specified strict setting.

Method Summary

static BigDecimalValidator
getInstance()
Return a singleton instance of this validator.
protected Object
parse(String value, Format formatter)
Parse the value with the specified Format.

Methods inherited from class org.apache.commons.validator.routines.BigDecimalValidator

getInstance, 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, processParsedValue

Methods inherited from class org.apache.commons.validator.routines.AbstractFormatValidator

format, format, format, format, format, getFormat, isStrict, isValid, isValid, isValid, isValid, parse, processParsedValue

Field Details

CURRENCY_SYMBOL

private static final char CURRENCY_SYMBOL
DecimalFormat's currency symbol
Field Value:
'\u00a4'

VALIDATOR

private static final CurrencyValidator VALIDATOR

Constructor Details

CurrencyValidator

public CurrencyValidator()
Construct a strict instance.

CurrencyValidator

public CurrencyValidator(boolean strict,
                         boolean allowFractions)
Construct an instance with the specified strict setting.
Parameters:
strict - true if strict Format parsing should be used.
allowFractions - true if fractions are allowed or false if integers only.

Method Details

getInstance

public static BigDecimalValidator getInstance()
Return a singleton instance of this validator.
Overrides:
getInstance in interface BigDecimalValidator
Returns:
A singleton instance of the CurrencyValidator.

parse

protected Object parse(String value,
                       Format formatter)
Parse the value with the specified Format.

This implementation is lenient whether the currency symbol is present or not. The default NumberFormat behaviour 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:
parse in interface AbstractFormatValidator
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.

Copyright (c) 2001-2004 Apache Software Foundation