Class NumericConversion<T extends Number>
- Type Parameters:
T
- The type of numbers supported by this conversion class.
- All Implemented Interfaces:
Conversion<String,
,T> FormattedConversion<DecimalFormat>
- Direct Known Subclasses:
FormattedBigDecimalConversion
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 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 constructor
The numeric patterns must follows the pattern rules of DecimalFormat
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String[]
private DecimalFormat[]
private final ParsePosition
-
Constructor Summary
ConstructorsConstructorDescriptionDefines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion
(String... numericFormats) Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion
(DecimalFormat... numericFormatters) Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion
(T valueIfStringIsNull, String valueIfObjectIsNull) Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion
(T valueIfStringIsNull, String valueIfObjectIsNull, String... numericFormats) Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns.NumericConversion
(T valueIfStringIsNull, String valueIfObjectIsNull, DecimalFormat... numericFormatters) Defines a conversion from String toNumber
using a sequence of acceptable numeric patterns. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a new numeric pattern to be used to parse input Strings and convert them to numbers.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.protected T
fromString
(String input) Converts a formatted numeric String to an instance of Number.Returns the formatter objectsReturns the implementation ofNumber
that should be used when converting numeric data.Converts Number to a formatted numeric String.void
setNumberType
(Class<? extends 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
-
Field Details
-
formatters
-
formats
-
position
-
numberType
-
-
Constructor Details
-
NumericConversion
public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull, 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, String valueIfObjectIsNull, 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
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
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
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 Details
-
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
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
Description copied from interface:FormattedConversion
Returns the formatter objects- Specified by:
getFormatterObjects
in interfaceFormattedConversion<T extends Number>
- Returns:
- the formatter objects used to apply formatting to values to generate formatted Strings, and parsing formatted Strings into values
-
configureFormatter
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
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 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
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<String,
T extends Number> - Overrides:
revert
in classObjectConversion<T extends 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
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"}
-