Class FunctionalUnit


  • final class FunctionalUnit
    extends Unit
    A unit that uses functional objects to convert to and from its base.
    Since:
    2019-05-22
    Author:
    Adrien Hopkins
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.function.DoubleUnaryOperator converterFrom
      A function that accepts a value expressed in the unit's base and returns that value expressed in this unit.
      private java.util.function.DoubleUnaryOperator converterTo
      A function that accepts a value expressed in the unit and returns that value expressed in the unit's base.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FunctionalUnit​(ObjectProduct<BaseUnit> base, java.util.function.DoubleUnaryOperator converterFrom, java.util.function.DoubleUnaryOperator converterTo)
      Creates the FunctionalUnit.
    • Field Detail

      • converterFrom

        private final java.util.function.DoubleUnaryOperator converterFrom
        A function that accepts a value expressed in the unit's base and returns that value expressed in this unit.
        Since:
        2019-05-22
      • converterTo

        private final java.util.function.DoubleUnaryOperator converterTo
        A function that accepts a value expressed in the unit and returns that value expressed in the unit's base.
        Since:
        2019-05-22
    • Constructor Detail

      • FunctionalUnit

        private FunctionalUnit​(ObjectProduct<BaseUnit> base,
                               java.util.function.DoubleUnaryOperator converterFrom,
                               java.util.function.DoubleUnaryOperator converterTo)
        Creates the FunctionalUnit.
        Parameters:
        base - unit's base
        converterFrom - function that accepts a value expressed in the unit's base and returns that value expressed in this unit.
        converterTo - function that accepts a value expressed in the unit and returns that value expressed in the unit's base.
        Throws:
        java.lang.NullPointerException - if any argument is null
        Since:
        2019-05-22
    • Method Detail

      • valueOf

        public static FunctionalUnit valueOf​(ObjectProduct<BaseUnit> base,
                                             java.util.function.DoubleUnaryOperator converterFrom,
                                             java.util.function.DoubleUnaryOperator converterTo)
        Returns a unit from its base and the functions it uses to convert to and from its base.
        Parameters:
        base - unit's base
        converterFrom - function that accepts a value expressed in the unit's base and returns that value expressed in this unit.
        converterTo - function that accepts a value expressed in the unit and returns that value expressed in the unit's base.
        Returns:
        a unit that uses the provided functions to convert.
        Throws:
        java.lang.NullPointerException - if any argument is null
        Since:
        2019-05-22
      • convertFromBase

        public double convertFromBase​(double value)
        Description copied from class: Unit
        Converts from a value expressed in this unit's base unit to a value expressed in this unit.

        This must be the inverse of convertToBase, so convertFromBase(convertToBase(value)) must be equal to value for any value, ignoring precision loss by roundoff error.

        If this unit is a base unit, this method should return value.

        Specified by:
        convertFromBase in class Unit
        Parameters:
        value - value expressed in base unit
        Returns:
        value expressed in this unit
      • convertToBase

        public double convertToBase​(double value)
        Description copied from class: Unit
        Converts from a value expressed in this unit to a value expressed in this unit's base unit.

        This must be the inverse of convertFromBase, so convertToBase(convertFromBase(value)) must be equal to value for any value, ignoring precision loss by roundoff error.

        If this unit is a base unit, this method should return value.

        Specified by:
        convertToBase in class Unit
        Parameters:
        value - value expressed in this unit
        Returns:
        value expressed in base unit