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

public abstract class NumericConversion<T extends Number> extends ObjectConversion<T> implements FormattedConversion<DecimalFormat>
Converts Strings to instances of 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 Details

  • Constructor Details

    • NumericConversion

      public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull, String... numericFormats)
      Defines a conversion from String to 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 - 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).
    • NumericConversion

      public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull, DecimalFormat... numericFormatters)
      Defines a conversion from String to Number using a sequence of acceptable numeric patterns.
      Parameters:
      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).
    • NumericConversion

      public NumericConversion(T valueIfStringIsNull, String valueIfObjectIsNull)
      Defines a conversion from String to 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 - 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.
    • NumericConversion

      public NumericConversion(String... numericFormats)
      Defines a conversion from String to 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 - list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String in revert(Number).
    • NumericConversion

      public NumericConversion(DecimalFormat... numericFormatters)
      Defines a conversion from String to 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 - 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).
    • NumericConversion

      public NumericConversion()
      Defines a conversion from String to 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 Details

    • getNumberType

      public Class<? extends Number> getNumberType()
      Returns the implementation of Number 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(Class<? extends Number> numberType)
      Defines a specific implementation of Number that should be used when converting numeric data.
      Parameters:
      numberType - the implementation of Number that should be used when converting numeric data.
    • getFormatterObjects

      public DecimalFormat[] getFormatterObjects()
      Description copied from interface: FormattedConversion
      Returns the formatter objects
      Specified by:
      getFormatterObjects in interface FormattedConversion<T extends 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(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(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 class ObjectConversion<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

      public 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 interface Conversion<String,T extends Number>
      Overrides:
      revert in class ObjectConversion<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

      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 - 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"}