Class RationalConverter

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<javax.measure.UnitConverter>, java.util.function.DoubleSupplier, java.util.function.Supplier<java.lang.Double>, javax.measure.UnitConverter, tech.uom.lib.common.function.ValueSupplier<java.lang.Double>

    public final class RationalConverter
    extends AbstractConverter
    implements tech.uom.lib.common.function.ValueSupplier<java.lang.Double>, java.util.function.Supplier<java.lang.Double>, java.util.function.DoubleSupplier

    This class represents a converter multiplying numeric values by an exact scaling factor (represented as the quotient of two BigInteger numbers).

    Since:
    1.0
    Version:
    1.0, Oct 11, 2016
    See Also:
    Serialized Form
    • Field Detail

      • dividend

        private final java.math.BigInteger dividend
        Holds the converter dividend.
      • divisor

        private final java.math.BigInteger divisor
        Holds the converter divisor (always positive).
    • Constructor Detail

      • RationalConverter

        public RationalConverter​(java.math.BigInteger dividend,
                                 java.math.BigInteger divisor)
        Creates a rational converter with the specified dividend and divisor.
        Parameters:
        dividend - the dividend.
        divisor - the positive divisor.
        Throws:
        java.lang.IllegalArgumentException - if divisor <= 0
      • RationalConverter

        public RationalConverter​(long dividend,
                                 long divisor)
        Convenience method equivalent to new RationalConverter(BigInteger.valueOf(dividend), BigInteger.valueOf(divisor))
        Parameters:
        dividend - the dividend.
        divisor - the positive divisor.
        Throws:
        java.lang.IllegalArgumentException - if divisor <= 0
        java.lang.IllegalArgumentException - if dividend == divisor
    • Method Detail

      • of

        public static RationalConverter of​(java.math.BigInteger dividend,
                                           java.math.BigInteger divisor)
        Convenience method equivalent to new RationalConverter(dividend, divisor)
        Parameters:
        dividend - the dividend.
        divisor - the positive divisor.
        Throws:
        java.lang.IllegalArgumentException - if divisor <= 0
        java.lang.IllegalArgumentException - if dividend == divisor
      • of

        public static RationalConverter of​(long dividend,
                                           long divisor)
        Convenience method equivalent to new RationalConverter(dividend, divisor)
        Parameters:
        dividend - the dividend.
        divisor - the positive divisor.
        Throws:
        java.lang.IllegalArgumentException - if divisor <= 0
        java.lang.IllegalArgumentException - if dividend == divisor
      • of

        public static RationalConverter of​(double dividend,
                                           double divisor)
        Convenience method equivalent to new RationalConverter(BigDecimal.valueOf(dividend).toBigInteger(), BigDecimal.valueOf(divisor).toBigInteger())
        Parameters:
        dividend - the dividend.
        divisor - the positive divisor.
        Throws:
        java.lang.IllegalArgumentException - if divisor <= 0
        java.lang.IllegalArgumentException - if dividend == divisor
      • getDividend

        public java.math.BigInteger getDividend()
        Returns the integer dividend for this rational converter.
        Returns:
        this converter dividend.
      • getDivisor

        public java.math.BigInteger getDivisor()
        Returns the integer (positive) divisor for this rational converter.
        Returns:
        this converter divisor.
      • toDouble

        private static double toDouble​(java.math.BigInteger integer)
      • convertWhenNotIdentity

        protected java.lang.Number convertWhenNotIdentity​(java.math.BigInteger value,
                                                          java.math.MathContext ctx)
        Description copied from class: AbstractConverter
        Non-API
        Overrides:
        convertWhenNotIdentity in class AbstractConverter
        Returns:
        transformed value (most likely a BigInteger or BigDecimal)
      • convertWhenNotIdentity

        public java.math.BigDecimal convertWhenNotIdentity​(java.math.BigDecimal value,
                                                           java.math.MathContext ctx)
                                                    throws java.lang.ArithmeticException
        Description copied from class: AbstractConverter
        Non-API
        Specified by:
        convertWhenNotIdentity in class AbstractConverter
        Returns:
        transformed value
        Throws:
        java.lang.ArithmeticException
      • isIdentity

        public boolean isIdentity()
        Specified by:
        isIdentity in interface javax.measure.UnitConverter
      • inverseWhenNotIdentity

        public RationalConverter inverseWhenNotIdentity()
        Description copied from class: AbstractConverter
        Non-API

        Returns an AbstractConverter that represents the inverse transformation of this converter, for cases where the transformation is not the identity transformation.

        Specified by:
        inverseWhenNotIdentity in class AbstractConverter
        Returns:
      • transformationLiteral

        public final java.lang.String transformationLiteral()
        Description copied from class: AbstractConverter
        Non-API

        Returns a String describing the transformation that is represented by this converter. Contributes to converter's toString method. If null or empty toString output becomes simplified.

        Specified by:
        transformationLiteral in class AbstractConverter
        Returns:
      • isLinear

        public boolean isLinear()
        Specified by:
        isLinear in interface javax.measure.UnitConverter
      • getValue

        public java.lang.Double getValue()
        Specified by:
        getValue in interface tech.uom.lib.common.function.ValueSupplier<java.lang.Double>
      • getAsDouble

        public double getAsDouble()
        Specified by:
        getAsDouble in interface java.util.function.DoubleSupplier
      • get

        public java.lang.Double get()
        Specified by:
        get in interface java.util.function.Supplier<java.lang.Double>
      • compareTo

        public int compareTo​(javax.measure.UnitConverter o)
        Specified by:
        compareTo in interface java.lang.Comparable<javax.measure.UnitConverter>