- java.lang.Object
-
- life.expert.value.numeric.amount.Amount
-
public final class Amount extends java.lang.Object implements Quantity, java.lang.Comparable<Quantity>
based implementation oflongQuantity.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 toBigAmount, which internally usesBigDecimal. 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);*
-
-
Field Summary
Fields Modifier and Type Field Description static AmountContextDEFAULT_CONTEXTThe constant DEFAULT_CONTEXT.static AmountContextMAX_CONTEXTThe constant MAX_CONTEXT.static AmountMAX_VALUEMaximum possible value supported, using XX (no unit).static AmountMIN_VALUEMinimum possible value supported, using XX (no unit).
-
Method Summary
Modifier and Type Method Description Amountabs()Returns aQuantitywhose value is the absolute value of thisQuantity, and whose scale isthis.scale().Amountadd(Quantity amount)Add amount.protected voidcheckNumber(java.lang.Number number)Internal method to check for correct number parameter.intcompareTo(Quantity o)Compare to int.Amountdivide(double divisor)Returns aQuantitywhose value isAmountdivide(long divisor)Returns aQuantitywhose value isAmountdivide(java.lang.Number divisor)Returns aQuantitywhose value isAmount[]divideAndRemainder(double divisor)Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands.Amount[]divideAndRemainder(long divisor)Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands.Amount[]divideAndRemainder(java.lang.Number divisor)Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands.AmountdivideToIntegralValue(double divisor)Returns aQuantitywhose value is the integer part of the quotientAmountdivideToIntegralValue(long divisor)Returns aQuantitywhose value is the integer part of the quotientAmountdivideToIntegralValue(java.lang.Number divisor)Returns aQuantitywhose value is the integer part of the quotientbooleanequals(java.lang.Object obj)static Amountfrom(Quantity amount)From amount.AmountContextgetContext()Access theAmountContextused by this instance.NumberValuegetNumber()Gets the number representation of the numeric value of this item.intgetPrecision()Gets precision.intgetScale()Gets scale.UnitgetUnit()Returns the amount’s unit, modelled asUnit.inthashCode()booleanhasSameNumberAs(java.lang.Number number)Has same number as boolean.booleanisEqualTo(Quantity amount)Is equal to boolean.booleanisGreaterThan(java.lang.Number number)Is greater than boolean.booleanisGreaterThan(Quantity amount)Is greater than boolean.booleanisGreaterThanOrEqualTo(java.lang.Number number)Is greater than or equal to boolean.booleanisGreaterThanOrEqualTo(Quantity amount)Is greater than or equal to boolean.booleanisLessThan(java.lang.Number number)Is less than boolean.booleanisLessThan(Quantity amount)Is less than boolean.booleanisLessThanOrEqualTo(java.lang.Number number)Is less than or equal to boolean.booleanisLessThanOrEqualTo(Quantity amount)Is less than or equal to boolean.booleanisNegative()Checks if aQuantityis negative.booleanisNegativeOrZero()Checks if aQuantityis negative or zero.booleanisPositive()Checks if aQuantityis positive.booleanisPositiveOrZero()Checks if aQuantityis positive or zero.booleanisZero()Checks if anQuantityis zero.Amountmultiply(double multiplicand)Returns aQuantitywhose value is (this × multiplicand), and whose scale isAmountmultiply(long multiplicand)Returns aQuantitywhose value is (this × multiplicand), and whose scale isAmountmultiply(java.lang.Number multiplicand)Returns aQuantitywhose value is (this × multiplicand) , and whose scale isAmountnegate()Returns aQuantitywhose value isstatic Amountof(java.lang.Number number)Static factory method for creating a new instance ofAmount.static Amountof(java.lang.Number number, java.lang.String unitCode)Static factory method for creating a new instance ofAmount.static Amountof(java.lang.Number number, Unit unit)Static factory method for creating a new instance ofAmount.static Amountof(NumberValue numberBinding, Unit unit)Static factory method for creating a new instance ofAmount.static AmountofMinor(Unit unit, long amountMinor)Obtains an instance ofDefaultAmountfrom an amount in minor units.static AmountofMinor(Unit unit, long amountMinor, int factionDigits)Obtains an instance ofDefaultAmountfrom an amount in minor units.static Amountparse(java.lang.CharSequence text)Obtains an instance of DefaultAmount from a text string such as 'piece 25.25'.Amountplus()Returns aQuantitywhose value isAmountremainder(double divisor)Returns aQuantitywhose value isAmountremainder(long divisor)Returns aQuantitywhose value isAmountremainder(java.lang.Number divisor)Returns aQuantitywhose value isAmountscaleByPowerOfTen(int power)Returns aQuantitywhose numerical value is equal to (this* 10n).intsignum()Returns the signum function of thisQuantity.AmountstripTrailingZeros()Returns aQuantitywhich is numerically equal to this one but with any trailing zeros removed from the representation.Amountsubtract(Quantity amount)Subtract amount.java.lang.StringtoString()Amountwith(Operator operator)With amount.static Amountzero(Unit unit)Obtains an instance ofAmountrepresenting zero.
-
-
-
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 nulljava.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 ofAmount.- 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 ofAmount.- 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 ofAmount.- 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 ofAmount.- Parameters:
number- The numeric part, not null.- Returns:
- A new instance of
Amount.
-
zero
public static Amount zero(Unit unit)
Obtains an instance ofAmountrepresenting zero.- Parameters:
unit- the target unit- Returns:
- an instance of
Amountrepresenting zero. - Since:
- 1.0.1
-
ofMinor
public static Amount ofMinor(Unit unit, long amountMinor)
Obtains an instance ofDefaultAmountfrom an amount in minor units. For example,ofMinor(USD, 1234)creates the instanceUSD 12.34.- Parameters:
unit- the unit, not nullamountMinor- the amount of units in the minor division of the unit- Returns:
- the amount from minor units
- Throws:
java.lang.NullPointerException- when the unit is nulljava.lang.IllegalArgumentException- whenUnit.getDefaultFractionDigits()is lesser than zero.- See Also:
Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits(),Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)Amount#ofMinor(Unit , long , int)
-
ofMinor
public static Amount ofMinor(Unit unit, long amountMinor, int factionDigits)
Obtains an instance ofDefaultAmountfrom an amount in minor units. For example,ofMinor(USD, 1234, 2)creates the instanceUSD 12.34.- Parameters:
unit- the unit, not nullamountMinor- the amount in the minor division of the unitfactionDigits- number of digits- Returns:
- the unit's amount from minor units
- Throws:
java.lang.NullPointerException- when the unit is nulljava.lang.IllegalArgumentException- when the factionDigits is negative- See Also:
Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()Unit#getDefaultFractionDigits()
-
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 numberUnknownUnitException- if the unit cannot be resolved
-
getUnit
public Unit getUnit()
Returns the amount’s unit, modelled asUnit. Implementations may co-variantly change the return type to a more specific implementation ofUnitif desired.- Specified by:
getUnitin interfaceQuantity- Returns:
- the unit, never
null - See Also:
Quantity.getUnit()
-
getContext
public AmountContext getContext()
Access theAmountContextused by this instance.- Specified by:
getContextin interfaceQuantity- Returns:
- the
AmountContextused, never null. - See Also:
Quantity.getContext()
-
compareTo
public int compareTo(Quantity o)
Compare to int.- Specified by:
compareToin interfacejava.lang.Comparable<Quantity>- Parameters:
o- the o- Returns:
- the int
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
abs
public Amount abs()
Description copied from interface:QuantityReturns aQuantitywhose value is the absolute value of thisQuantity, and whose scale isthis.scale().
-
divide
public Amount divide(java.lang.Number divisor)
Description copied from interface:QuantityReturns aQuantitywhose value is
, and whose preferred scale isthis / divisor
; if the exact quotient cannot be represented anthis.scale() - divisor.scale()ArithmeticExceptionis thrown.
-
divideAndRemainder
public Amount[] divideAndRemainder(java.lang.Number divisor)
Description copied from interface:QuantityReturns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands. Note that if both the integer quotient and remainder are needed, this method is faster than using thedivideToIntegralValueandremaindermethods separately because the division need only be carried out once.- Specified by:
divideAndRemainderin interfaceQuantity- Parameters:
divisor- value by which thisQuantityis to be divided, and the remainder computed.- Returns:
- a two element
Quantityarray: the quotient (the result ofdivideToIntegralValue) is the initial element and the remainder is the final element. - See Also:
#divideToIntegralValue(Number)#divideToIntegralValue(Number)#divideToIntegralValue(Number)#divideToIntegralValue(Number)#divideToIntegralValue(Number),#remainder(Number)#remainder(Number)#remainder(Number)#remainder(Number)#remainder(Number)
-
divideToIntegralValue
public Amount divideToIntegralValue(java.lang.Number divisor)
Description copied from interface:QuantityReturns aQuantitywhose value is the integer part of the quotient
rounded down. The preferred scale of the result isthis / divisor
.this.scale() - divisor.scale()- Specified by:
divideToIntegralValuein interfaceQuantity- Parameters:
divisor- value by which thisBigDecimalis 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:QuantityReturns aQuantitywhose value is (this × multiplicand) , and whose scale is
.this.scale() + multiplicand.scale()
-
negate
public Amount negate()
Description copied from interface:QuantityReturns aQuantitywhose value is
, and whose scale is-thisthis.scale().
-
plus
public Amount plus()
Description copied from interface:QuantityReturns aQuantitywhose value is
, with rounding according to the context settings.+this
-
remainder
public Amount remainder(java.lang.Number divisor)
Description copied from interface:QuantityReturns aQuantitywhose value is
. The remainder is given bythis % divisor
. Note that this is not the modulo operation (the result can be negative).this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)
-
scaleByPowerOfTen
public Amount scaleByPowerOfTen(int power)
Description copied from interface:QuantityReturns aQuantitywhose numerical value is equal to (this* 10n). The scale of the result is
.this.scale() - n- Specified by:
scaleByPowerOfTenin interfaceQuantity- Parameters:
power- the power.- Returns:
- the calculated amount value.
-
isZero
public boolean isZero()
Description copied from interface:QuantityChecks if anQuantityis zero.
-
isPositive
public boolean isPositive()
Description copied from interface:QuantityChecks if aQuantityis positive.- Specified by:
isPositivein interfaceQuantity- Returns:
true if signum() > 0.
-
isPositiveOrZero
public boolean isPositiveOrZero()
Description copied from interface:QuantityChecks if aQuantityis positive or zero.- Specified by:
isPositiveOrZeroin interfaceQuantity- Returns:
true if signum() >= 0
-
isNegative
public boolean isNegative()
Description copied from interface:QuantityChecks if aQuantityis negative.- Specified by:
isNegativein interfaceQuantity- Returns:
true if signum() <0
-
isNegativeOrZero
public boolean isNegativeOrZero()
Description copied from interface:QuantityChecks if aQuantityis negative or zero.- Specified by:
isNegativeOrZeroin interfaceQuantity- 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:QuantityReturns the signum function of thisQuantity.
-
isLessThan
public boolean isLessThan(Quantity amount)
Is less than boolean.- Specified by:
isLessThanin interfaceQuantity- Parameters:
amount- the amount- Returns:
- the boolean
-
isLessThanOrEqualTo
public boolean isLessThanOrEqualTo(Quantity amount)
Is less than or equal to boolean.- Specified by:
isLessThanOrEqualToin interfaceQuantity- Parameters:
amount- the amount- Returns:
- the boolean
-
isGreaterThan
public boolean isGreaterThan(Quantity amount)
Is greater than boolean.- Specified by:
isGreaterThanin interfaceQuantity- Parameters:
amount- the amount- Returns:
- the boolean
-
isGreaterThanOrEqualTo
public boolean isGreaterThanOrEqualTo(Quantity amount)
Is greater than or equal to boolean.- Specified by:
isGreaterThanOrEqualToin interfaceQuantity- Parameters:
amount- the amount- Returns:
- the boolean
-
isEqualTo
public boolean isEqualTo(Quantity amount)
Is equal to boolean.
-
getNumber
public NumberValue getNumber()
Gets the number representation of the numeric value of this item.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
multiply
public Amount multiply(double multiplicand)
Description copied from interface:QuantityReturns aQuantitywhose value is (this × multiplicand), and whose scale is
. By default the input value's scale will be rounded to accommodate the format capabilities, and nothis.scale() + multiplicand.scale()ArithmeticExceptionis thrown if the input number's scale exceeds the capabilities.
-
divide
public Amount divide(long divisor)
Description copied from interface:QuantityReturns aQuantitywhose value is
, and whose preferred scale isthis / divisor
; if the exact quotient cannot be represented anthis.scale() - divisor.scale()ArithmeticExceptionis thrown.
-
divide
public Amount divide(double divisor)
Description copied from interface:QuantityReturns aQuantitywhose value is
, and whose preferred scale isthis / divisor
; if the exact quotient cannot be represented anthis.scale() - divisor.scale()ArithmeticExceptionis thrown.
-
remainder
public Amount remainder(long divisor)
Description copied from interface:QuantityReturns aQuantitywhose value is
. The remainder is given bythis % divisor
. Note that this is not the modulo operation (the result can be negative).this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)
-
remainder
public Amount remainder(double divisor)
Description copied from interface:QuantityReturns aQuantitywhose value is
. The remainder is given bythis % divisor
. Note that this is not the modulo operation (the result can be negative).this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)
-
divideAndRemainder
public Amount[] divideAndRemainder(long divisor)
Description copied from interface:QuantityReturns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands. Note that if both the integer quotient and remainder are needed, this method is faster than using thedivideToIntegralValueandremaindermethods separately because the division need only be carried out once.- Specified by:
divideAndRemainderin interfaceQuantity- Parameters:
divisor- value by which thisQuantityis to be divided, and the remainder computed.- Returns:
- a two element
Quantityarray: the quotient (the result ofdivideToIntegralValue) is the initial element and the remainder is the final element. - See Also:
#divideToIntegralValue(long)#divideToIntegralValue(long)#divideToIntegralValue(long)#divideToIntegralValue(long)#divideToIntegralValue(long),#remainder(long)#remainder(long)#remainder(long)#remainder(long)#remainder(long)
-
divideAndRemainder
public Amount[] divideAndRemainder(double divisor)
Description copied from interface:QuantityReturns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands. Note that if both the integer quotient and remainder are needed, this method is faster than using thedivideToIntegralValueandremaindermethods separately because the division need only be carried out once.- Specified by:
divideAndRemainderin interfaceQuantity- Parameters:
divisor- value by which thisQuantityis to be divided, and the remainder computed.- Returns:
- a two element
Quantityarray: the quotient (the result ofdivideToIntegralValue) is the initial element and the remainder is the final element. - See Also:
#divideToIntegralValue(double)#divideToIntegralValue(double)#divideToIntegralValue(double)#divideToIntegralValue(double)#divideToIntegralValue(double),#remainder(double)#remainder(double)#remainder(double)#remainder(double)#remainder(double)
-
stripTrailingZeros
public Amount stripTrailingZeros()
Description copied from interface:QuantityReturns aQuantitywhich is numerically equal to this one but with any trailing zeros removed from the representation. For example, stripping the trailing zeros from theQuantityvalueUNIT 600.0, which has [BigInteger,scale] components equals to [6000, 1], yields6E2with [BigInteger,scale] components equals to [6, -2]- Specified by:
stripTrailingZerosin interfaceQuantity- Returns:
- a numerically equal
Quantitywith any trailing zeros removed.
-
multiply
public Amount multiply(long multiplicand)
Description copied from interface:QuantityReturns aQuantitywhose value is (this × multiplicand), and whose scale is
.this.scale() + multiplicand.scale()
-
divideToIntegralValue
public Amount divideToIntegralValue(long divisor)
Description copied from interface:QuantityReturns aQuantitywhose value is the integer part of the quotient
rounded down. The preferred scale of the result isthis / divisor
.this.scale() - divisor.scale()- Specified by:
divideToIntegralValuein interfaceQuantity- Parameters:
divisor- value by which thisBigDecimalis 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:QuantityReturns aQuantitywhose value is the integer part of the quotient
rounded down. The preferred scale of the result isthis / divisor
.this.scale() - divisor.scale()- Specified by:
divideToIntegralValuein interfaceQuantity- Parameters:
divisor- value by which thisBigDecimalis 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)
-
-