Class NumberConverter<N extends Number>
java.lang.Object
org.apache.commons.beanutils2.converters.AbstractConverter<N>
org.apache.commons.beanutils2.converters.NumberConverter<N>
- Type Parameters:
N- The default value type.
- All Implemented Interfaces:
Converter<N>
- Direct Known Subclasses:
BigDecimalConverter,BigIntegerConverter,ByteConverter,DoubleConverter,FloatConverter,IntegerConverter,LongConverter,ShortConverter
Converter implementation that handles conversion to and from java.lang.Number objects.
This implementation handles conversion for the following 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 the default Locale, configure using:
setUseLocaleFormat(true)
- Using the default format for a specified Locale, configure using:
setLocale(Locale)
- Using a specified pattern for the default Locale, configure using:
setPattern(String)
- Using a specified pattern for a specified Locale, configure using:
setPattern(String)setLocale(Locale)
- If none of the above are configured the
toNumber(String)method is used to convert from String to Number and the Number'stoString()method used to convert from Number to String.
N.B.Patterns can only be specified using the standard pattern characters and NOT in localized form (see
DecimalFormat). 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</code> and the locale set to <code>Locale.GERMANY
- Since:
- 1.8.0
-
Constructor Summary
ConstructorsConstructorDescriptionNumberConverter(boolean allowDecimals) Constructs a java.lang.Number Converter that throws aConversionExceptionif a error occurs.NumberConverter(boolean allowDecimals, N defaultValue) Constructs aNumberConverter that returns a default value if an error occurs. -
Method Summary
Modifier and TypeMethodDescriptionprotected StringconvertToString(Object value) Convert an input Number object into a String.protected <T> TconvertToType(Class<T> targetType, Object value) Convert the input object into a Number object of the specified type.Gets the Locale for the Converter (ornullif none specified).Gets the number format pattern used to convert Numbers to/from aString(ornullif none specified).booleanGets whether decimals are allowed in the number.voidSets the Locale for the Converter.voidsetPattern(String pattern) Sets a number format pattern to use to convert Numbers to/from aString.voidsetUseLocaleFormat(boolean useLocaleFormat) Sets whether a format should be used to convert the Number.toString()Provide a String representation of this number converter.Methods inherited from class org.apache.commons.beanutils2.converters.AbstractConverter
conversionException, convert, convertArray, getDefault, getDefaultType, handleError, handleMissing, isUseDefault, setDefaultValue, toLowerCase, toString, toTrim
-
Constructor Details
-
NumberConverter
Constructs a java.lang.Number Converter that throws aConversionExceptionif a error occurs.- Parameters:
allowDecimals- Indicates whether decimals are allowed
-
NumberConverter
Constructs aNumberConverter that returns a default value if an error occurs.- Parameters:
allowDecimals- Indicates whether decimals are alloweddefaultValue- The default value to be returned
-
-
Method Details
-
convertToString
Convert an input Number object into a String.- Overrides:
convertToStringin classAbstractConverter<N extends Number>- Parameters:
value- The input value to be converted- Returns:
- the converted String value.
- Throws:
IllegalArgumentException- if an error occurs converting to a String
-
convertToType
Convert the input object into a Number object of the specified type.- Specified by:
convertToTypein classAbstractConverter<N extends Number>- Type Parameters:
T- Target type of the conversion.- 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
Gets the Locale for the Converter (ornullif none specified).- Returns:
- The locale to use for conversion
-
getPattern
Gets the number format pattern used to convert Numbers to/from aString(ornullif none specified).See
DecimalFormatfor details of how to specify the pattern.- Returns:
- The format pattern.
-
isAllowDecimals
Gets whether decimals are allowed in the number.- Returns:
- Whether decimals are allowed in the number
-
setLocale
Sets the Locale for the Converter.- Parameters:
locale- The locale to use for conversion
-
setPattern
Sets a number format pattern to use to convert Numbers to/from aString.See
DecimalFormatfor details of how to specify the pattern.- Parameters:
pattern- The format pattern.
-
setUseLocaleFormat
Sets whether a format should be used to convert the Number.- Parameters:
useLocaleFormat-trueif a number format should be used.
-
toString
Provide a String representation of this number converter.- Overrides:
toStringin classAbstractConverter<N extends Number>- Returns:
- A String representation of this number converter
-