Class NumericConversion<T extends java.lang.Number>
- java.lang.Object
-
- com.univocity.parsers.conversions.NullConversion<java.lang.String,T>
-
- com.univocity.parsers.conversions.ObjectConversion<T>
-
- com.univocity.parsers.conversions.NumericConversion<T>
-
- Type Parameters:
T
- The type of numbers supported by this conversion class.
- All Implemented Interfaces:
Conversion<java.lang.String,T>
,FormattedConversion<java.text.DecimalFormat>
- Direct Known Subclasses:
FormattedBigDecimalConversion
public abstract class NumericConversion<T extends java.lang.Number> extends ObjectConversion<T> implements FormattedConversion<java.text.DecimalFormat>
Converts Strings to instances ofNumber
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
configureFormatter(DecimalFormat)
method to provide specific configuration to the DecimalFormat instance.The reverse conversion from a Number to String (in
revert(Number)
will return a formatted String using the pattern provided in this class constructorThe numeric patterns must follows the pattern rules of
DecimalFormat
- See Also:
DecimalFormat
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String[]
formats
private java.text.DecimalFormat[]
formatters
private java.lang.Class<? extends java.lang.Number>
numberType
private java.text.ParsePosition
position
-
Constructor Summary
Constructors Constructor Description NumericConversion()
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion(java.lang.String... numericFormats)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion(java.text.DecimalFormat... numericFormatters)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion(T valueIfStringIsNull, java.lang.String valueIfObjectIsNull)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion(T valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... numericFormats)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion(T valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.text.DecimalFormat... numericFormatters)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addFormat(java.lang.String format, java.lang.String... formatOptions)
Adds a new numeric pattern to be used to parse input Strings and convert them to numbers.protected abstract void
configureFormatter(java.text.DecimalFormat formatter)
Method called by the constructor of this class to apply custom configurations to each formatter instantiated with the numeric formats provided.protected T
fromString(java.lang.String input)
Converts a formatted numeric String to an instance of Number.java.text.DecimalFormat[]
getFormatterObjects()
Returns the formatter objectsjava.lang.Class<? extends java.lang.Number>
getNumberType()
Returns the implementation ofNumber
that should be used when converting numeric data.java.lang.String
revert(T input)
Converts Number to a formatted numeric String.void
setNumberType(java.lang.Class<? extends java.lang.Number> numberType)
Defines a specific implementation ofNumber
that should be used when converting numeric data.-
Methods inherited from class com.univocity.parsers.conversions.ObjectConversion
execute, fromInput, getValueIfObjectIsNull, getValueIfStringIsNull, setValueIfObjectIsNull, setValueIfStringIsNull, undo
-
Methods inherited from class com.univocity.parsers.conversions.NullConversion
getValueOnNullInput, getValueOnNullOutput, setValueOnNullInput, setValueOnNullOutput
-
-
-
-
Constructor Detail
-
NumericConversion
public NumericConversion(T valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... numericFormats)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns. This constructor assumes the output of a conversion should be null when input is null- Parameters:
valueIfStringIsNull
- default Number to be returned when the input String is null. Used whenObjectConversion.execute(String)
is invoked.valueIfObjectIsNull
- default String value to be returned when a Number input is null. Used whenrevert(Number)
is invoked.numericFormats
- list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String inrevert(Number)
.
-
NumericConversion
public NumericConversion(T valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.text.DecimalFormat... numericFormatters)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.- Parameters:
valueIfStringIsNull
- default Number to be returned when the input String is null. Used whenObjectConversion.execute(String)
is invoked.valueIfObjectIsNull
- default String value to be returned when a Number input is null. Used whenrevert(Number)
is invoked.numericFormatters
- list formatters of acceptable numeric patterns. The first formatter in this sequence will be used to convert a Number into a String inrevert(Number)
.
-
NumericConversion
public NumericConversion(T valueIfStringIsNull, java.lang.String valueIfObjectIsNull)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns. The patterns must be added to this conversion class through theaddFormat(String, String...)
method.- Parameters:
valueIfStringIsNull
- default Number to be returned when the input String is null. Used whenObjectConversion.execute(String)
is invoked.valueIfObjectIsNull
- default String value to be returned when a Number input is null. Used whenrevert(Number)
is invoked.
-
NumericConversion
public NumericConversion(java.lang.String... numericFormats)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns. This constructor assumes the output of a conversion should be null when input is null- Parameters:
numericFormats
- list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String inrevert(Number)
.
-
NumericConversion
public NumericConversion(java.text.DecimalFormat... numericFormatters)
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns. This constructor assumes the output of a conversion should be null when input is null- Parameters:
numericFormatters
- list formatters of acceptable numeric patterns. The first formatter in this sequence will be used to convert a Number into a String inrevert(Number)
.
-
NumericConversion
public NumericConversion()
Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns. The patterns must be added to this conversion class through theaddFormat(String, String...)
method. This constructor assumes the output of a conversion should be null when input is null
-
-
Method Detail
-
getNumberType
public java.lang.Class<? extends java.lang.Number> getNumberType()
Returns the implementation ofNumber
that should be used when converting numeric data.- Returns:
- the implementation of
Number
that should be used when converting numeric data.
-
setNumberType
public void setNumberType(java.lang.Class<? extends java.lang.Number> numberType)
Defines a specific implementation ofNumber
that should be used when converting numeric data.- Parameters:
numberType
- the implementation ofNumber
that should be used when converting numeric data.
-
getFormatterObjects
public java.text.DecimalFormat[] getFormatterObjects()
Description copied from interface:FormattedConversion
Returns the formatter objects- Specified by:
getFormatterObjects
in interfaceFormattedConversion<T extends java.lang.Number>
- Returns:
- the formatter objects used to apply formatting to values to generate formatted Strings, and parsing formatted Strings into values
-
configureFormatter
protected abstract void configureFormatter(java.text.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
- a DecimalFormat instance initialized with one of the patterns provided in the constructor of this class.
-
fromString
protected T fromString(java.lang.String input)
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.
- Specified by:
fromString
in classObjectConversion<T extends java.lang.Number>
- Parameters:
input
- the String containing a formatted number which must be converted to a number- Returns:
- the Number instance containing the value represented by the given String, or the value of
ObjectConversion.getValueIfStringIsNull()
if the String input is null.
-
revert
public java.lang.String revert(T input)
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
- Specified by:
revert
in interfaceConversion<java.lang.String,T extends java.lang.Number>
- Overrides:
revert
in classObjectConversion<T extends java.lang.Number>
- Parameters:
input
- the Number to be converted to a 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.
-
addFormat
public void addFormat(java.lang.String format, java.lang.String... formatOptions)
Adds a new numeric pattern to be used to parse input Strings and convert them to numbers.- Parameters:
format
- a numeric pattern. The first pattern added to this class will be used to convert a Number into a String inrevert(Number)
.formatOptions
- a sequence of properties and their values, used to configure the underlying formatter. Each element must be specified asproperty_name=property_value
, e.g. options={"decimalSeparator=,", "maximumFractionDigits=3"}
-
-