BeanIO 1.2.3

org.beanio.types
Class NumberTypeHandler

java.lang.Object
  extended by org.beanio.types.NumberTypeHandler
All Implemented Interfaces:
Cloneable, ConfigurableTypeHandler, TypeHandler
Direct Known Subclasses:
BigDecimalTypeHandler, BigIntegerTypeHandler, ByteTypeHandler, DoubleTypeHandler, FloatTypeHandler, IntegerTypeHandler, LongTypeHandler, ShortTypeHandler

public abstract class NumberTypeHandler
extends Object
implements ConfigurableTypeHandler, Cloneable

Base class for type handlers that parse objects extending from Number. If a pattern is set, a DecimalFormat is used to parse and format the value. Otherwise, the value is parsed and formatted using the Number subclass specific to this type handler.

Since:
1.0
Author:
Kevin Seim
See Also:
DecimalFormat

Field Summary
 
Fields inherited from interface org.beanio.types.ConfigurableTypeHandler
FORMAT_SETTING
 
Fields inherited from interface org.beanio.types.TypeHandler
NIL
 
Constructor Summary
NumberTypeHandler()
           
 
Method Summary
protected  DecimalFormat createDecimalFormat()
          Creates a DecimalFormat for parsing and formatting the number value.
protected abstract  Number createNumber(BigDecimal bg)
          Parses a Number from a BigDecimal.
protected abstract  Number createNumber(String text)
          Parses a Number from text.
 String format(Object value)
          Formats a Number by calling toString().
 String getPattern()
          Returns the DecimalFormat pattern to use to parse and format the number value.
 TypeHandler newInstance(Properties properties)
          Creates a customized instance of this type handler.
 Number parse(String text)
          Parses a Number from the given text.
 void setPattern(String pattern)
          Sets the DeimcalFormat pattern to use to parse and format the number value.
 
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
 

Constructor Detail

NumberTypeHandler

public NumberTypeHandler()
Method Detail

parse

public final Number parse(String text)
                   throws TypeConversionException
Parses a Number from the given text.

Specified by:
parse in interface TypeHandler
Parameters:
text - the text to parse
Returns:
the parsed Number, or null if text was null or an empty string
Throws:
TypeConversionException - if the text is not a valid number

createNumber

protected abstract Number createNumber(String text)
                                throws NumberFormatException
Parses a Number from text.

Parameters:
text - the text to convert to a Number
Returns:
the parsed Number
Throws:
NumberFormatException - if the text is not a valid number

createNumber

protected abstract Number createNumber(BigDecimal bg)
                                throws ArithmeticException
Parses a Number from a BigDecimal.

Parameters:
bg - the BigDecimal version of the number
Returns:
the parsed Number
Throws:
ArithmeticException - if the BigDecimal cannot be converted to the Number type supported by this handler

newInstance

public TypeHandler newInstance(Properties properties)
                        throws IllegalArgumentException
Description copied from interface: ConfigurableTypeHandler
Creates a customized instance of this type handler.

Specified by:
newInstance in interface ConfigurableTypeHandler
Parameters:
properties - the properties for customizing the instance
Returns:
the new TypeHandler
Throws:
IllegalArgumentException - if a property value is invalid

createDecimalFormat

protected DecimalFormat createDecimalFormat()
Creates a DecimalFormat for parsing and formatting the number value.

Returns:
the new DecimalFormat

format

public String format(Object value)
Formats a Number by calling toString(). If value is null, null is returned.

Specified by:
format in interface TypeHandler
Parameters:
value - the number to format
Returns:
the formatted number

getPattern

public String getPattern()
Returns the DecimalFormat pattern to use to parse and format the number value. May be null if a DecimalFormat is not used.

Returns:
the DeimcalFormat pattern

setPattern

public void setPattern(String pattern)
Sets the DeimcalFormat pattern to use to parse and format the number value. By default, the pattern is set to null and a Number subclass is used to parse and format the number value.

Parameters:
pattern - the DecimalFormat pattern

BeanIO 1.2.3

Copyright © 2010-2012 Kevin Seim