Class AbstractConverter

Object
AbstractConverter
All Implemented Interfaces:
Serializable, Unit­Converter

public abstract class AbstractConverter extends Object implements UnitConverter, Serializable
Skeletal implementation of the Unit­Converter 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(Unit­Converter). See Units​.logarithm(Unit) for an example.

Since:
1.5
See Also:
  • Constructor Details

    • AbstractConverter

      protected AbstractConverter()
      Creates a new converter.
  • Method Details

    • isIdentity

      public boolean isIdentity()
      Returns true if Unit­Converter​.convert(double) returns given values unchanged. The default implementation returns false for convenience of non-linear conversions. Subclasses should override if their conversions may be identity.
      Specified by:
      is­Identity in interface Unit­Converter
    • isLinear

      public boolean isLinear()
      Indicates if this converter is linear in JSR-385 sense (not the usual mathematical sense). The default implementation returns false for convenience of non-linear conversions. Subclasses should override if their conversions may be identity.
      Specified by:
      is­Linear in interface Unit­Converter
    • convert

      public Number convert(Number value)
      Performs a unit conversion on the given number. The default implementation delegates to the version working on double 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 interface Unit­Converter
    • derivative

      public abstract double derivative(double value)
      Returns the derivative of the conversion function at the given value, or Na­N if unknown.
      Parameters:
      value - the point at which to compute the derivative. Ignored (can be Double​.Na­N) if the conversion is linear.
    • concatenate

      public UnitConverter concatenate(UnitConverter before)
      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 interface Unit­Converter
      Parameters:
      before - the converter to concatenate before this converter.
      Returns:
      a conversion which applies before first, then this.
    • getConversionSteps

      public List<UnitConverter> getConversionSteps()
      Returns the steps of fundamental converters making up this converter. The default implementation returns only this on the assumption that this conversion is not a concatenation of other converters. Subclasses should override if this assumption does not hold.
      Specified by:
      get­Conversion­Steps in interface Unit­Converter
      Returns:
      list of steps in the unit conversion represented by this instance.