Class LinearUnitConverter

  • All Implemented Interfaces:
    UnitConverter

    public class LinearUnitConverter
    extends Object
    implements UnitConverter
    A linear unit converter is a converter implementation were all units can be converted to another unit using a multiplication factor and/or an offset. The formula used in this converter is:

    value(ref) = value(specific) * factor + offset

    Version:
    2.9
    Author:
    Nicklas
    Last modified
    $Date$
    • Field Detail

      • factor

        private final double factor
      • offset

        private final double offset
    • Constructor Detail

      • LinearUnitConverter

        public LinearUnitConverter​(double factor,
                                   double offset)
        Create a new unit converter.
        Parameters:
        factor - The factor to use when converting values
        offset - The offset to use when converting values
      • LinearUnitConverter

        public LinearUnitConverter​(double factor,
                                   double offset,
                                   double referenceFactor,
                                   double referenceOffset)
        Create a unit converter that converts between two units that are both related to a third "common" unit by given factors and offsets. The formula used is: value(ref) * factor(ref) + offset(ref) == value(specific) * factor(specfic) + offset(specific) which is equivalent to :
        factor = factor(specific) / factor(ref)
        offset = (offset(specific) - offset(ref)) / factor(ref)
        
        Parameters:
        factor - The factor of the specific unit
        offset - The offset of the specific unit
        referenceFactor - The factor of the reference unit
        referenceOffset - The offset of the specific unit
      • LinearUnitConverter

        public LinearUnitConverter​(Unit unit)
        Create a new unit converter that converts to/from a given unit. The reference unit is the unit given by Quantity.getReferenceUnit() for the given specific unit's quantity.
        Parameters:
        unit - The specific unit
      • LinearUnitConverter

        public LinearUnitConverter​(Unit specific,
                                   Unit reference)
        Create a new unit converter that converts to/from given specific and reference unit.
        Parameters:
        specific - The specific unit
        reference - The reference unit
    • Method Detail

      • convertToReferenceUnit

        public double convertToReferenceUnit​(double specific)
        value(ref) = value(specific) * factor + offset
        Specified by:
        convertToReferenceUnit in interface UnitConverter
        Parameters:
        specific - The value to convert
        Returns:
        The converted value
      • convertToSpecificUnit

        public double convertToSpecificUnit​(double ref)
        value(specific) = (value(ref) - offset) / factor
        Specified by:
        convertToSpecificUnit in interface UnitConverter
        Parameters:
        ref - The reference value
        Returns:
        The converted value