org.apache.commons.beanutils.converters

Class NumberConverter

Implemented Interfaces:
Converter
Known Direct Subclasses:
BigDecimalConverter, BigIntegerConverter, ByteConverter, DoubleConverter, FloatConverter, IntegerConverter, LongConverter, ShortConverter

public 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:
  • Using the default format for a specified Locale, configure using:
  • Using a specified pattern for the default Locale, configure using:
  • Using a specified pattern for a specified Locale, configure using:
  • If none of the above are configured the toNumber(String) method is used to convert from String to Number and the Number's toString() method used to convert from Number to String.
  • 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.

    Version:
    $Revision: 555845 $ $Date: 2007-07-13 03:52:05 +0100 (Fri, 13 Jul 2007) $
    Since:
    1.8.0

    Field Summary

    private static Integer
    ONE
    private static Integer
    ZERO
    private boolean
    allowDecimals
    private Locale
    locale
    private String
    pattern
    private boolean
    useLocaleFormat

    Fields inherited from class org.apache.commons.beanutils.converters.AbstractConverter

    DEFAULT_CONFIG_MSG, PACKAGE, defaultType, defaultValue, log, useDefault

    Constructor Summary

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

    Method Summary

    protected String
    convertToString(Object value)
    Convert an input Number object into a String.
    protected Object
    convertToType(Class targetType, Object value)
    Convert the input object into a Number object of the specified type.
    private NumberFormat
    getFormat()
    Return a NumberFormat to use for Conversion.
    Locale
    getLocale()
    Return the Locale for the Converter (or null if none specified).
    String
    getPattern()
    Return the number format pattern used to convert Numbers to/from a java.lang.String (or null if none specified).
    boolean
    isAllowDecimals()
    Return whether decimals are allowed in the number.
    private Number
    parse(Class sourceType, Class targetType, String value, NumberFormat format)
    Convert a String into a Number object.
    void
    setLocale(Locale locale)
    Set the Locale for the Converter.
    void
    setPattern(String pattern)
    Set a number format pattern to use to convert Numbers to/from a java.lang.String.
    void
    setUseLocaleFormat(boolean useLocaleFormat)
    Set whether a format should be used to convert the Number.
    private Number
    toNumber(Class sourceType, Class targetType, Number value)
    Convert any Number object to the specified type for this Converter.
    private Number
    toNumber(Class sourceType, Class targetType, String value)
    Default String to Number conversion.
    String
    toString()
    Provide a String representation of this number converter.

    Methods inherited from class org.apache.commons.beanutils.converters.AbstractConverter

    convert, convertArray, convertToString, convertToType, getDefault, getDefaultType, handleError, handleMissing, isUseDefault, log, primitive, setDefaultValue, toString, toString

    Field Details

    ONE

    private static final Integer ONE

    ZERO

    private static final Integer ZERO

    allowDecimals

    private boolean allowDecimals

    locale

    private Locale locale

    pattern

    private String pattern

    useLocaleFormat

    private boolean useLocaleFormat

    Constructor Details

    NumberConverter

    public NumberConverter(Class defaultType,
                           boolean allowDecimals)
    Construct a java.lang.Number Converter that throws a ConversionException if a error occurs.
    Parameters:
    defaultType - The default type this Converter handles
    allowDecimals - Indicates whether decimals are allowed

    NumberConverter

    public NumberConverter(Class defaultType,
                           boolean allowDecimals,
                           Object defaultValue)
    Construct a java.lang.Number Converter that returns a default value if an error occurs.
    Parameters:
    defaultType - The default type this Converter handles
    allowDecimals - Indicates whether decimals are allowed
    defaultValue - The default value to be returned

    Method Details

    convertToString

    protected String convertToString(Object value)
                throws Throwable
    Convert an input Number object into a String.
    Overrides:
    convertToString in interface AbstractConverter
    Parameters:
    value - The input value to be converted
    Returns:
    the converted String value.

    convertToType

    protected Object convertToType(Class targetType,
                                   Object value)
                throws Throwable
    Convert the input object into a Number object of the specified type.
    Overrides:
    convertToType in interface AbstractConverter
    Parameters:
    targetType - Data type to which this value should be converted.
    value - The input value to be converted.
    Returns:
    The converted value.

    getFormat

    private NumberFormat getFormat()
    Return a NumberFormat to use for Conversion.
    Returns:
    The NumberFormat.

    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

    parse

    private Number parse(Class sourceType,
                         Class targetType,
                         String value,
                         NumberFormat format)
    Convert a String into a Number object.
    Parameters:
    sourceType - TODO
    targetType - The type to convert the value to
    value - The String date value.
    format - The NumberFormat to parse the String value.
    Returns:
    The converted Number object.

    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.

    toNumber

    private Number toNumber(Class sourceType,
                            Class targetType,
                            Number value)
    Convert any Number object to the specified type for this Converter.

    This method handles conversion to the following types:

    • java.lang.Byte
    • java.lang.Short
    • java.lang.Integer
    • java.lang.Long
    • java.lang.Float
    • java.lang.Double
    • java.math.BigDecimal
    • java.math.BigInteger
    Parameters:
    sourceType - The type being converted from
    targetType - The Number type to convert to
    value - The Number to convert.
    Returns:
    The converted value.

    toNumber

    private Number toNumber(Class sourceType,
                            Class targetType,
                            String value)
    Default String to Number conversion.

    This method handles conversion from a String to the following types:

    • java.lang.Byte
    • java.lang.Short
    • java.lang.Integer
    • java.lang.Long
    • java.lang.Float
    • java.lang.Double
    • java.math.BigDecimal
    • java.math.BigInteger
    Parameters:
    sourceType - The type being converted from
    targetType - The Number type to convert to
    value - The String value to convert.
    Returns:
    The converted Number value.

    toString

    public String toString()
    Provide a String representation of this number converter.
    Overrides:
    toString in interface AbstractConverter
    Returns:
    A String representation of this number converter

    Copyright (c) 2001-2007 - Apache Software Foundation