net.sourceforge.stripes.validation
Class PercentageTypeConverter

java.lang.Object
  extended by net.sourceforge.stripes.validation.NumberTypeConverterSupport
      extended by net.sourceforge.stripes.validation.PercentageTypeConverter
All Implemented Interfaces:
TypeConverter<Number>

public class PercentageTypeConverter
extends NumberTypeConverterSupport
implements TypeConverter<Number>

A locale aware number converter that parses percentages. Consistent with other areas of Java (and computing) values are divided by 100 before being returned. For example "100%" will return 1, "58%" will return 0.58 etc. The value returned is either a double, float, or BigDecimal depending on the target type supplied (which is usually driven by the type of the property being converted).

Author:
Tim Fennell

Field Summary
static Pattern PRE_PROCESS_PATTERN
          Pattern used to remove any spaces between the value and the % sign.
 
Constructor Summary
PercentageTypeConverter()
           
 
Method Summary
 Number convert(String input, Class<? extends Number> targetType, Collection<ValidationError> errors)
          Converts the input to a subclass of Number based on the targetType provided.
protected  NumberFormat[] getNumberFormats()
          Returns a single percentage instance of NumberFormat.
protected  String preprocess(String input)
          Pre-processes the input by first using NumberTypeConverterSupport.preprocess(String) and further pre-processing by adding the % sign if it is missing, any removing any spaces between the value and the % sign.
 
Methods inherited from class net.sourceforge.stripes.validation.NumberTypeConverterSupport
getLocale, parse, setLocale
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sourceforge.stripes.validation.TypeConverter
setLocale
 

Field Detail

PRE_PROCESS_PATTERN

public static final Pattern PRE_PROCESS_PATTERN
Pattern used to remove any spaces between the value and the % sign.

Constructor Detail

PercentageTypeConverter

public PercentageTypeConverter()
Method Detail

getNumberFormats

protected NumberFormat[] getNumberFormats()
Returns a single percentage instance of NumberFormat.

Overrides:
getNumberFormats in class NumberTypeConverterSupport
Returns:
one or more NumberFormats to use in parsing numbers

preprocess

protected String preprocess(String input)
Pre-processes the input by first using NumberTypeConverterSupport.preprocess(String) and further pre-processing by adding the % sign if it is missing, any removing any spaces between the value and the % sign.

Overrides:
preprocess in class NumberTypeConverterSupport
Parameters:
input - the String as input by the user
Returns:
the result of preprocessing the String

convert

public Number convert(String input,
                      Class<? extends Number> targetType,
                      Collection<ValidationError> errors)
Converts the input to a subclass of Number based on the targetType provided. Uses a NumberFormat Percentage instance to do the parsing, making sure that the number is divided by 100 and any percent signs etc. are handled.

Specified by:
convert in interface TypeConverter<Number>
Parameters:
input - the String being converted
targetType - the Class representing the type of the property to which the return value of the conversion will be assigned. In many cases this can be ignored as converters will return a single type more often than not.
errors - an empty collection of validation errors that should be populated by the converter for any errors that occur during validation that are user input related.
Returns:
T an instance of the converted type, or null if the input cannot be converted


? Copyright 2005-2006, Stripes Development Team.