org.apache.commons.validator.routines

Class AbstractNumberValidator

public abstract class AbstractNumberValidator extends AbstractFormatValidator

Abstract class for Number Validation.

This is a base class for building Number Validators using format parsing.

Since: Validator 1.3.0

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

Field Summary
booleanallowFractions
static intCURRENCY_FORMAT
Currency NumberFormat type
intformatType
static intPERCENT_FORMAT
Percent NumberFormat type
static intSTANDARD_FORMAT
Standard NumberFormat type
Constructor Summary
AbstractNumberValidator(boolean strict, int formatType, boolean allowFractions)
Construct an instance with specified strict and decimal parameters.
Method Summary
protected intdetermineScale(NumberFormat format)

Returns the multiplier of the NumberFormat.

protected FormatgetFormat(String pattern, Locale locale)

Returns a NumberFormat for the specified pattern and/or Locale.

protected FormatgetFormat(Locale locale)

Returns a NumberFormat for the specified Locale.

intgetFormatType()

Indicates the type of NumberFormat created by this validator instance.

booleanisAllowFractions()

Indicates whether the number being validated is a decimal or integer.

booleanisInRange(Number value, Number min, Number max)
Check if the value is within a specified range.
booleanisValid(String value, String pattern, Locale locale)

Validate using the specified Locale.

booleanmaxValue(Number value, Number max)
Check if the value is less than or equal to a maximum.
booleanminValue(Number value, Number min)
Check if the value is greater than or equal to a minimum.
protected Objectparse(String value, String pattern, Locale locale)

Parse the value using the specified pattern.

protected abstract ObjectprocessParsedValue(Object value, Format formatter)

Process the parsed value, performing any further validation and type conversion required.

Field Detail

allowFractions

private boolean allowFractions

CURRENCY_FORMAT

public static final int CURRENCY_FORMAT
Currency NumberFormat type

formatType

private int formatType

PERCENT_FORMAT

public static final int PERCENT_FORMAT
Percent NumberFormat type

STANDARD_FORMAT

public static final int STANDARD_FORMAT
Standard NumberFormat type

Constructor Detail

AbstractNumberValidator

public AbstractNumberValidator(boolean strict, int formatType, boolean allowFractions)
Construct an instance with specified strict and decimal parameters.

Parameters: strict true if strict Format parsing should be used. formatType The NumberFormat type to create for validation, default is STANDARD_FORMAT. allowFractions true if fractions are allowed or false if integers only.

Method Detail

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

protected Format getFormat(String pattern, Locale locale)

Returns a NumberFormat for the specified pattern and/or Locale.

Parameters: pattern The pattern used to validate the value against or null to use the default for the Locale. locale The locale to use for the currency format, system default if null.

Returns: The NumberFormat to created.

getFormat

protected Format 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.

getFormatType

public int getFormatType()

Indicates the type of NumberFormat created by this validator instance.

Returns: the format type created.

isAllowFractions

public boolean isAllowFractions()

Indicates whether the number being validated is a decimal or integer.

Returns: true if decimals are allowed or false if the number is an integer.

isInRange

public boolean isInRange(Number value, Number min, Number max)
Check if the value is within a specified range.

Parameters: value The value validation is being performed on. min The minimum value of the range. max The maximum value of the range.

Returns: true if the value is within the specified range.

isValid

public boolean isValid(String value, String pattern, Locale locale)

Validate using the specified Locale.

Parameters: value The value validation is being performed on. pattern The pattern used to validate the value against, or the default for the Locale if null. locale The locale to use for the date format, system default if null.

Returns: true if the value is valid.

maxValue

public boolean maxValue(Number value, Number max)
Check if the value is less than or equal to a maximum.

Parameters: value The value validation is being performed on. max The maximum value.

Returns: true if the value is less than or equal to the maximum.

minValue

public boolean minValue(Number value, Number min)
Check if the value is greater than or equal to a minimum.

Parameters: value The value validation is being performed on. min The minimum value.

Returns: true if the value is greater than or equal to the minimum.

parse

protected Object parse(String value, String pattern, Locale locale)

Parse the value using the specified pattern.

Parameters: value The value validation is being performed on. pattern The pattern used to validate the value against, or the default for the Locale if null. locale The locale to use for the date format, system default if null.

Returns: The parsed value if valid or null if invalid.

processParsedValue

protected abstract Object processParsedValue(Object value, Format formatter)

Process the parsed value, performing any further validation and type conversion required.

Parameters: value The parsed object created. formatter The Format used to parse the value with.

Returns: The parsed value converted to the appropriate type if valid or null if invalid.

Copyright (c) 2001-2004 Apache Software Foundation