Top Description Fields Constructors Methods
com.univocity.parsers.conversions

public abstract Class NumericConversion<T extends Number>

extends ObjectConversion<T extends Number>
implements FormattedConversion<DecimalFormat>
Class Inheritance
All Implemented Interfaces
com.univocity.parsers.conversions.FormattedConversion
Known Direct Subclasses
com.univocity.parsers.conversions.FormattedBigDecimalConversion
Type Parameters
<T>
The type of numbers supported by this conversion class.
Imports
com.univocity.parsers.annotations.helpers.*, com.univocity.parsers.common.*, java.math.BigDecimal, .BigInteger, java.text.*, java.util.*

Converts Strings to instances of java.lang.Number and vice versa.

This class supports multiple Number formats. For example, you can define conversions from Numbers represented by different Strings such as "1,000,000.00 and $5.00".

Extending classes must implement the NumericConversion#configureFormatter(DecimalFormat) method to provide specific configuration to the DecimalFormat instance.

The reverse conversion from a Number to String (in NumericConversion#revert(Number) will return a formatted String using the pattern provided in this class constructor

The numeric patterns must follows the pattern rules of java.text.DecimalFormat

Author
Univocity Software Pty Ltd - [email protected]
See Also
java.text.DecimalFormat

Field Summary

Modifier and TypeField and Description
private String[]
private DecimalFormat[]
private Class<? extends Number>
private final ParsePosition

Constructor Summary

AccessConstructor and Description
public
NumericConversion(T
default Number to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.
valueIfStringIsNull
,
String
default String value to be returned when a Number input is null. Used when NumericConversion#revert(Number) is invoked.
valueIfObjectIsNull
,
String...
list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String in NumericConversion#revert(Number).
numericFormats
)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns.

public
NumericConversion(T
default Number to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.
valueIfStringIsNull
,
String
default String value to be returned when a Number input is null. Used when NumericConversion#revert(Number) is invoked.
valueIfObjectIsNull
,
DecimalFormat...
list formatters of acceptable numeric patterns. The first formatter in this sequence will be used to convert a Number into a String in NumericConversion#revert(Number).
numericFormatters
)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns.

public
NumericConversion(T
default Number to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.
valueIfStringIsNull
,
String
default String value to be returned when a Number input is null. Used when NumericConversion#revert(Number) is invoked.
valueIfObjectIsNull
)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns.

public
NumericConversion(String...
list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String in NumericConversion#revert(Number).
numericFormats
)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns.

public
NumericConversion(DecimalFormat...
list formatters of acceptable numeric patterns. The first formatter in this sequence will be used to convert a Number into a String in NumericConversion#revert(Number).
numericFormatters
)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns.

public
NumericConversion()

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns.

Method Summary

Modifier and TypeMethod and Description
public void
addFormat(String
a numeric pattern. The first pattern added to this class will be used to convert a Number into a String in NumericConversion#revert(Number).
format
,
String...
a sequence of properties and their values, used to configure the underlying formatter. Each element must be specified as property_name=property_value, e.g. options={"decimalSeparator=,", "maximumFractionDigits=3"}
formatOptions
)

Adds a new numeric pattern to be used to parse input Strings and convert them to numbers.

protected abstract void
configureFormatter(DecimalFormat
a DecimalFormat instance initialized with one of the patterns provided in the constructor of this class.
formatter
)

Method called by the constructor of this class to apply custom configurations to each formatter instantiated with the numeric formats provided.

protected T

Returns:

the Number instance containing the value represented by the given String, or the value of ObjectConversion#getValueIfStringIsNull() if the String input is null.
fromString
(String
the String containing a formatted number which must be converted to a number
input
)

Implements abstract com.univocity.parsers.conversions.ObjectConversion.fromString.

Converts a formatted numeric String to an instance of Number.
public DecimalFormat[]
public Class<? extends Number>

Returns:

the implementation of Number that should be used when converting numeric data.
getNumberType
()

Returns the implementation of Number that should be used when converting numeric data.

public String

Returns:

a formatted numeric String representing the value provided by the given Number, or the value of ObjectConversion#getValueIfObjectIsNull() if the Number parameter is null.
revert
(T
the Number to be converted to a String
input
)

Overrides com.univocity.parsers.conversions.ObjectConversion.revert.

Implements com.univocity.parsers.conversions.Conversion.revert.

Converts Number to a formatted numeric String.
public void
setNumberType(Class<? extends Number>
the implementation of Number that should be used when converting numeric data.
numberType
)

Defines a specific implementation of Number that should be used when converting numeric data.

Inherited from com.univocity.parsers.conversions.ObjectConversion:
executefromInputgetValueIfObjectIsNullgetValueIfStringIsNullsetValueIfObjectIsNullsetValueIfStringIsNullundo

Field Detail

formatsback to summary
private String[] formats
formattersback to summary
private DecimalFormat[] formatters
numberTypeback to summary
private Class<? extends Number> numberType
positionback to summary
private final ParsePosition position

Constructor Detail

NumericConversionback to summary
public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull, String... numericFormats)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns. This constructor assumes the output of a conversion should be null when input is null

Parameters
valueIfStringIsNull:T

default Number to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.

valueIfObjectIsNull:String

default String value to be returned when a Number input is null. Used when NumericConversion#revert(Number) is invoked.

numericFormats:String[]

list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String in NumericConversion#revert(Number).

NumericConversionback to summary
public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull, DecimalFormat... numericFormatters)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns.

Parameters
valueIfStringIsNull:T

default Number to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.

valueIfObjectIsNull:String

default String value to be returned when a Number input is null. Used when NumericConversion#revert(Number) is invoked.

numericFormatters:DecimalFormat[]

list formatters of acceptable numeric patterns. The first formatter in this sequence will be used to convert a Number into a String in NumericConversion#revert(Number).

NumericConversionback to summary
public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns. The patterns must be added to this conversion class through the addFormat(String, String...) method.

Parameters
valueIfStringIsNull:T

default Number to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.

valueIfObjectIsNull:String

default String value to be returned when a Number input is null. Used when NumericConversion#revert(Number) is invoked.

NumericConversionback to summary
public NumericConversion(String... numericFormats)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns. This constructor assumes the output of a conversion should be null when input is null

Parameters
numericFormats:String[]

list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String in NumericConversion#revert(Number).

NumericConversionback to summary
public NumericConversion(DecimalFormat... numericFormatters)

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns. This constructor assumes the output of a conversion should be null when input is null

Parameters
numericFormatters:DecimalFormat[]

list formatters of acceptable numeric patterns. The first formatter in this sequence will be used to convert a Number into a String in NumericConversion#revert(Number).

NumericConversionback to summary
public NumericConversion()

Defines a conversion from String to java.lang.Number using a sequence of acceptable numeric patterns. The patterns must be added to this conversion class through the addFormat(String, String...) method. This constructor assumes the output of a conversion should be null when input is null

Method Detail

addFormatback to summary
public void addFormat(String format, String... formatOptions)

Adds a new numeric pattern to be used to parse input Strings and convert them to numbers.

Parameters
format:String

a numeric pattern. The first pattern added to this class will be used to convert a Number into a String in NumericConversion#revert(Number).

formatOptions:String[]

a sequence of properties and their values, used to configure the underlying formatter. Each element must be specified as property_name=property_value, e.g. options={"decimalSeparator=,", "maximumFractionDigits=3"}

configureFormatterback to summary
protected abstract void configureFormatter(DecimalFormat formatter)

Method called by the constructor of this class to apply custom configurations to each formatter instantiated with the numeric formats provided.

Parameters
formatter:DecimalFormat

a DecimalFormat instance initialized with one of the patterns provided in the constructor of this class.

fromStringback to summary
protected T fromString(String input)

Implements abstract com.univocity.parsers.conversions.ObjectConversion.fromString.

Converts a formatted numeric String to an instance of Number.

The pattern in the formatted input must match one of the numeric patterns provided in the constructor of this class.

Parameters
input:String

the String containing a formatted number which must be converted to a number

Returns:T

the Number instance containing the value represented by the given String, or the value of ObjectConversion#getValueIfStringIsNull() if the String input is null.

Annotations
@SuppressWarnings:unchecked
@Override
getFormatterObjectsback to summary
public DecimalFormat[] getFormatterObjects()

Implements com.univocity.parsers.conversions.FormattedConversion.getFormatterObjects.

Doc from com.univocity.parsers.conversions.FormattedConversion.getFormatterObjects.

Returns the formatter objects

Returns:DecimalFormat[]

the formatter objects used to apply formatting to values to generate formatted Strings, and parsing formatted Strings into values

Annotations
@Override
getNumberTypeback to summary
public Class<? extends Number> getNumberType()

Returns the implementation of Number that should be used when converting numeric data.

Returns:Class<? extends Number>

the implementation of Number that should be used when converting numeric data.

revertback to summary
public String revert(T input)

Overrides com.univocity.parsers.conversions.ObjectConversion.revert.

Implements com.univocity.parsers.conversions.Conversion.revert.

Converts Number to a formatted numeric String.

The pattern used to generate the formatted number is the first numeric pattern provided in the constructor of this class

Parameters
input:T

the Number to be converted to a String

Returns:String

a formatted numeric String representing the value provided by the given Number, or the value of ObjectConversion#getValueIfObjectIsNull() if the Number parameter is null.

Annotations
@Override
setNumberTypeback to summary
public void setNumberType(Class<? extends Number> numberType)

Defines a specific implementation of Number that should be used when converting numeric data.

Parameters
numberType:Class<? extends Number>

the implementation of Number that should be used when converting numeric data.