Package org.unitConverter.unit
Class FunctionalUnit
- java.lang.Object
-
- org.unitConverter.unit.Unit
-
- org.unitConverter.unit.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.DoubleUnaryOperatorconverterFromA function that accepts a value expressed in the unit's base and returns that value expressed in this unit.private java.util.function.DoubleUnaryOperatorconverterToA 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 privateFunctionalUnit(ObjectProduct<BaseUnit> base, java.util.function.DoubleUnaryOperator converterFrom, java.util.function.DoubleUnaryOperator converterTo)Creates theFunctionalUnit.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleconvertFromBase(double value)Converts from a value expressed in this unit's base unit to a value expressed in this unit.doubleconvertToBase(double value)Converts from a value expressed in this unit to a value expressed in this unit's base unit.static FunctionalUnitvalueOf(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.-
Methods inherited from class org.unitConverter.unit.Unit
canConvertTo, convertTo, fromConversionFunctions, getBase, getDimension, toString
-
-
-
-
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 theFunctionalUnit.- Parameters:
base- unit's baseconverterFrom- 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 baseconverterFrom- 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:UnitConverts from a value expressed in this unit's base unit to a value expressed in this unit.This must be the inverse of
convertToBase, soconvertFromBase(convertToBase(value))must be equal tovaluefor any value, ignoring precision loss by roundoff error.If this unit is a base unit, this method should return
value.- Specified by:
convertFromBasein classUnit- Parameters:
value- value expressed in base unit- Returns:
- value expressed in this unit
-
convertToBase
public double convertToBase(double value)
Description copied from class:UnitConverts from a value expressed in this unit to a value expressed in this unit's base unit.This must be the inverse of
convertFromBase, soconvertToBase(convertFromBase(value))must be equal tovaluefor any value, ignoring precision loss by roundoff error.If this unit is a base unit, this method should return
value.- Specified by:
convertToBasein classUnit- Parameters:
value- value expressed in this unit- Returns:
- value expressed in base unit
-
-