Class AbstractConverter
Object
AbstractConverter
- All Implemented Interfaces:
Serializable
,UnitConverter
Skeletal implementation of the
UnitConverter
interface for reducing implementation effort.
This class makes easier to define a non-linear conversion between two units of measurement.
Note that for linear conversions, the standard Unit.shift(Number)
, Unit.multiply(Number)
and Unit.divide(Number)
methods should be used instead.
After a non-linear conversion has been created, a new unit of measurement using that conversion
can be defined by a call to Unit.transform(UnitConverter)
.
See Units.logarithm(Unit)
for an example.
- Since:
- 1.5
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionconcatenate
(UnitConverter before) Concatenates this converter with another converter.Performs a unit conversion on the given number.abstract double
derivative
(double value) Returns the derivative of the conversion function at the given value, orNaN
if unknown.Returns the steps of fundamental converters making up this converter.boolean
Returnstrue
ifUnitConverter.convert(double)
returns given values unchanged.boolean
Indicates if this converter is linear in JSR-385 sense (not the usual mathematical sense).Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface UnitConverter
convert, inverse
-
Constructor Details
-
AbstractConverter
protected AbstractConverter()Creates a new converter.
-
-
Method Details
-
isIdentity
public boolean isIdentity()Returnstrue
ifUnitConverter.convert(double)
returns given values unchanged. The default implementation returnsfalse
for convenience of non-linear conversions. Subclasses should override if their conversions may be identity.- Specified by:
isIdentity
in interfaceUnitConverter
-
isLinear
public boolean isLinear()Indicates if this converter is linear in JSR-385 sense (not the usual mathematical sense). The default implementation returnsfalse
for convenience of non-linear conversions. Subclasses should override if their conversions may be identity.- Specified by:
isLinear
in interfaceUnitConverter
-
convert
Performs a unit conversion on the given number. The default implementation delegates to the version working ondouble
primitive type, so it may not provide the accuracy normally required by this method contract. Linear conversions should override this method.- Specified by:
convert
in interfaceUnitConverter
-
derivative
public abstract double derivative(double value) Returns the derivative of the conversion function at the given value, orNaN
if unknown.- Parameters:
value
- the point at which to compute the derivative. Ignored (can beDouble.NaN
) if the conversion is linear.
-
concatenate
Concatenates this converter with another converter. The resulting converter is equivalent to first converting by the specified converter (right converter), and then converting by this converter (left converter).The default implementation is okay, but subclasses should override if they can detect optimizations.
- Specified by:
concatenate
in interfaceUnitConverter
- Parameters:
before
- the converter to concatenate before this converter.- Returns:
- a conversion which applies
before
first, thenthis
.
-
getConversionSteps
Returns the steps of fundamental converters making up this converter. The default implementation returns onlythis
on the assumption that this conversion is not a concatenation of other converters. Subclasses should override if this assumption does not hold.- Specified by:
getConversionSteps
in interfaceUnitConverter
- Returns:
- list of steps in the unit conversion represented by this instance.
-