public abstract class Unit
extends java.lang.Object
Modifier | Constructor and Description |
---|---|
protected |
Unit(ObjectProduct<BaseUnit> unitBase,
NameSymbol ns)
Creates the
AbstractUnit . |
Modifier and Type | Method and Description |
---|---|
boolean |
canConvertTo(Unit other)
Checks if a value expressed in this unit can be converted to a value expressed in
other |
protected abstract double |
convertFromBase(double value)
Converts from a value expressed in this unit's base unit to a value expressed in this unit.
|
double |
convertTo(Unit other,
double value)
Converts a value expressed in this unit to a value expressed in
other . |
protected abstract double |
convertToBase(double value)
Converts from a value expressed in this unit to a value expressed in this unit's base unit.
|
static Unit |
fromConversionFunctions(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.
|
static Unit |
fromConversionFunctions(ObjectProduct<BaseUnit> base,
java.util.function.DoubleUnaryOperator converterFrom,
java.util.function.DoubleUnaryOperator converterTo,
NameSymbol ns)
Returns a unit from its base and the functions it uses to convert to and from its base.
|
ObjectProduct<BaseUnit> |
getBase() |
ObjectProduct<BaseDimension> |
getDimension() |
java.util.Set<java.lang.String> |
getOtherNames() |
java.util.Optional<java.lang.String> |
getPrimaryName() |
java.util.Optional<java.lang.String> |
getSymbol() |
java.lang.String |
toString() |
Unit |
withName(NameSymbol ns) |
protected Unit(ObjectProduct<BaseUnit> unitBase, NameSymbol ns)
AbstractUnit
.unitBase
- base of unitns
- names and symbol of unitjava.lang.NullPointerException
- if unitBase or ns is nullpublic static final Unit fromConversionFunctions(ObjectProduct<BaseUnit> base, java.util.function.DoubleUnaryOperator converterFrom, java.util.function.DoubleUnaryOperator converterTo)
For example, to get a unit representing the degree Celsius, the following code can be used:
Unit.fromConversionFunctions(SI.KELVIN, tempK -> tempK - 273.15, tempC -> tempC + 273.15);
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.java.lang.NullPointerException
- if any argument is nullpublic static final Unit fromConversionFunctions(ObjectProduct<BaseUnit> base, java.util.function.DoubleUnaryOperator converterFrom, java.util.function.DoubleUnaryOperator converterTo, NameSymbol ns)
For example, to get a unit representing the degree Celsius, the following code can be used:
Unit.fromConversionFunctions(SI.KELVIN, tempK -> tempK - 273.15, tempC -> tempC + 273.15);
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.ns
- names and symbol of unitjava.lang.NullPointerException
- if any argument is nullpublic final boolean canConvertTo(Unit other)
other
other
- unit to test withjava.lang.NullPointerException
- if other is nullprotected abstract double convertFromBase(double value)
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
.
value
- value expressed in base unitconvertTo(org.unitConverter.unit.Unit, double)
, and its behaviour affects the behaviour of
convertTo
.public final double convertTo(Unit other, double value)
other
.other
- unit to convert tovalue
- value to convertjava.lang.IllegalArgumentException
- if other
is incompatible for conversion with this unit (as tested by
canConvertTo(org.unitConverter.unit.Unit)
).java.lang.NullPointerException
- if other is nullother.convertFromBase(this.convertToBase(value))
. Therefore, overriding either of those methods
will change the output of this method.protected abstract double convertToBase(double value)
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
.
value
- value expressed in this unitconvertTo(org.unitConverter.unit.Unit, double)
, and its behaviour affects the behaviour of
convertTo
.public final ObjectProduct<BaseUnit> getBase()
public final ObjectProduct<BaseDimension> getDimension()
public final java.util.Set<java.lang.String> getOtherNames()
public final java.util.Optional<java.lang.String> getPrimaryName()
public final java.util.Optional<java.lang.String> getSymbol()
public java.lang.String toString()
toString
in class java.lang.Object
public Unit withName(NameSymbol ns)
ns
- name(s) and symbol to usejava.lang.NullPointerException
- if ns is null