Module life.expert

Class Amount

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

    public final class Amount
    extends java.lang.Object
    implements Quantity, java.lang.Comparable<Quantity>
    long
    based implementation of Quantity.This class internally uses a single long number as numeric representation, which basically is interpreted as minor units. It suggested to have a performance advantage of a 10-15 times faster compared to BigAmount, which internally uses BigDecimal. Nevertheless this comes with a amount of less precision. As an example performing the following calculation one million times, results in slightly different results:
    
     Amount x = x.add(DefaultAmount.of(1234567.3444, "BOXES"));
     x = x.subtract(DefaultAmount.of(232323, "BOTTLES"));
     x = x.multiply(3.4);
     x = x.divide(5.456);
     *
    • Method Summary

      Modifier and Type Method Description
      Amount abs()
      Returns a Quantity whose value is the absolute value of this Quantity, and whose scale is this.scale().
      Amount add​(Quantity amount)
      Add amount.
      protected void checkNumber​(java.lang.Number number)
      Internal method to check for correct number parameter.
      int compareTo​(Quantity o)
      Compare to int.
      Amount divide​(double divisor)
      Returns a Quantity whose value is
      Amount divide​(long divisor)
      Returns a Quantity whose value is
      Amount divide​(java.lang.Number divisor)
      Returns a Quantity whose value is
      Amount[] divideAndRemainder​(double divisor)
      Returns a two-element Quantity array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.
      Amount[] divideAndRemainder​(long divisor)
      Returns a two-element Quantity array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.
      Amount[] divideAndRemainder​(java.lang.Number divisor)
      Returns a two-element Quantity array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.
      Amount divideToIntegralValue​(double divisor)
      Returns a Quantity whose value is the integer part of the quotient
      Amount divideToIntegralValue​(long divisor)
      Returns a Quantity whose value is the integer part of the quotient
      Amount divideToIntegralValue​(java.lang.Number divisor)
      Returns a Quantity whose value is the integer part of the quotient
      boolean equals​(java.lang.Object obj)  
      static Amount from​(Quantity amount)
      From amount.
      AmountContext getContext()
      Access the AmountContext used by this instance.
      NumberValue getNumber()
      Gets the number representation of the numeric value of this item.
      int getPrecision()
      Gets precision.
      int getScale()
      Gets scale.
      Unit getUnit()
      Returns the amount’s unit, modelled as Unit.
      int hashCode()  
      boolean hasSameNumberAs​(java.lang.Number number)
      Has same number as boolean.
      boolean isEqualTo​(Quantity amount)
      Is equal to boolean.
      boolean isGreaterThan​(java.lang.Number number)
      Is greater than boolean.
      boolean isGreaterThan​(Quantity amount)
      Is greater than boolean.
      boolean isGreaterThanOrEqualTo​(java.lang.Number number)
      Is greater than or equal to boolean.
      boolean isGreaterThanOrEqualTo​(Quantity amount)
      Is greater than or equal to boolean.
      boolean isLessThan​(java.lang.Number number)
      Is less than boolean.
      boolean isLessThan​(Quantity amount)
      Is less than boolean.
      boolean isLessThanOrEqualTo​(java.lang.Number number)
      Is less than or equal to boolean.
      boolean isLessThanOrEqualTo​(Quantity amount)
      Is less than or equal to boolean.
      boolean isNegative()
      Checks if a Quantity is negative.
      boolean isNegativeOrZero()
      Checks if a Quantity is negative or zero.
      boolean isPositive()
      Checks if a Quantity is positive.
      boolean isPositiveOrZero()
      Checks if a Quantity is positive or zero.
      boolean isZero()
      Checks if an Quantity is zero.
      Amount multiply​(double multiplicand)
      Returns a Quantity whose value is (this × multiplicand), and whose scale is
      Amount multiply​(long multiplicand)
      Returns a Quantity whose value is (this × multiplicand), and whose scale is
      Amount multiply​(java.lang.Number multiplicand)
      Returns a Quantity whose value is (this × multiplicand) , and whose scale is
      Amount negate()
      Returns a Quantity whose value is
      static Amount of​(java.lang.Number number)
      Static factory method for creating a new instance of Amount.
      static Amount of​(java.lang.Number number, java.lang.String unitCode)
      Static factory method for creating a new instance of Amount.
      static Amount of​(java.lang.Number number, Unit unit)
      Static factory method for creating a new instance of Amount.
      static Amount of​(NumberValue numberBinding, Unit unit)
      Static factory method for creating a new instance of Amount.
      static Amount ofMinor​(Unit unit, long amountMinor)
      Obtains an instance of DefaultAmount from an amount in minor units.
      static Amount ofMinor​(Unit unit, long amountMinor, int factionDigits)
      Obtains an instance of DefaultAmount from an amount in minor units.
      static Amount parse​(java.lang.CharSequence text)
      Obtains an instance of DefaultAmount from a text string such as 'piece 25.25'.
      Amount plus()
      Returns a Quantity whose value is
      Amount remainder​(double divisor)
      Returns a Quantity whose value is
      Amount remainder​(long divisor)
      Returns a Quantity whose value is
      Amount remainder​(java.lang.Number divisor)
      Returns a Quantity whose value is
      Amount scaleByPowerOfTen​(int power)
      Returns a Quantity whose numerical value is equal to ( this * 10n).
      int signum()
      Returns the signum function of this Quantity.
      Amount stripTrailingZeros()
      Returns a Quantity which is numerically equal to this one but with any trailing zeros removed from the representation.
      Amount subtract​(Quantity amount)
      Subtract amount.
      java.lang.String toString()  
      Amount with​(Operator operator)
      With amount.
      static Amount zero​(Unit unit)
      Obtains an instance of Amount representing zero.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DEFAULT_CONTEXT

        public static final AmountContext DEFAULT_CONTEXT
        The constant DEFAULT_CONTEXT.
      • MAX_CONTEXT

        public static final AmountContext MAX_CONTEXT
        The constant MAX_CONTEXT.
      • MAX_VALUE

        public static final Amount MAX_VALUE
        Maximum possible value supported, using XX (no unit).
      • MIN_VALUE

        public static final Amount MIN_VALUE
        Minimum possible value supported, using XX (no unit).
    • Method Detail

      • checkNumber

        protected void checkNumber​(java.lang.Number number)
        Internal method to check for correct number parameter.
        Parameters:
        number - the number to be checked, including null..
        Throws:
        java.lang.NullPointerException - If the number is null
        java.lang.ArithmeticException - If the number exceeds the capabilities of this class.
      • isLessThan

        public final boolean isLessThan​(java.lang.Number number)
        Is less than boolean.
        Parameters:
        number - the number
        Returns:
        the boolean
      • isLessThanOrEqualTo

        public final boolean isLessThanOrEqualTo​(java.lang.Number number)
        Is less than or equal to boolean.
        Parameters:
        number - the number
        Returns:
        the boolean
      • isGreaterThan

        public final boolean isGreaterThan​(java.lang.Number number)
        Is greater than boolean.
        Parameters:
        number - the number
        Returns:
        the boolean
      • isGreaterThanOrEqualTo

        public final boolean isGreaterThanOrEqualTo​(java.lang.Number number)
        Is greater than or equal to boolean.
        Parameters:
        number - the number
        Returns:
        the boolean
      • hasSameNumberAs

        public final boolean hasSameNumberAs​(java.lang.Number number)
        Has same number as boolean.
        Parameters:
        number - the number
        Returns:
        the boolean
      • of

        public static Amount of​(NumberValue numberBinding,
                                Unit unit)
        Static factory method for creating a new instance of Amount.
        Parameters:
        numberBinding - The numeric part, not null.
        unit - The target unit, not null.
        Returns:
        A new instance of Amount.
      • of

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

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

        public static Amount of​(java.lang.Number number)
        Static factory method for creating a new instance of Amount.
        Parameters:
        number - The numeric part, not null.
        Returns:
        A new instance of Amount.
      • zero

        public static Amount zero​(Unit unit)
        Obtains an instance of Amount representing zero.
        Parameters:
        unit - the target unit
        Returns:
        an instance of Amount representing zero.
        Since:
        1.0.1
      • from

        public static Amount from​(Quantity amount)
        From amount.
        Parameters:
        amount - the amount
        Returns:
        the amount
      • parse

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

        public Unit getUnit()
        Returns the amount’s unit, modelled as Unit. Implementations may co-variantly change the return type to a more specific implementation of Unit if desired.
        Specified by:
        getUnit in interface Quantity
        Returns:
        the unit, never null
        See Also:
        Quantity.getUnit()
      • 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
      • 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
      • abs

        public Amount 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 Amount add​(Quantity amount)
        Add amount.
        Specified by:
        add in interface Quantity
        Parameters:
        amount - the amount
        Returns:
        the amount
      • divide

        public Amount 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 Amount 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 Amount 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 Amount 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 Amount 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 Amount subtract​(Quantity amount)
        Subtract amount.
        Specified by:
        subtract in interface Quantity
        Parameters:
        amount - the subtrahend
        Returns:
        the amount
      • remainder

        public Amount 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 Amount 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.
      • getScale

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

        public int getPrecision()
        Gets precision.
        Returns:
        the precision
      • 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
      • getNumber

        public NumberValue getNumber()
        Gets the number representation of the numeric value of this item.
        Specified by:
        getNumber in interface Quantity
        Returns:
        The Number representation matching best.
      • toString

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

        public Amount with​(Operator operator)
        With amount.
        Specified by:
        with in interface Quantity
        Parameters:
        operator - the operator
        Returns:
        the amount
      • multiply

        public Amount 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 Amount 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 Amount 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 Amount 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 Amount 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 Amount 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.
      • multiply

        public Amount 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
      • divideToIntegralValue

        public Amount 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 Amount 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)