T
- The type of numbers supported by this conversion class.public abstract class NumericConversion<T extends Number> extends ObjectConversion<T> implements FormattedConversion<DecimalFormat>
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
DecimalFormat
Constructor and Description |
---|
NumericConversion()
Defines a conversion from String to
Number using a sequence of acceptable numeric patterns. |
NumericConversion(DecimalFormat... numericFormatters)
Defines a conversion from String to
Number using a sequence of acceptable numeric patterns. |
NumericConversion(String... numericFormats)
Defines a conversion from String to
Number using a sequence of acceptable numeric patterns. |
NumericConversion(T valueIfStringIsNull,
String valueIfObjectIsNull)
Defines a conversion from String to
Number using a sequence of acceptable numeric patterns. |
NumericConversion(T valueIfStringIsNull,
String valueIfObjectIsNull,
DecimalFormat... numericFormatters)
Defines a conversion from String to
Number using a sequence of acceptable numeric patterns. |
NumericConversion(T valueIfStringIsNull,
String valueIfObjectIsNull,
String... numericFormats)
Defines a conversion from String to
Number using a sequence of acceptable numeric patterns. |
Modifier and Type | Method and Description |
---|---|
void |
addFormat(String format,
String... formatOptions)
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.
|
DecimalFormat[] |
getFormatterObjects()
Returns the formatter objects
|
Class<? extends Number> |
getNumberType()
Returns the implementation of
Number that should be used when converting numeric data. |
String |
revert(T input)
Converts Number to a formatted numeric String.
|
void |
setNumberType(Class<? extends Number> numberType)
Defines a specific implementation of
Number that should be used when converting numeric data. |
execute, fromInput, getValueIfObjectIsNull, getValueIfStringIsNull, setValueIfObjectIsNull, setValueIfStringIsNull, undo
getValueOnNullInput, getValueOnNullOutput, setValueOnNullInput, setValueOnNullOutput
public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull, String... numericFormats)
Number
using a sequence of acceptable numeric patterns.
This constructor assumes the output of a conversion should be null when input is nullvalueIfStringIsNull
- default Number to be returned when the input String is null. Used when ObjectConversion.execute(String)
is invoked.valueIfObjectIsNull
- default String value to be returned when a Number input is null. Used when revert(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 in revert(Number)
.public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull, DecimalFormat... numericFormatters)
Number
using a sequence of acceptable numeric patterns.valueIfStringIsNull
- default Number to be returned when the input String is null. Used when ObjectConversion.execute(String)
is invoked.valueIfObjectIsNull
- default String value to be returned when a Number input is null. Used when revert(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 in revert(Number)
.public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull)
Number
using a sequence of acceptable numeric patterns. The patterns
must be added to this conversion class through the addFormat(String, String...)
method.valueIfStringIsNull
- default Number to be returned when the input String is null. Used when ObjectConversion.execute(String)
is invoked.valueIfObjectIsNull
- default String value to be returned when a Number input is null. Used when revert(Number)
is invoked.public NumericConversion(String... numericFormats)
Number
using a sequence of acceptable numeric patterns.
This constructor assumes the output of a conversion should be null when input is nullnumericFormats
- list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String in revert(Number)
.public NumericConversion(DecimalFormat... numericFormatters)
Number
using a sequence of acceptable numeric patterns.
This constructor assumes the output of a conversion should be null when input is nullnumericFormatters
- list formatters of acceptable numeric patterns. The first formatter in this sequence will be used to convert a Number into a String in revert(Number)
.public NumericConversion()
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 nullpublic Class<? extends Number> getNumberType()
Number
that should be used when converting numeric data.Number
that should be used when converting numeric data.public void setNumberType(Class<? extends Number> numberType)
Number
that should be used when converting numeric data.numberType
- the implementation of Number
that should be used when converting numeric data.public DecimalFormat[] getFormatterObjects()
FormattedConversion
getFormatterObjects
in interface FormattedConversion<DecimalFormat>
protected abstract void configureFormatter(DecimalFormat formatter)
formatter
- a DecimalFormat instance initialized with one of the patterns provided in the constructor of this class.protected T fromString(String input)
The pattern in the formatted input must match one of the numeric patterns provided in the constructor of this class.
fromString
in class ObjectConversion<T extends Number>
input
- the String containing a formatted number which must be converted to a numberObjectConversion.getValueIfStringIsNull()
if the String input is null.public String revert(T input)
The pattern used to generate the formatted number is the first numeric pattern provided in the constructor of this class
revert
in interface Conversion<String,T extends Number>
revert
in class ObjectConversion<T extends Number>
input
- the Number to be converted to a StringObjectConversion.getValueIfObjectIsNull()
if the Number parameter is null.public void addFormat(String format, String... formatOptions)
format
- a numeric pattern. The first pattern added to this class will be used to convert a Number into a String in revert(Number)
.formatOptions
- 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"}Copyright © 2024 uniVocity Software Pty Ltd. All rights reserved.