Package org.beanio.types
Class NumberTypeHandler
- java.lang.Object
-
- org.beanio.types.LocaleSupport
-
- org.beanio.types.NumberTypeHandler
-
- All Implemented Interfaces:
java.lang.Cloneable
,ConfigurableTypeHandler
,TypeHandler
- Direct Known Subclasses:
BigDecimalTypeHandler
,BigIntegerTypeHandler
,ByteTypeHandler
,DoubleTypeHandler
,FloatTypeHandler
,IntegerTypeHandler
,LongTypeHandler
,ShortTypeHandler
public abstract class NumberTypeHandler extends LocaleSupport implements ConfigurableTypeHandler, java.lang.Cloneable
Base class for type handlers that parse objects extending fromNumber
. If apattern
is set, aDecimalFormat
is used to parse and format the value. Otherwise, the value is parsed and formatted using theNumber
subclass specific to this type handler.- Since:
- 1.0
- See Also:
DecimalFormat
-
-
Field Summary
-
Fields inherited from class org.beanio.types.LocaleSupport
locale
-
Fields inherited from interface org.beanio.types.ConfigurableTypeHandler
FORMAT_SETTING
-
Fields inherited from interface org.beanio.types.TypeHandler
NIL
-
-
Constructor Summary
Constructors Constructor Description NumberTypeHandler()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected java.text.DecimalFormat
createDecimalFormat()
Creates aDecimalFormat
for parsing and formatting the number value.protected abstract java.lang.Number
createNumber(java.lang.String text)
Parses aNumber
from text.protected abstract java.lang.Number
createNumber(java.math.BigDecimal bg)
Parses aNumber
from aBigDecimal
.java.lang.String
format(java.lang.Object value)
Formats aNumber
by callingtoString()
.java.lang.String
getPattern()
Returns theDecimalFormat
pattern to use to parse and format the number value.TypeHandler
newInstance(java.util.Properties properties)
Creates a customized instance of this type handler.java.lang.Number
parse(java.lang.String text)
Parses aNumber
from the given text.void
setPattern(java.lang.String pattern)
Sets theDeimcalFormat
pattern to use to parse and format the number value.-
Methods inherited from class org.beanio.types.LocaleSupport
getLocale, setLocale
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.beanio.types.TypeHandler
getType
-
-
-
-
Method Detail
-
parse
public final java.lang.Number parse(java.lang.String text) throws TypeConversionException
Parses aNumber
from the given text.- Specified by:
parse
in interfaceTypeHandler
- Parameters:
text
- the text to parse- Returns:
- the parsed Number, or null if
text
wasnull
or an empty string - Throws:
TypeConversionException
- if the text is not a valid number
-
createNumber
protected abstract java.lang.Number createNumber(java.lang.String text) throws java.lang.NumberFormatException
Parses aNumber
from text.- Parameters:
text
- the text to convert to a Number- Returns:
- the parsed
Number
- Throws:
java.lang.NumberFormatException
- if the text is not a valid number
-
createNumber
protected abstract java.lang.Number createNumber(java.math.BigDecimal bg) throws java.lang.ArithmeticException
Parses aNumber
from aBigDecimal
.- Parameters:
bg
- theBigDecimal
version of the number- Returns:
- the parsed
Number
- Throws:
java.lang.ArithmeticException
- if theBigDecimal
cannot be converted to theNumber
type supported by this handler
-
newInstance
public TypeHandler newInstance(java.util.Properties properties) throws java.lang.IllegalArgumentException
Description copied from interface:ConfigurableTypeHandler
Creates a customized instance of this type handler.- Specified by:
newInstance
in interfaceConfigurableTypeHandler
- Parameters:
properties
- the properties for customizing the instance- Returns:
- the new
TypeHandler
- Throws:
java.lang.IllegalArgumentException
- if a property value is invalid
-
createDecimalFormat
protected java.text.DecimalFormat createDecimalFormat()
Creates aDecimalFormat
for parsing and formatting the number value.- Returns:
- the new
DecimalFormat
-
format
public java.lang.String format(java.lang.Object value)
Formats aNumber
by callingtoString()
. Ifvalue
is null,null
is returned.- Specified by:
format
in interfaceTypeHandler
- Parameters:
value
- the number to format- Returns:
- the formatted number
-
getPattern
public java.lang.String getPattern()
Returns theDecimalFormat
pattern to use to parse and format the number value. May benull
if aDecimalFormat
is not used.- Returns:
- the
DeimcalFormat
pattern
-
setPattern
public void setPattern(java.lang.String pattern)
Sets theDeimcalFormat
pattern to use to parse and format the number value. By default, the pattern is set tonull
and aNumber
subclass is used to parse and format the number value.- Parameters:
pattern
- theDecimalFormat
pattern
-
-