net.sourceforge.stripes.validation
Class NumberTypeConverterSupport

java.lang.Object
  extended by net.sourceforge.stripes.validation.NumberTypeConverterSupport
Direct Known Subclasses:
BigDecimalTypeConverter, BigIntegerTypeConverter, ByteTypeConverter, DoubleTypeConverter, FloatTypeConverter, IntegerTypeConverter, LongTypeConverter, PercentageTypeConverter, ShortTypeConverter

public class NumberTypeConverterSupport
extends Object

Provides the basic support for converting Strings to non-floating point numbers (i.e. shorts, integers, and longs).

Author:
Tim Fennell

Constructor Summary
NumberTypeConverterSupport()
           
 
Method Summary
 Locale getLocale()
          Returns the Locale set on the object using setLocale().
protected  NumberFormat[] getNumberFormats()
          Fetches one or more NumberFormat instances that can be used to parse numbers for the current locale.
protected  Number parse(String input, Collection<ValidationError> errors)
          Parse the input using a NumberFormatter.
protected  String preprocess(String input)
          Pre-processes the String to give the NumberFormats a better shot at parsing the input.
 void setLocale(Locale locale)
          Used by Stripes to tell the converter what locale the incoming text is in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NumberTypeConverterSupport

public NumberTypeConverterSupport()
Method Detail

setLocale

public void setLocale(Locale locale)
Used by Stripes to tell the converter what locale the incoming text is in.


getLocale

public Locale getLocale()
Returns the Locale set on the object using setLocale().


getNumberFormats

protected NumberFormat[] getNumberFormats()
Fetches one or more NumberFormat instances that can be used to parse numbers for the current locale. The default implementation returns two instances, one regular NumberFormat and a currency instance of NumberFormat.

Returns:
one or more NumberFormats to use in parsing numbers

parse

protected Number parse(String input,
                       Collection<ValidationError> errors)
Parse the input using a NumberFormatter. If the number cannot be parsed, the error key number.invalidNumber will be added to the errors.


preprocess

protected String preprocess(String input)
Pre-processes the String to give the NumberFormats a better shot at parsing the input. The default implementation trims the String for whitespace and then looks to see if the number is surrounded by parentheses, e.g. (800), and if so removes the parentheses and prepends a minus sign. Lastly it will remove the currency symbol from the String so that we don't have to use too many NumberFormats!

Parameters:
input - the String as input by the user
Returns:
the result of preprocessing the String


? Copyright 2005-2006, Stripes Development Team.