Package net.sf.basedb.util.units
Class LinearUnitConverter
java.lang.Object
net.sf.basedb.util.units.LinearUnitConverter
- All Implemented Interfaces:
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 Summary
-
Constructor Summary
ConstructorDescriptionLinearUnitConverter
(double factor, double offset) Create a new unit converter.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.LinearUnitConverter
(Unit unit) Create a new unit converter that converts to/from a given unit.LinearUnitConverter
(Unit specific, Unit reference) Create a new unit converter that converts to/from given specific and reference unit. -
Method Summary
Modifier and TypeMethodDescriptiondouble
convertToReferenceUnit
(double specific) value(ref) = value(specific) * factor + offsetdouble
convertToSpecificUnit
(double ref) value(specific) = (value(ref) - offset) / factor
-
Field Details
-
factor
private final double factor -
offset
private final double offset
-
-
Constructor Details
-
LinearUnitConverter
public LinearUnitConverter(double factor, double offset) Create a new unit converter.- Parameters:
factor
- The factor to use when converting valuesoffset
- 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 unitoffset
- The offset of the specific unitreferenceFactor
- The factor of the reference unitreferenceOffset
- The offset of the specific unit
-
LinearUnitConverter
Create a new unit converter that converts to/from a given unit. The reference unit is the unit given byQuantity.getReferenceUnit()
for the given specific unit's quantity.- Parameters:
unit
- The specific unit
-
LinearUnitConverter
Create a new unit converter that converts to/from given specific and reference unit.- Parameters:
specific
- The specific unitreference
- The reference unit
-
-
Method Details
-
convertToReferenceUnit
public double convertToReferenceUnit(double specific) value(ref) = value(specific) * factor + offset- Specified by:
convertToReferenceUnit
in interfaceUnitConverter
- 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 interfaceUnitConverter
- Parameters:
ref
- The reference value- Returns:
- The converted value
-