Class UnitConverter

java.lang.Object
de.turnertech.measures.UnitConverter

public class UnitConverter extends Object
The UnitConverter handles specialist conversions with more accuracy than would be possible via conversions to base units. For example if converting from a unit like Kilometer to Nautical Mile, it is possble that converting first to meters would cause overrun errors or lose accuracy due to binary limitations. 1. Functions 2. Scalars 3. Divisors (Scalar) 4. Unit Base Unit
  • Constructor Details

    • UnitConverter

      public UnitConverter()
  • Method Details

    • putScalar

      public static Double putScalar(Unit unitIn, Unit unitOut, double scalar)

      Adds a scalar function for transitioning from the unitIn, to the unitOut. This scalar will also be used as a divisor in the reverse direction.

      Parameters:
      unitIn - the Unit in which the in parameter is represented.
      unitOut - the desired Unit which should be present in the returned.
      scalar - the scalar.
      Returns:
      the result of Map.put(...)
    • putFunction

      public static DoubleUnaryOperator putFunction(Unit unitIn, Unit unitOut, DoubleUnaryOperator function)

      Adds a conversion function for transitioning from the unitIn, to the unitOut. This should be the highest accuracy possible and will be prioritised over other conversion methods.

      Parameters:
      unitIn - the Unit in which the in parameter is represented.
      unitOut - the desired Unit which should be present in the returned.
      function - the conversion function.
      Returns:
      the response to Map.put(...)
    • convert

      public static Measure convert(Measure in, Unit unitOut)

      Converts between Units using one of a number of conversion options. The conversion method used is prioritised to enable accuracy.

      Parameters:
      in - Measure to convert.
      unitOut - Desired output Unit
      Returns:
      a measure containing the in parameter represented in the unitOut.
    • convert

      public static Measure convert(double in, Unit unitIn, Unit unitOut)

      Converts between Units using one of a number of conversion options. The conversion method used is prioritised to enable accuracy.

      1. Conversion Function stored in this class
      2. Conversion Scalar stored in this class
      3. Conversion Divisor using the Scalar stored in this class
      4. Conversion to/from the base Units stored in the Unit class
      Parameters:
      in - value to convert between Units.
      unitIn - the Unit in which the in parameter is represented.
      unitOut - the desired Unit which should be present in the returned measure
      Returns:
      a measure containing the in parameter represented in the unitOut.
      Throws:
      UnsupportedOperationException - if conversion is not supported