org.apache.commons.validator.routines
Class PercentValidator

java.lang.Object
  extended by org.apache.commons.validator.routines.AbstractFormatValidator
      extended by org.apache.commons.validator.routines.AbstractNumberValidator
          extended by org.apache.commons.validator.routines.BigDecimalValidator
              extended by org.apache.commons.validator.routines.PercentValidator
All Implemented Interfaces:
java.io.Serializable

public class PercentValidator
extends BigDecimalValidator

Percentage Validation and Conversion routines (java.math.BigDecimal).

This is one implementation of a percent validator that has the following features:

However any of the number validators can be used for percent validation. For example, if you wanted a percent validator that converts to a java.lang.Float then you can simply instantiate an FloatValidator with the appropriate format type:

... = new FloatValidator(false, FloatValidator.PERCENT_FORMAT);

Pick the appropriate validator, depending on the type (i.e Float, Double or BigDecimal) you want the percent converted to. Please note, it makes no sense to use one of the validators that doesn't handle fractions (i.e. byte, short, integer, long and BigInteger) since percentages are converted to fractions (i.e 50% is converted to 0.5).

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

Field Summary
private static char PERCENT_SYMBOL
          DecimalFormat's percent (thousand multiplier) symbol
private static java.math.BigDecimal POINT_ZERO_ONE
           
private static PercentValidator VALIDATOR
           
 
Fields inherited from class org.apache.commons.validator.routines.AbstractNumberValidator
CURRENCY_FORMAT, PERCENT_FORMAT, STANDARD_FORMAT
 
Constructor Summary
PercentValidator()
          Construct a strict instance.
PercentValidator(boolean strict)
          Construct an instance with the specified strict setting.
 
Method Summary
static BigDecimalValidator getInstance()
          Return a singleton instance of this validator.
protected  java.lang.Object parse(java.lang.String value, java.text.Format formatter)
          Parse the value with the specified Format.
 
Methods inherited from class org.apache.commons.validator.routines.BigDecimalValidator
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
 
Methods inherited from class org.apache.commons.validator.routines.AbstractFormatValidator
format, format, format, format, format, isStrict, isValid, isValid, isValid
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VALIDATOR

private static final PercentValidator VALIDATOR

PERCENT_SYMBOL

private static final char PERCENT_SYMBOL
DecimalFormat's percent (thousand multiplier) symbol

See Also:
Constant Field Values

POINT_ZERO_ONE

private static final java.math.BigDecimal POINT_ZERO_ONE
Constructor Detail

PercentValidator

public PercentValidator()
Construct a strict instance.


PercentValidator

public PercentValidator(boolean strict)
Construct an instance with the specified strict setting.

Parameters:
strict - true if strict Format parsing should be used.
Method Detail

getInstance

public static BigDecimalValidator getInstance()
Return a singleton instance of this validator.

Returns:
A singleton instance of the PercentValidator.

parse

protected java.lang.Object parse(java.lang.String value,
                                 java.text.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 class 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