Class ComplexUnitsConverter


  • public class ComplexUnitsConverter
    extends java.lang.Object
    Converts from single or compound unit to single, compound or mixed units. For example, from meter to foot+inch.

    DESIGN: This class uses UnitsConverter in order to perform the single converter (i.e. converters from a single unit to another single unit). Therefore, ComplexUnitsConverter class contains multiple instances of the UnitsConverter to perform the conversion.

    • Field Detail

      • EPSILON

        public static final java.math.BigDecimal EPSILON
      • EPSILON_MULTIPLIER

        public static final java.math.BigDecimal EPSILON_MULTIPLIER
      • unitsConverters_

        public java.util.ArrayList<UnitsConverter> unitsConverters_
    • Constructor Detail

      • ComplexUnitsConverter

        public ComplexUnitsConverter​(MeasureUnitImpl targetUnit,
                                     ConversionRates conversionRates)
        Constructs ComplexUnitsConverter for an inputUnit that could be Single, Compound or Mixed. In case of: 1- Single and Compound units, the conversion will not perform anything, the input will be equal to the output. 2- Mixed Unit the conversion will consider the input in the biggest unit. and will convert it to be spread throw the input units. For example: if input unit is "inch-and-foot", and the input is 2.5. The converter will consider the input value in "foot", because foot is the biggest unit. Then, it will convert 2.5 feet to "inch-and-foot".
        Parameters:
        targetUnit - represents the input unit. could be any type. (single, compound or mixed).
      • ComplexUnitsConverter

        public ComplexUnitsConverter​(java.lang.String inputUnitIdentifier,
                                     java.lang.String outputUnitsIdentifier)
        Constructs ComplexUnitsConverter NOTE: - inputUnit and outputUnits must be under the same category - e.g. meter to feet and inches --> all of them are length units.
        Parameters:
        inputUnitIdentifier - represents the source unit identifier. (should be single or compound unit).
        outputUnitsIdentifier - represents the output unit identifier. could be any type. (single, compound or mixed).
      • ComplexUnitsConverter

        public ComplexUnitsConverter​(MeasureUnitImpl inputUnit,
                                     MeasureUnitImpl outputUnits,
                                     ConversionRates conversionRates)
        Constructs ComplexUnitsConverter NOTE: - inputUnit and outputUnits must be under the same category - e.g. meter to feet and inches --> all of them are length units.
        Parameters:
        inputUnit - represents the source unit. (should be single or compound unit).
        outputUnits - represents the output unit. could be any type. (single, compound or mixed).
        conversionRates - a ConversionRates instance containing the unit conversion rates.
    • Method Detail

      • init

        private void init​(ConversionRates conversionRates)
        Sorts units_, which must be populated before calling this, and populates unitsConverters_.
      • greaterThanOrEqual

        public boolean greaterThanOrEqual​(java.math.BigDecimal quantity,
                                          java.math.BigDecimal limit)
        Returns true if the specified quantity of the inputUnit, expressed in terms of the biggest unit in the MeasureUnit outputUnit, is greater than or equal to limit.

        For example, if the input unit is meter and the target unit is foot+inch. Therefore, this function will convert the quantity from meter to foot, then, it will compare the value in foot with the limit.

      • convert

        public ComplexUnitsConverter.ComplexConverterResult convert​(java.math.BigDecimal quantity,
                                                                    Precision rounder)
        Returns outputMeasures which is an array with the corresponding values. - E.g. converting meters to feet and inches. 1 meter --> 3 feet, 3.3701 inches NOTE: the smallest element is the only element that could have fractional values. And all other elements are floored to the nearest integer
      • applyRounder

        private java.math.BigDecimal applyRounder​(java.util.List<java.math.BigInteger> intValues,
                                                  java.math.BigDecimal quantity,
                                                  Precision rounder)
        Applies the rounder to the quantity (last element) and bubble up any carried value to all the intValues.
        Returns:
        the rounded quantity
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object