Class NumericConversion<T extends java.lang.Number>

  • 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 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:
    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 to Number using a sequence of acceptable numeric patterns.
      NumericConversion​(java.lang.String... numericFormats)
      Defines a conversion from String to Number using a sequence of acceptable numeric patterns.
      NumericConversion​(java.text.DecimalFormat... numericFormatters)
      Defines a conversion from String to Number using a sequence of acceptable numeric patterns.
      NumericConversion​(T valueIfStringIsNull, java.lang.String valueIfObjectIsNull)
      Defines a conversion from String to Number using a sequence of acceptable numeric patterns.
      NumericConversion​(T valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... numericFormats)
      Defines a conversion from String to Number using a sequence of acceptable numeric patterns.
      NumericConversion​(T valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.text.DecimalFormat... numericFormatters)
      Defines a conversion from String to Number using a sequence of acceptable numeric patterns.
    • Field Detail

      • formatters

        private java.text.DecimalFormat[] formatters
      • formats

        private java.lang.String[] formats
      • position

        private final java.text.ParsePosition position
      • numberType

        private java.lang.Class<? extends java.lang.Number> numberType
    • Constructor Detail

      • NumericConversion

        public NumericConversion​(T valueIfStringIsNull,
                                 java.lang.String valueIfObjectIsNull,
                                 java.lang.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,
                                 java.lang.String valueIfObjectIsNull,
                                 java.text.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,
                                 java.lang.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​(java.lang.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​(java.text.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 Detail

      • getNumberType

        public java.lang.Class<? extends java.lang.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​(java.lang.Class<? extends java.lang.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 java.text.DecimalFormat[] getFormatterObjects()
        Description copied from interface: FormattedConversion
        Returns the formatter objects
        Specified by:
        getFormatterObjects in interface FormattedConversion<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 class ObjectConversion<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 interface Conversion<java.lang.String,​T extends java.lang.Number>
        Overrides:
        revert in class ObjectConversion<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 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"}