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 apatternis set, aDecimalFormatis used to parse and format the value. Otherwise, the value is parsed and formatted using theNumbersubclass 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.DecimalFormatcreateDecimalFormat()Creates aDecimalFormatfor parsing and formatting the number value.protected abstract java.lang.NumbercreateNumber(java.lang.String text)Parses aNumberfrom text.protected abstract java.lang.NumbercreateNumber(java.math.BigDecimal bg)Parses aNumberfrom aBigDecimal.java.lang.Stringformat(java.lang.Object value)Formats aNumberby callingtoString().java.lang.StringgetPattern()Returns theDecimalFormatpattern to use to parse and format the number value.TypeHandlernewInstance(java.util.Properties properties)Creates a customized instance of this type handler.java.lang.Numberparse(java.lang.String text)Parses aNumberfrom the given text.voidsetPattern(java.lang.String pattern)Sets theDeimcalFormatpattern 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 TypeConversionExceptionParses aNumberfrom the given text.- Specified by:
parsein interfaceTypeHandler- Parameters:
text- the text to parse- Returns:
- the parsed Number, or null if
textwasnullor 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.NumberFormatExceptionParses aNumberfrom 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.ArithmeticExceptionParses aNumberfrom aBigDecimal.- Parameters:
bg- theBigDecimalversion of the number- Returns:
- the parsed
Number - Throws:
java.lang.ArithmeticException- if theBigDecimalcannot be converted to theNumbertype supported by this handler
-
newInstance
public TypeHandler newInstance(java.util.Properties properties) throws java.lang.IllegalArgumentException
Description copied from interface:ConfigurableTypeHandlerCreates a customized instance of this type handler.- Specified by:
newInstancein 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 aDecimalFormatfor parsing and formatting the number value.- Returns:
- the new
DecimalFormat
-
format
public java.lang.String format(java.lang.Object value)
Formats aNumberby callingtoString(). Ifvalueis null,nullis returned.- Specified by:
formatin interfaceTypeHandler- Parameters:
value- the number to format- Returns:
- the formatted number
-
getPattern
public java.lang.String getPattern()
Returns theDecimalFormatpattern to use to parse and format the number value. May benullif aDecimalFormatis not used.- Returns:
- the
DeimcalFormatpattern
-
setPattern
public void setPattern(java.lang.String pattern)
Sets theDeimcalFormatpattern to use to parse and format the number value. By default, the pattern is set tonulland aNumbersubclass is used to parse and format the number value.- Parameters:
pattern- theDecimalFormatpattern
-
-