org.apache.commons.beanutils.converters

Class NumberConverter

public abstract class NumberConverter extends AbstractConverter

Converter implementaion that handles conversion to and from java.lang.Number objects.

This implementation handles conversion for the following java.lang.Number types.

String Conversions (to and from)

This class provides a number of ways in which number conversions to/from Strings can be achieved:

N.B.Patterns can only be specified used the standard pattern characters and NOT in localized form (see java.text.SimpleDateFormat). For example to cater for number styles used in Germany such as 0.000,00 the pattern is specified in the normal form 0,000.00 and the locale set to Locale.GERMANY.

Since: 1.8.0

Version: $Revision: 745081 $ $Date: 2009-02-17 14:05:20 +0000 (Tue, 17 Feb 2009) $

Constructor Summary
NumberConverter(boolean allowDecimals)
Construct a java.lang.Number Converter that throws a ConversionException if a error occurs.
NumberConverter(boolean allowDecimals, Object defaultValue)
Construct a java.lang.Number Converter that returns a default value if an error occurs.
Method Summary
protected StringconvertToString(Object value)
Convert an input Number object into a String.
protected ObjectconvertToType(Class targetType, Object value)
Convert the input object into a Number object of the specified type.
LocalegetLocale()
Return the Locale for the Converter (or null if none specified).
StringgetPattern()
Return the number format pattern used to convert Numbers to/from a java.lang.String (or null if none specified).
booleanisAllowDecimals()
Return whether decimals are allowed in the number.
voidsetLocale(Locale locale)
Set the Locale for the Converter.
voidsetPattern(String pattern)
Set a number format pattern to use to convert Numbers to/from a java.lang.String.
voidsetUseLocaleFormat(boolean useLocaleFormat)
Set whether a format should be used to convert the Number.
StringtoString()
Provide a String representation of this number converter.

Constructor Detail

NumberConverter

public NumberConverter(boolean allowDecimals)
Construct a java.lang.Number Converter that throws a ConversionException if a error occurs.

Parameters: allowDecimals Indicates whether decimals are allowed

NumberConverter

public NumberConverter(boolean allowDecimals, Object defaultValue)
Construct a java.lang.Number Converter that returns a default value if an error occurs.

Parameters: allowDecimals Indicates whether decimals are allowed defaultValue The default value to be returned

Method Detail

convertToString

protected String convertToString(Object value)
Convert an input Number object into a String.

Parameters: value The input value to be converted

Returns: the converted String value.

Throws: Throwable if an error occurs converting to a String

convertToType

protected Object convertToType(Class targetType, Object value)
Convert the input object into a Number object of the specified type.

Parameters: targetType Data type to which this value should be converted. value The input value to be converted.

Returns: The converted value.

Throws: Throwable if an error occurs converting to the specified type

getLocale

public Locale getLocale()
Return the Locale for the Converter (or null if none specified).

Returns: The locale to use for conversion

getPattern

public String getPattern()
Return the number format pattern used to convert Numbers to/from a java.lang.String (or null if none specified).

See java.text.SimpleDateFormat for details of how to specify the pattern.

Returns: The format pattern.

isAllowDecimals

public boolean isAllowDecimals()
Return whether decimals are allowed in the number.

Returns: Whether decimals are allowed in the number

setLocale

public void setLocale(Locale locale)
Set the Locale for the Converter.

Parameters: locale The locale to use for conversion

setPattern

public void setPattern(String pattern)
Set a number format pattern to use to convert Numbers to/from a java.lang.String.

See java.text.SimpleDateFormat for details of how to specify the pattern.

Parameters: pattern The format pattern.

setUseLocaleFormat

public void setUseLocaleFormat(boolean useLocaleFormat)
Set whether a format should be used to convert the Number.

Parameters: useLocaleFormat true if a number format should be used.

toString

public String toString()
Provide a String representation of this number converter.

Returns: A String representation of this number converter

Copyright © 2000-2010 Apache Software Foundation. All Rights Reserved.