Class NumberConverter
- java.lang.Object
-
- org.apache.commons.beanutils.converters.AbstractConverter
-
- org.apache.commons.beanutils.converters.NumberConverter
-
- All Implemented Interfaces:
Converter
- Direct Known Subclasses:
BigDecimalConverter,BigIntegerConverter,ByteConverter,DoubleConverter,FloatConverter,IntegerConverter,LongConverter,ShortConverter
public abstract class NumberConverter extends AbstractConverter
Converterimplementation that handles conversion to and from java.lang.Number objects.This implementation handles conversion for the following
java.lang.Numbertypes.java.lang.Bytejava.lang.Shortjava.lang.Integerjava.lang.Longjava.lang.Floatjava.lang.Doublejava.math.BigDecimaljava.math.BigInteger
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
java.text.DecimalFormat). For example to cater for number styles used in Germany such as0.000,00the pattern is specified in the normal form0,000.00and the locale set toLocale.GERMANY.- Since:
- 1.8.0
-
-
Field Summary
Fields Modifier and Type Field Description private booleanallowDecimalsprivate java.util.Localelocaleprivate static java.lang.IntegerONEprivate java.lang.Stringpatternprivate booleanuseLocaleFormatprivate static java.lang.IntegerZERO
-
Constructor Summary
Constructors Constructor Description NumberConverter(boolean allowDecimals)Construct a java.lang.Number Converter that throws aConversionExceptionif a error occurs.NumberConverter(boolean allowDecimals, java.lang.Object defaultValue)Construct ajava.lang.NumberConverter that returns a default value if an error occurs.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.StringconvertToString(java.lang.Object value)Convert an input Number object into a String.protected <T> TconvertToType(java.lang.Class<T> targetType, java.lang.Object value)Convert the input object into a Number object of the specified type.private java.text.NumberFormatgetFormat()Return a NumberFormat to use for Conversion.java.util.LocalegetLocale()Return the Locale for the Converter (ornullif none specified).java.lang.StringgetPattern()Return the number format pattern used to convert Numbers to/from ajava.lang.String(ornullif none specified).booleanisAllowDecimals()Tests whether decimals are allowed in the number.private java.lang.Numberparse(java.lang.Class<?> sourceType, java.lang.Class<?> targetType, java.lang.String value, java.text.NumberFormat format)Convert a String into aNumberobject.voidsetLocale(java.util.Locale locale)Set the Locale for the Converter.voidsetPattern(java.lang.String pattern)Set a number format pattern to use to convert Numbers to/from ajava.lang.String.voidsetUseLocaleFormat(boolean useLocaleFormat)Set whether a format should be used to convert the Number.private java.lang.NumbertoNumber(java.lang.Class<?> sourceType, java.lang.Class<?> targetType, java.lang.String value)Default String to Number conversion.private <T> TtoNumber(java.lang.Class<?> sourceType, java.lang.Class<T> targetType, java.lang.Number value)Convert any Number object to the specified type for this Converter.java.lang.StringtoString()Provide a String representation of this number converter.-
Methods inherited from class org.apache.commons.beanutils.converters.AbstractConverter
conversionException, convert, convertArray, getDefault, getDefaultType, handleError, handleMissing, isUseDefault, log, setDefaultValue, toString
-
-
-
-
Constructor Detail
-
NumberConverter
public NumberConverter(boolean allowDecimals)
Construct a java.lang.Number Converter that throws aConversionExceptionif a error occurs.- Parameters:
allowDecimals- Indicates whether decimals are allowed
-
NumberConverter
public NumberConverter(boolean allowDecimals, java.lang.Object defaultValue)Construct ajava.lang.NumberConverter that returns a default value if an error occurs.- Parameters:
allowDecimals- Indicates whether decimals are alloweddefaultValue- The default value to be returned
-
-
Method Detail
-
convertToString
protected java.lang.String convertToString(java.lang.Object value) throws java.lang.ThrowableConvert an input Number object into a String.- Overrides:
convertToStringin classAbstractConverter- Parameters:
value- The input value to be converted- Returns:
- the converted String value.
- Throws:
java.lang.Throwable- if an error occurs converting to a String
-
convertToType
protected <T> T convertToType(java.lang.Class<T> targetType, java.lang.Object value) throws java.lang.ThrowableConvert the input object into a Number object of the specified type.- Specified by:
convertToTypein classAbstractConverter- 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:
java.lang.Throwable- if an error occurs converting to the specified type
-
getFormat
private java.text.NumberFormat getFormat()
Return a NumberFormat to use for Conversion.- Returns:
- The NumberFormat.
-
getLocale
public java.util.Locale getLocale()
Return the Locale for the Converter (ornullif none specified).- Returns:
- The locale to use for conversion
-
getPattern
public java.lang.String getPattern()
Return the number format pattern used to convert Numbers to/from ajava.lang.String(ornullif none specified).See
java.text.DecimalFormatfor details of how to specify the pattern.- Returns:
- The format pattern.
-
isAllowDecimals
public boolean isAllowDecimals()
Tests whether decimals are allowed in the number.- Returns:
- Whether decimals are allowed in the number
-
parse
private java.lang.Number parse(java.lang.Class<?> sourceType, java.lang.Class<?> targetType, java.lang.String value, java.text.NumberFormat format)Convert a String into aNumberobject.- Parameters:
sourceType- the source type of the conversiontargetType- The type to convert the value tovalue- The String date value.format- The NumberFormat to parse the String value.- Returns:
- The converted Number object.
- Throws:
ConversionException- if the String cannot be converted.
-
setLocale
public void setLocale(java.util.Locale locale)
Set the Locale for the Converter.- Parameters:
locale- The locale to use for conversion
-
setPattern
public void setPattern(java.lang.String pattern)
Set a number format pattern to use to convert Numbers to/from ajava.lang.String.See
java.text.DecimalFormatfor 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-trueif a number format should be used.
-
toNumber
private java.lang.Number toNumber(java.lang.Class<?> sourceType, java.lang.Class<?> targetType, java.lang.String value)Default String to Number conversion.This method handles conversion from a String to the following types:
java.lang.Bytejava.lang.Shortjava.lang.Integerjava.lang.Longjava.lang.Floatjava.lang.Doublejava.math.BigDecimaljava.math.BigInteger
- Parameters:
sourceType- The type being converted fromtargetType- The Number type to convert tovalue- The String value to convert.- Returns:
- The converted Number value.
-
toNumber
private <T> T toNumber(java.lang.Class<?> sourceType, java.lang.Class<T> targetType, java.lang.Number value)Convert any Number object to the specified type for this Converter.This method handles conversion to the following types:
java.lang.Bytejava.lang.Shortjava.lang.Integerjava.lang.Longjava.lang.Floatjava.lang.Doublejava.math.BigDecimaljava.math.BigInteger
- Parameters:
sourceType- The type being converted fromtargetType- The Number type to convert tovalue- The Number to convert.- Returns:
- The converted value.
-
toString
public java.lang.String toString()
Provide a String representation of this number converter.- Overrides:
toStringin classAbstractConverter- Returns:
- A String representation of this number converter
-
-