- java.lang.Object
-
- life.expert.value.numeric.amount.BigAmount
-
public final class BigAmount extends java.lang.Object implements Quantity, java.lang.Comparable<Quantity>
Default immutable implementation ofQuantitybased onBigDecimalas numeric representation. As required byQuantitythis class is final, thread-safe, immutable and serializable. This class can be configured with an arbitraryAmountContext. The defaultAmountContextused models by default the same settings asMathContext.DECIMAL64.
-
-
Field Summary
Fields Modifier and Type Field Description static AmountContextDEFAULT_CONTEXTThe constant DEFAULT_CONTEXT.static AmountContextMAX_CONTEXTThe constant MAX_CONTEXT.
-
Method Summary
Modifier and Type Method Description BigAmountabs()Returns aQuantitywhose value is the absolute value of thisQuantity, and whose scale isthis.scale().BigAmountadd(Quantity amount)Add unit.intcompareTo(Quantity o)Compare to int.BigAmountdivide(double divisor)Returns aQuantitywhose value isBigAmountdivide(long divisor)Returns aQuantitywhose value isBigAmountdivide(java.lang.Number divisor)Returns aQuantitywhose value isBigAmount[]divideAndRemainder(double divisor)Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands.BigAmount[]divideAndRemainder(long divisor)Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands.BigAmount[]divideAndRemainder(java.lang.Number divisor)Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands.BigAmountdivideToIntegralValue(double divisor)Returns aQuantitywhose value is the integer part of the quotientBigAmountdivideToIntegralValue(long divisor)Returns aQuantitywhose value is the integer part of the quotientBigAmountdivideToIntegralValue(java.lang.Number divisor)Returns aQuantitywhose value is the integer part of the quotientbooleanequals(java.lang.Object obj)static BigAmountfrom(Quantity amt)AmountContextgetContext()Access theAmountContextused by this instance.NumberValuegetNumber()Gets the number representation of the numeric value of this item.java.math.BigDecimalgetNumberStripped()Method that returns BigDecimal.ZERO, ifisZero(), and#stripTrailingZeros()in all other cases.UnitgetUnit()Returns the amount’s unit, modelled asUnit.inthashCode()booleanisEqualTo(Quantity amount)Is equal to boolean.booleanisGreaterThan(Quantity amount)Is greater than boolean.booleanisGreaterThanOrEqualTo(Quantity amount)Is greater than or equal to boolean.booleanisLessThan(Quantity amount)Is less than 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.BigAmountmultiply(double multiplicand)Returns aQuantitywhose value is (this × multiplicand), and whose scale isBigAmountmultiply(long multiplicand)Returns aQuantitywhose value is (this × multiplicand), and whose scale isBigAmountmultiply(java.lang.Number multiplicand)Returns aQuantitywhose value is (this × multiplicand) , and whose scale isBigAmountnegate()Returns aQuantitywhose value isstatic BigAmountof(java.lang.Number number, java.lang.String unitCode)Static factory method for creating a new instance ofBigAmount.static BigAmountof(java.lang.Number number, java.lang.String unitCode, AmountContext amountContext)Static factory method for creating a new instance ofBigAmount.static BigAmountof(java.lang.Number number, Unit unit)Creates a new instance ofBigAmount, using the defaultAmountContext.static BigAmountof(java.lang.Number number, Unit unit, AmountContext amountContext)Creates a new instance ofBigAmount, using an explicitAmountContext.static BigAmountof(java.math.BigDecimal number, java.lang.String unitCode)Static factory method for creating a new instance ofBigAmount.static BigAmountof(java.math.BigDecimal number, java.lang.String unitCode, AmountContext amountContext)Static factory method for creating a new instance ofBigAmount.static BigAmountof(java.math.BigDecimal number, Unit unit)Creates a new instance ofBigAmount, using the defaultAmountContext.static BigAmountof(java.math.BigDecimal number, Unit unit, AmountContext amountContext)Creates a new instance ofBigAmount, using an explicitAmountContext.static BigAmountofMinor(Unit unit, long amountMinor)Obtains an instance ofBigAmountfrom an amount in minor units.static BigAmountofMinor(Unit unit, long amountMinor, int fractionDigits)Obtains an instance ofBigAmountfrom an amount in minor units.static BigAmountparse(java.lang.CharSequence text)Obtains an instance of BigAmount from a text string such as 'EUR 25.25'.BigAmountplus()Returns aQuantitywhose value isBigAmountremainder(double divisor)Returns aQuantitywhose value isBigAmountremainder(long divisor)Returns aQuantitywhose value isBigAmountremainder(java.lang.Number divisor)Returns aQuantitywhose value isBigAmountscaleByPowerOfTen(int power)Returns aQuantitywhose numerical value is equal to (this* 10n).intsignum()Returns the signum function of thisQuantity.BigAmountstripTrailingZeros()Returns aQuantitywhich is numerically equal to this one but with any trailing zeros removed from the representation.BigAmountsubtract(Quantity amount)Subtract unit.java.lang.StringtoString()BigAmountwith(Operator operator)With unit.static BigAmountzero(Unit unit)Obtains an instance ofBigAmountrepresenting 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.
-
-
Method Detail
-
of
public static BigAmount of(java.math.BigDecimal number, Unit unit)
Creates a new instance ofBigAmount, using the defaultAmountContext.- Parameters:
number- numeric value, notnull.unit- unit unit, notnull.- Returns:
- a
BigAmountcombining the numeric value and unit unit. - Throws:
java.lang.ArithmeticException- If the number exceeds the capabilities of the defaultAmountContextused.
-
of
public static BigAmount of(java.math.BigDecimal number, Unit unit, AmountContext amountContext)
Creates a new instance ofBigAmount, using an explicitAmountContext.- Parameters:
number- numeric value, notnull.unit- unit unit, notnull.amountContext- theAmountContextto be used, ifnullthe defaultAmountContextis used.- Returns:
- a
BigAmountinstance based on the monetary context with the given numeric value, unit unit. - Throws:
java.lang.ArithmeticException- If the number exceeds the capabilities of theAmountContextused.
-
of
public static BigAmount of(java.lang.Number number, Unit unit)
Creates a new instance ofBigAmount, using the defaultAmountContext.- Parameters:
number- The numeric part, not null.unit- The target unit, not null.- Returns:
- A new instance of
BigAmount. - Throws:
java.lang.ArithmeticException- If the number exceeds the capabilities of the defaultAmountContextused.
-
of
public static BigAmount of(java.lang.Number number, Unit unit, AmountContext amountContext)
Creates a new instance ofBigAmount, using an explicitAmountContext.- Parameters:
number- The numeric part, not null.unit- The target unit, not null.amountContext- theAmountContextto be used, ifnullthe defaultAmountContextis used.- Returns:
- A new instance of
BigAmount. - Throws:
java.lang.ArithmeticException- If the number exceeds the capabilities of theAmountContextused.
-
of
public static BigAmount of(java.lang.Number number, java.lang.String unitCode)
Static factory method for creating a new instance ofBigAmount.- Parameters:
number- The numeric part, not null.unitCode- The target unit as ISO unit code.- Returns:
- A new instance of
BigAmount.
-
of
public static BigAmount of(java.math.BigDecimal number, java.lang.String unitCode)
Static factory method for creating a new instance ofBigAmount.- Parameters:
number- The numeric part, not null.unitCode- The target unit as ISO unit code.- Returns:
- A new instance of
BigAmount.
-
of
public static BigAmount of(java.lang.Number number, java.lang.String unitCode, AmountContext amountContext)
Static factory method for creating a new instance ofBigAmount.- Parameters:
number- The numeric part, not null.unitCode- The target unit as ISO unit code.amountContext- theAmountContextto be used, ifnullthe defaultAmountContextis used.- Returns:
- A new instance of
BigAmount.
-
of
public static BigAmount of(java.math.BigDecimal number, java.lang.String unitCode, AmountContext amountContext)
Static factory method for creating a new instance ofBigAmount.- Parameters:
number- The numeric part, not null.unitCode- The target unit as ISO unit code.amountContext- theAmountContextto be used, ifnullthe defaultAmountContextis used.- Returns:
- A new instance of
BigAmount.
-
zero
public static BigAmount zero(Unit unit)
Obtains an instance ofBigAmountrepresenting zero.- Parameters:
unit- the unit, not null.- Returns:
- an instance of
BigAmountrepresenting zero.
-
ofMinor
public static BigAmount ofMinor(Unit unit, long amountMinor)
Obtains an instance ofBigAmountfrom an amount in minor units. For example,ofMinor(USD, 1234)creates the instanceUSD 12.34.- Parameters:
unit- the unitamountMinor- the amount of unit in the minor division of the unit- Returns:
- the BigAmount from minor units
- Throws:
java.lang.NullPointerException- when the unit is nulljava.lang.IllegalArgumentException- whenUnit.getDefaultFractionDigits()is lesser than zero.- Since:
- 1.0.1
- See Also:
Unit#getDefaultFractionDigits()
-
ofMinor
public static BigAmount ofMinor(Unit unit, long amountMinor, int fractionDigits)
Obtains an instance ofBigAmountfrom an amount in minor units. For example,ofMinor(USD, 1234, 2)creates the instanceUSD 12.34.- Parameters:
unit- the unit, not nullamountMinor- the amount of unit in the minor division of the unitfractionDigits- number of digits- Returns:
- the monetary amount from minor units
- Throws:
java.lang.NullPointerException- when the unit is nulljava.lang.IllegalArgumentException- when the fractionDigits is negative- Since:
- 1.0.1
- See Also:
Unit#getDefaultFractionDigits(),BigAmount#ofMinor(Unit , long , int)
-
from
public static BigAmount from(Quantity amt)
Converts (if necessary) the givenQuantityto aBigAmountinstance. TheAmountContextwill be adapted as necessary, if the precision of the given amount exceeds the capabilities of the defaultAmountContext.- Parameters:
amt- the amount to be converted- Returns:
- an according BigAmount instance.
-
parse
public static BigAmount parse(java.lang.CharSequence text)
Obtains an instance of BigAmount from a text string such as 'EUR 25.25'.- Parameters:
text- the text to parse not null- Returns:
- BigAmount instance
- Throws:
java.lang.NumberFormatException- if the amount is not a numberUnknownUnitException- if the unit cannot be resolved
-
getNumberStripped
public java.math.BigDecimal getNumberStripped()
Method that returns BigDecimal.ZERO, ifisZero(), and#stripTrailingZeros()in all other cases.- Returns:
- the stripped number value.
-
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()
-
getNumber
public NumberValue getNumber()
Gets the number representation of the numeric value of this item.
-
compareTo
public int compareTo(Quantity o)
Compare to int.- Specified by:
compareToin interfacejava.lang.Comparable<Quantity>- Parameters:
o- the o- Returns:
- the int
-
abs
public BigAmount abs()
Description copied from interface:QuantityReturns aQuantitywhose value is the absolute value of thisQuantity, and whose scale isthis.scale().
-
divide
public BigAmount 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 BigAmount 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.
-
divideAndRemainder
public BigAmount[] 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 BigAmount[] 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)
-
multiply
public BigAmount multiply(long multiplicand)
Description copied from interface:QuantityReturns aQuantitywhose value is (this × multiplicand), and whose scale is
.this.scale() + multiplicand.scale()
-
multiply
public BigAmount 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.
-
remainder
public BigAmount 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 BigAmount 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)
-
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.
-
divide
public BigAmount 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 BigAmount[] 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 BigAmount 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 BigAmount 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)
-
divideToIntegralValue
public BigAmount 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 BigAmount 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 BigAmount negate()
Description copied from interface:QuantityReturns aQuantitywhose value is
, and whose scale is-thisthis.scale().
-
plus
public BigAmount plus()
Description copied from interface:QuantityReturns aQuantitywhose value is
, with rounding according to the context settings.+this
-
stripTrailingZeros
public BigAmount 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.
-
remainder
public BigAmount 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 BigAmount 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.
-
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.
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-