Module life.expert

Class RoundedAmount

  • All Implemented Interfaces:
    java.lang.Comparable<Quantity>, Quantity

    public final class RoundedAmount
    extends java.lang.Object
    implements Quantity, java.lang.Comparable<Quantity>
    Default immutable implementation of Quantity based on BigDecimal for the numeric representation. As required by Quantity this class is final, thread-safe, immutable and serializable.
    • Field Detail

      • DEFAULT_CONTEXT

        public static final AmountContext DEFAULT_CONTEXT
        The constant DEFAULT_CONTEXT.
    • Constructor Detail

      • RoundedAmount

        public RoundedAmount​(java.lang.Number number,
                             Unit unit)
        Creates a new instance os RoundedAmount.
        Parameters:
        number - the amount, not null.
        unit - the unit, not null.
      • RoundedAmount

        @Deprecated
        public RoundedAmount​(java.lang.Number number,
                             Unit unit,
                             java.math.MathContext mathContext)
        Deprecated.
        Instantiates a new Rounded unit.
        Parameters:
        number - the number
        unit - the unit
        mathContext - the math context
      • RoundedAmount

        public RoundedAmount​(java.lang.Number number,
                             Unit unit,
                             AmountContext mathContext)
        Instantiates a new Rounded amount.
        Parameters:
        number - the number
        unit - the unit
        mathContext - the math context
      • RoundedAmount

        public RoundedAmount​(java.lang.Number number,
                             Unit unit,
                             Operator rounding)
        Creates a new instance os RoundedAmount.
        Parameters:
        number - the amount, not null.
        unit - , not null.
        rounding - the rounding
      • RoundedAmount

        @Deprecated
        public RoundedAmount​(java.lang.Number number,
                             Unit unit,
                             AmountContext context,
                             Operator rounding)
        Deprecated.
        Instantiates a new Rounded unit.
        Parameters:
        number - the number
        unit - the unit
        context - the context
        rounding - the rounding
    • Method Detail

      • ulp

        public RoundedAmount ulp()
        Ulp rounded unit.
        Returns:
        the rounded unit
      • pow

        public RoundedAmount pow​(int n)
        Pow rounded unit.
        Parameters:
        n - the n
        Returns:
        the rounded unit
      • with

        public RoundedAmount with​(java.lang.Number amount)
        With rounded unit.
        Parameters:
        amount - the amount
        Returns:
        the rounded unit
      • with

        public RoundedAmount with​(Unit unit)
        Creates a new Amount instance, by just replacing the Unit.
        Parameters:
        unit - the unit unit to be replaced, not null
        Returns:
        the new amount with the same numeric value and MathContext, but the new Unit.
      • with

        public RoundedAmount with​(Unit unit,
                                  java.lang.Number amount)
        With rounded unit.
        Parameters:
        unit - the unit
        amount - the amount
        Returns:
        the rounded unit
      • getScale

        public int getScale()
        Gets scale.
        Returns:
        the scale
      • getPrecision

        public int getPrecision()
        Gets precision.
        Returns:
        the precision
      • asType

        @Deprecated
        public <T> T asType​(java.lang.Class<T> type)
        Deprecated.
        As type t.
        Type Parameters:
        T - the type parameter
        Parameters:
        type - the type
        Returns:
        the t
      • asType

        @Deprecated
        public <T> T asType​(java.lang.Class<T> type,
                            Operator adjuster)
        Deprecated.
        As type t.
        Type Parameters:
        T - the type parameter
        Parameters:
        type - the type
        adjuster - the adjuster
        Returns:
        the t
      • asNumberStripped

        public java.math.BigDecimal asNumberStripped()
        Method that returns BigDecimal.ZERO, if isZero(), and #number stripTrailingZeros() in all other cases.
        Returns:
        the stripped number value.
      • of

        public static RoundedAmount of​(java.math.BigDecimal number,
                                       Unit unit)
        Translates a BigDecimal value and a Unit unit into a Amount.
        Parameters:
        number - numeric value of the Amount.
        unit - unit unit of the Amount.
        Returns:
        a Amount combining the numeric value and unit unit.
      • of

        public static RoundedAmount of​(java.math.BigDecimal number,
                                       Unit unit,
                                       Operator rounding)
        Translates a BigDecimal value and a Unit unit into a Amount.
        Parameters:
        number - numeric value of the Amount.
        unit - unit unit of the Amount.
        rounding - The rounding to be applied.
        Returns:
        a Amount combining the numeric value and unit unit.
      • of

        public static RoundedAmount of​(java.math.BigDecimal number,
                                       Unit unit,
                                       java.math.MathContext mathContext)
        Translates a BigDecimal value and a Unit unit into a Amount.
        Parameters:
        number - numeric value of the Amount.
        unit - unit unit of the Amount.
        mathContext - the MathContext to be used.
        Returns:
        a Amount combining the numeric value and unit unit.
      • of

        public static RoundedAmount of​(java.lang.Number number,
                                       Unit unit,
                                       Operator rounding)
        Static factory method for creating a new instance of RoundedAmount .
        Parameters:
        number - The numeric part, not null.
        unit - The target unit, not null.
        rounding - The rounding to be applied.
        Returns:
        A new instance of RoundedAmount.
      • of

        public static RoundedAmount of​(java.lang.Number number,
                                       Unit unit,
                                       AmountContext amountContext)
        Static factory method for creating a new instance of RoundedAmount .
        Parameters:
        number - The numeric part, not null.
        unit - The target unit, not null.
        amountContext - the monetary context
        Returns:
        A new instance of RoundedAmount.
      • of

        @Deprecated
        public static RoundedAmount of​(Unit unit,
                                       java.lang.Number number,
                                       AmountContext amountContext,
                                       Operator rounding)
        Deprecated.
        Static factory method for creating a new instance of RoundedAmount .
        Parameters:
        unit - The target unit, not null.
        number - The numeric part, not null.
        amountContext - the AmountContext to be used.
        rounding - The rounding to be applied.
        Returns:
        A new instance of RoundedAmount.
      • of

        @Deprecated
        public static RoundedAmount of​(java.lang.Number number,
                                       java.lang.String unitCode)
        Deprecated.
        Static factory method for creating a new instance of RoundedAmount .
        Parameters:
        number - The numeric part, not null.
        unitCode - The target unit as ISO unit code.
        Returns:
        A new instance of RoundedAmount.
      • of

        public static RoundedAmount of​(java.lang.Number number,
                                       java.lang.String unitCode,
                                       Operator rounding)
        Static factory method for creating a new instance of RoundedAmount .
        Parameters:
        number - The numeric part, not null.
        unitCode - The target unit as ISO unit code.
        rounding - The rounding to be applied.
        Returns:
        A new instance of RoundedAmount.
      • of

        @Deprecated
        public static RoundedAmount of​(java.lang.Number number,
                                       java.lang.String unitCode,
                                       AmountContext amountContext)
        Deprecated.
        Static factory method for creating a new instance of RoundedAmount .
        Parameters:
        number - The numeric part, not null.
        unitCode - The target unit as ISO unit code.
        amountContext - the monetary context
        Returns:
        A new instance of RoundedAmount.
      • of

        public static RoundedAmount of​(java.lang.String unitCode,
                                       java.lang.Number number,
                                       AmountContext amountContext,
                                       Operator rounding)
        Static factory method for creating a new instance of RoundedAmount .
        Parameters:
        unitCode - The target unit as ISO unit code.
        number - The numeric part, not null.
        amountContext - the monetary context
        rounding - The rounding to be applied.
        Returns:
        A new instance of RoundedAmount.
      • zero

        public static RoundedAmount zero​(Unit unit)
        Obtains an instance of RoundedAmount representing zero.
        Parameters:
        unit - the unit, not null.
        Returns:
        an instance of RoundedAmount representing zero.
        Since:
        1.0.1
      • from

        public static RoundedAmount from​(Quantity amt)
        From rounded unit.
        Parameters:
        amt - the amt
        Returns:
        the rounded unit
      • parse

        public static RoundedAmount parse​(java.lang.CharSequence text)
        Obtains an instance of RoundedAmount from a text string such as 'EUR 25.25'.
        Parameters:
        text - the input text, not null.
        Returns:
        RoundedAmount instance
        Throws:
        java.lang.NumberFormatException - if the amount is not a number
        UnknownUnitException - the unit cannot be resolved
      • getUnit

        public Unit getUnit()
        Gets unit.
        Specified by:
        getUnit in interface Quantity
        Returns:
        the unit
      • getContext

        public AmountContext getContext()
        Access the MathContext used by this instance.
        Specified by:
        getContext in interface Quantity
        Returns:
        the MathContext used, never null.
      • abs

        public RoundedAmount abs()
        Description copied from interface: Quantity
        Returns a Quantity whose value is the absolute value of this Quantity, and whose scale is this.scale().
        Specified by:
        abs in interface Quantity
        Returns:
        abs(this
      • add

        public RoundedAmount add​(Quantity amount)
        Add rounded unit.
        Specified by:
        add in interface Quantity
        Parameters:
        amount - the amount
        Returns:
        the rounded unit
      • divide

        public RoundedAmount divide​(java.lang.Number divisor)
        Description copied from interface: Quantity
        Returns a Quantity whose value is
        this /
         divisor
        , and whose preferred scale is
        this.scale() -
         divisor.scale()
        ; if the exact quotient cannot be represented an ArithmeticException is thrown.
        Specified by:
        divide in interface Quantity
        Parameters:
        divisor - value by which this Quantity is to be divided.
        Returns:
        this / divisor
      • divideToIntegralValue

        public RoundedAmount divideToIntegralValue​(java.lang.Number divisor)
        Description copied from interface: Quantity
        Returns a Quantity whose value is the integer part of the quotient
        this / divisor
        rounded down. The preferred scale of the result is
        this.scale() -
         divisor.scale()
        .
        Specified by:
        divideToIntegralValue in interface Quantity
        Parameters:
        divisor - value by which this BigDecimal is to be divided.
        Returns:
        The integer part of this / divisor.
        See Also:
        java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)
      • multiply

        public RoundedAmount multiply​(java.lang.Number multiplicand)
        Description copied from interface: Quantity
        Returns a Quantity whose value is (this × multiplicand) , and whose scale is
        this.scale() +
         multiplicand.scale()
        .
        Specified by:
        multiply in interface Quantity
        Parameters:
        multiplicand - value to be multiplied by this Quantity. If the multiplicand's scale exceeds the capabilities of the implementation, it may be rounded implicitly.
        Returns:
        this * multiplicand
      • negate

        public RoundedAmount negate()
        Description copied from interface: Quantity
        Returns a Quantity whose value is
        -this
        , and whose scale is this.scale().
        Specified by:
        negate in interface Quantity
        Returns:
        -this.
      • plus

        public RoundedAmount plus()
        Description copied from interface: Quantity
        Returns a Quantity whose value is
        +this
        , with rounding according to the context settings.
        Specified by:
        plus in interface Quantity
        Returns:
        this, rounded as necessary. A zero result will have a scale of 0.
        See Also:
        java.math.BigDecimal#plus()java.math.BigDecimal#plus()java.math.BigDecimal#plus()java.math.BigDecimal#plus()java.math.BigDecimal#plus()
      • subtract

        public RoundedAmount subtract​(Quantity amount)
        Subtract rounded unit.
        Specified by:
        subtract in interface Quantity
        Parameters:
        amount - the amount
        Returns:
        the rounded unit
      • remainder

        public RoundedAmount remainder​(java.lang.Number divisor)
        Description copied from interface: Quantity
        Returns a Quantity whose value is
        this % divisor
        . The remainder is given by
        this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)
        . Note that this is not the modulo operation (the result can be negative).
        Specified by:
        remainder in interface Quantity
        Parameters:
        divisor - value by which this Quantity is to be divided.
        Returns:
        this % divisor.
      • scaleByPowerOfTen

        public RoundedAmount scaleByPowerOfTen​(int power)
        Description copied from interface: Quantity
        Returns a Quantity whose numerical value is equal to ( this * 10n). The scale of the result is
        this.scale() - n
        .
        Specified by:
        scaleByPowerOfTen in interface Quantity
        Parameters:
        power - the power.
        Returns:
        the calculated amount value.
      • isZero

        public boolean isZero()
        Description copied from interface: Quantity
        Checks if an Quantity is zero.
        Specified by:
        isZero in interface Quantity
        Returns:
        true if signum() == 0.
      • isPositive

        public boolean isPositive()
        Description copied from interface: Quantity
        Checks if a Quantity is positive.
        Specified by:
        isPositive in interface Quantity
        Returns:
        true if signum() > 0.
      • isPositiveOrZero

        public boolean isPositiveOrZero()
        Description copied from interface: Quantity
        Checks if a Quantity is positive or zero.
        Specified by:
        isPositiveOrZero in interface Quantity
        Returns:
        true if signum() >= 0
      • isNegative

        public boolean isNegative()
        Description copied from interface: Quantity
        Checks if a Quantity is negative.
        Specified by:
        isNegative in interface Quantity
        Returns:
        true if signum() <0
      • isNegativeOrZero

        public boolean isNegativeOrZero()
        Description copied from interface: Quantity
        Checks if a Quantity is negative or zero.
        Specified by:
        isNegativeOrZero in interface Quantity
        Returns:
        true if signum() <= 0.
      • signum

        public int signum()
        Description copied from interface: Quantity
        Returns the signum function of this Quantity.
        Specified by:
        signum in interface Quantity
        Returns:
        -1, 0, or 1 as the value of this Quantity is negative, zero, or positive.
      • isLessThan

        public boolean isLessThan​(Quantity amount)
        Is less than boolean.
        Specified by:
        isLessThan in interface Quantity
        Parameters:
        amount - the amount
        Returns:
        the boolean
      • isLessThanOrEqualTo

        public boolean isLessThanOrEqualTo​(Quantity amount)
        Is less than or equal to boolean.
        Specified by:
        isLessThanOrEqualTo in interface Quantity
        Parameters:
        amount - the amount
        Returns:
        the boolean
      • isGreaterThan

        public boolean isGreaterThan​(Quantity amount)
        Is greater than boolean.
        Specified by:
        isGreaterThan in interface Quantity
        Parameters:
        amount - the amount
        Returns:
        the boolean
      • isGreaterThanOrEqualTo

        public boolean isGreaterThanOrEqualTo​(Quantity amount)
        Is greater than or equal to boolean.
        Specified by:
        isGreaterThanOrEqualTo in interface Quantity
        Parameters:
        amount - the amount
        Returns:
        the boolean
      • isEqualTo

        public boolean isEqualTo​(Quantity amount)
        Is equal to boolean.
        Specified by:
        isEqualTo in interface Quantity
        Parameters:
        amount - the amount
        Returns:
        the boolean
      • isNotEqualTo

        public boolean isNotEqualTo​(Quantity amount)
        Is not equal to boolean.
        Parameters:
        amount - the amount
        Returns:
        the boolean
      • with

        public RoundedAmount with​(Operator operator)
        With rounded unit.
        Specified by:
        with in interface Quantity
        Parameters:
        operator - the operator
        Returns:
        the rounded unit
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • compareTo

        public int compareTo​(Quantity o)
        Compare to int.
        Specified by:
        compareTo in interface java.lang.Comparable<Quantity>
        Parameters:
        o - the o
        Returns:
        the int
      • multiply

        public RoundedAmount multiply​(long multiplicand)
        Description copied from interface: Quantity
        Returns a Quantity whose value is (this × multiplicand), and whose scale is
        this.scale() +
         multiplicand.scale()
        .
        Specified by:
        multiply in interface Quantity
        Parameters:
        multiplicand - value to be multiplied by this Quantity.
        Returns:
        this * multiplicand
      • multiply

        public RoundedAmount multiply​(double multiplicand)
        Description copied from interface: Quantity
        Returns a Quantity whose value is (this × multiplicand), and whose scale is
        this.scale() +
         multiplicand.scale()
        . By default the input value's scale will be rounded to accommodate the format capabilities, and no ArithmeticException is thrown if the input number's scale exceeds the capabilities.
        Specified by:
        multiply in interface Quantity
        Parameters:
        multiplicand - value to be multiplied by this Quantity. If the multiplicand's scale exceeds the capabilities of the implementation, it may be rounded implicitly.
        Returns:
        this * multiplicand
      • divide

        public RoundedAmount divide​(long divisor)
        Description copied from interface: Quantity
        Returns a Quantity whose value is
        this /
         divisor
        , and whose preferred scale is
        this.scale() -
         divisor.scale()
        ; if the exact quotient cannot be represented an ArithmeticException is thrown.
        Specified by:
        divide in interface Quantity
        Parameters:
        divisor - value by which this Quantity is to be divided.
        Returns:
        this / divisor
      • divide

        public RoundedAmount divide​(double divisor)
        Description copied from interface: Quantity
        Returns a Quantity whose value is
        this /
         divisor
        , and whose preferred scale is
        this.scale() -
         divisor.scale()
        ; if the exact quotient cannot be represented an ArithmeticException is thrown.
        Specified by:
        divide in interface Quantity
        Parameters:
        divisor - value by which this Quantity is to be divided.
        Returns:
        this / divisor
      • remainder

        public RoundedAmount remainder​(long divisor)
        Description copied from interface: Quantity
        Returns a Quantity whose value is
        this % divisor
        . The remainder is given by
        this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)
        . Note that this is not the modulo operation (the result can be negative).
        Specified by:
        remainder in interface Quantity
        Parameters:
        divisor - value by which this Quantity is to be divided.
        Returns:
        this % divisor.
      • remainder

        public RoundedAmount remainder​(double divisor)
        Description copied from interface: Quantity
        Returns a Quantity whose value is
        this % divisor
        . The remainder is given by
        this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)
        . Note that this is not the modulo operation (the result can be negative).
        Specified by:
        remainder in interface Quantity
        Parameters:
        divisor - value by which this Quantity is to be divided.
        Returns:
        this % divisor.
      • stripTrailingZeros

        public RoundedAmount stripTrailingZeros()
        Description copied from interface: Quantity
        Returns a Quantity which is numerically equal to this one but with any trailing zeros removed from the representation. For example, stripping the trailing zeros from the Quantity value UNIT 600.0, which has [BigInteger, scale] components equals to [6000, 1], yields 6E2 with [ BigInteger, scale] components equals to [6, -2]
        Specified by:
        stripTrailingZeros in interface Quantity
        Returns:
        a numerically equal Quantity with any trailing zeros removed.
      • divideToIntegralValue

        public RoundedAmount divideToIntegralValue​(long divisor)
        Description copied from interface: Quantity
        Returns a Quantity whose value is the integer part of the quotient
        this / divisor
        rounded down. The preferred scale of the result is
        this.scale() -
         divisor.scale()
        .
        Specified by:
        divideToIntegralValue in interface Quantity
        Parameters:
        divisor - value by which this BigDecimal is to be divided.
        Returns:
        The integer part of this / divisor.
        See Also:
        java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)
      • divideToIntegralValue

        public RoundedAmount divideToIntegralValue​(double divisor)
        Description copied from interface: Quantity
        Returns a Quantity whose value is the integer part of the quotient
        this / divisor
        rounded down. The preferred scale of the result is
        this.scale() - divisor.scale()
        .
        Specified by:
        divideToIntegralValue in interface Quantity
        Parameters:
        divisor - value by which this BigDecimal is to be divided.
        Returns:
        The integer part of this / divisor.
        See Also:
        java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)