Each instance of an amount provides additional meta-data in form of a MonetaryContext.
This context contains detailed information on the numeric capabilities, e.g. the supported
precision and maximal scale, as well as the common implementation flavor.
Also a MonetaryAmount provides a NumberValue, which allows easily to extract the
numeric value, of the amount. And finally getFactory() provides a
MonetaryAmountFactory, which allows to of instances of MonetaryAmount based
on the same numeric implementation.
This JSR additionally recommends to consider the following aspects:
ArithmeticException, if performing arithmetic
operations between amounts exceeds the capabilities of the numeric representation type used. Any
implicit truncating, that would lead to complete invalid and useless results, should be avoided.
This recommendation does not affect format rounding, as required by the format numeric
representation of a monetary amount.
MonetaryAmount.NumberValue#numberValue(Class), whereas the exact counterpart,
NumberValue#numberValueExact(Class), works similar to
java.math.BigDecimal#longValueExact().
with(MonetaryOperator) must be of the same type as
type on which with was called. The with method also defines additional
interoperability requirements that are important to enable this invariant.from(MonetaryAmount) is
recommended to be implemented on each implementation class, that allows conversion of a
MonetaryAmount to a concrete instance. E.g.a class MyMoney extends MonetaryAmount
would contain the following method:
public final class MyMoney implements MonetaryAmount{ ... public static MyMoney from(MonetaryAmount amount)(...) }
MonetaryContext and a serialized
CurrencyUnit.equals/hashCode, hereby considering
with(MonetaryOperator)
| Modifier and Type | Method and Description |
|---|---|
| public MonetaryAmount | Returns: abs(thisReturns a |
| public MonetaryAmount | Returns: this + augendvalue to be added to this augend)MonetaryAmount.Returns a |
| public MonetaryAmount | Returns: this / divisorvalue by which this divisor)MonetaryAmount is to be divided.Returns a |
| public MonetaryAmount | Returns: this / divisorvalue by which this divisor)MonetaryAmount is to be divided.Returns a |
| public MonetaryAmount | Returns: this / divisorvalue by which this divisor)MonetaryAmount is to be divided.Returns a |
| public MonetaryAmount[] | Returns: a two elementMonetaryAmount array: the quotient (the result of
divideToIntegralValue) is the initial element and the remainder is the final
element.value by which this divisor)MonetaryAmount is to be divided, and the remainder
computed.Returns a two-element |
| public MonetaryAmount[] | Returns: a two elementMonetaryAmount array: the quotient (the result of
divideToIntegralValue) is the initial element and the remainder is the final
element.value by which this divisor)MonetaryAmount is to be divided, and the remainder
computed.Returns a two-element |
| public MonetaryAmount[] | Returns: a two elementMonetaryAmount array: the quotient (the result of
divideToIntegralValue) is the initial element and the remainder is the final
element.value by which this divisor)MonetaryAmount is to be divided, and the remainder
computed.Returns a two-element |
| public MonetaryAmount | Returns: The integer part ofthis / divisor.value by which this divisor)BigDecimal is to be divided.Returns a |
| public MonetaryAmount | Returns: The integer part ofthis / divisor.value by which this divisor)BigDecimal is to be divided.Returns a |
| public MonetaryAmount | Returns: The integer part ofthis / divisor.value by which this divisor)BigDecimal is to be divided.Returns a |
| public MonetaryContext | |
| public MonetaryAmountFactory | getFactory()
Creates a new |
| public boolean | Returns: true if amount == this.the amount)MonetaryAmount to be compared with this instance.Compares two instances of |
| public boolean | Returns: true if amount > this.the amount)MonetaryAmount to be compared with this instance.Compares two instances of |
| public boolean | Returns: true if amount >= this.the amount)MonetaryAmount to be compared with this instance.Compares two instances of |
| public boolean | Returns: true if amount < this.the amount)MonetaryAmount to be compared with this instance.Compares two instances of |
| public boolean | Returns: true if amount <= this.the amt)MonetaryAmount to be compared with this instance.Compares two instances of |
| public default boolean | |
| public default boolean | |
| public default boolean | |
| public default boolean | |
| public default boolean | |
| public MonetaryAmount | Returns: this * multiplicandvalue to be multiplied by this multiplicand)MonetaryAmount.Returns a |
| public MonetaryAmount | Returns: this * multiplicandvalue to be multiplied by this multiplicand)MonetaryAmount. If the multiplicand's scale exceeds
the
capabilities of the implementation, it may be rounded implicitly.Returns a |
| public MonetaryAmount | Returns: this * multiplicandvalue to be multiplied by this multiplicand)MonetaryAmount. If the multiplicand's scale exceeds
the
capabilities of the implementation, it may be rounded implicitly.Returns a |
| public MonetaryAmount | Returns: -this, rounded as necessary.Returns a |
| public MonetaryAmount | |
| public default < the type of the result R> R | Returns: the query result, null may be returned (defined by the query)the query to invoke, not null query)Queries this monetary amount for a value. |
| public MonetaryAmount | Returns: this % divisor.value by which this divisor)MonetaryAmount is to be divided.Returns a |
| public MonetaryAmount | Returns: this % divisor.value by which this divisor)MonetaryAmount is to be divided.Returns a |
| public MonetaryAmount | |
| public MonetaryAmount | Returns: the calculated amount value.the power. power)Returns a |
| public int | Returns: -1, 0, or 1 as the value of thisMonetaryAmount is negative, zero, or
positive.Returns the signum function of this |
| public MonetaryAmount | Returns: a numerically equalMonetaryAmount with any trailing zeros removed.Returns a |
| public MonetaryAmount | Returns: this - subtrahendvalue to be subtracted from this subtrahend)MonetaryAmount.Returns a |
| public default MonetaryAmount | Returns: an object of the same type with the specified conversion made, not nullthe operator to use, not null operator)Returns an operated object of the same type as this object with the operation made. |
| abs | back to summary |
|---|---|
| public MonetaryAmount abs() Returns a
| |
| add | back to summary |
|---|---|
| public MonetaryAmount add(MonetaryAmount augend) Returns a
| |
| divide | back to summary |
|---|---|
| public MonetaryAmount divide(long divisor) Returns a
| |
| divide | back to summary |
|---|---|
| public MonetaryAmount divide(double divisor) Returns a
| |
| divide | back to summary |
|---|---|
| public MonetaryAmount divide(Number divisor) Returns a
| |
| divideAndRemainder | back to summary |
|---|---|
| public MonetaryAmount[] divideAndRemainder(long divisor) Returns a two-element
Note that if both the integer quotient and remainder are needed, this method is faster than
using the
| |
| divideAndRemainder | back to summary |
|---|---|
| public MonetaryAmount[] divideAndRemainder(double divisor) Returns a two-element
Note that if both the integer quotient and remainder are needed, this method is faster than
using the
| |
| divideAndRemainder | back to summary |
|---|---|
| public MonetaryAmount[] divideAndRemainder(Number divisor) Returns a two-element
Note that if both the integer quotient and remainder are needed, this method is faster than
using the
| |
| divideToIntegralValue | back to summary |
|---|---|
| public MonetaryAmount divideToIntegralValue(long divisor) Returns a
| |
| divideToIntegralValue | back to summary |
|---|---|
| public MonetaryAmount divideToIntegralValue(double divisor) Returns a
| |
| divideToIntegralValue | back to summary |
|---|---|
| public MonetaryAmount divideToIntegralValue(Number divisor) Returns a
| |
| getContext | back to summary |
|---|---|
| public MonetaryContext getContext() Returns the
| |
| getFactory | back to summary |
|---|---|
| public MonetaryAmountFactory Creates a new This method is used for creating a new amount result after having done calculations that are not directly mappable to the default monetary arithmetics, e.g. currency conversion.
| |
| isEqualTo | back to summary |
|---|---|
| public boolean isEqualTo(MonetaryAmount amount) Compares two instances of
| |
| isGreaterThan | back to summary |
|---|---|
| public boolean isGreaterThan(MonetaryAmount amount) Compares two instances of
| |
| isGreaterThanOrEqualTo | back to summary |
|---|---|
| public boolean isGreaterThanOrEqualTo(MonetaryAmount amount) Compares two instances of
| |
| isLessThan | back to summary |
|---|---|
| public boolean isLessThan(MonetaryAmount amount) Compares two instances of
| |
| isLessThanOrEqualTo | back to summary |
|---|---|
| public boolean isLessThanOrEqualTo(MonetaryAmount amt) Compares two instances of
| |
| isNegative | back to summary |
|---|---|
| public default boolean isNegative() Checks if a
| |
| isNegativeOrZero | back to summary |
|---|---|
| public default boolean isNegativeOrZero() Checks if a
| |
| isPositive | back to summary |
|---|---|
| public default boolean isPositive() Checks if a
| |
| isPositiveOrZero | back to summary |
|---|---|
| public default boolean isPositiveOrZero() Checks if a
| |
| isZero | back to summary |
|---|---|
| public default boolean isZero() Checks if an
| |
| multiply | back to summary |
|---|---|
| public MonetaryAmount multiply(long multiplicand) Returns a
| |
| multiply | back to summary |
|---|---|
| public MonetaryAmount multiply(double multiplicand) Returns a
| |
| multiply | back to summary |
|---|---|
| public MonetaryAmount multiply(Number multiplicand) Returns a
| |
| negate | back to summary |
|---|---|
| public MonetaryAmount negate() Returns a
| |
| plus | back to summary |
|---|---|
| public MonetaryAmount plus() Returns a This method, which simply returns this
| |
| query | back to summary |
|---|---|
| public default <R> R query(MonetaryQuery<R> query) Queries this monetary amount for a value. This queries this amount using the specified query strategy object. Implementations must ensure that no observable state is altered when this read-only method is invoked.
| |
| remainder | back to summary |
|---|---|
| public MonetaryAmount remainder(long divisor) Returns a
The remainder is given by
| |
| remainder | back to summary |
|---|---|
| public MonetaryAmount remainder(double divisor) Returns a
The remainder is given by
| |
| remainder | back to summary |
|---|---|
| public MonetaryAmount remainder(Number divisor) Returns a
The remainder is given by
| |
| scaleByPowerOfTen | back to summary |
|---|---|
| public MonetaryAmount scaleByPowerOfTen(int power) Returns a
| |
| signum | back to summary |
|---|---|
| public int signum() Returns the signum function of this
| |
| stripTrailingZeros | back to summary |
|---|---|
| public MonetaryAmount stripTrailingZeros() Returns a
| |
| subtract | back to summary |
|---|---|
| public MonetaryAmount subtract(MonetaryAmount subtrahend) Returns a
| |
| with | back to summary |
|---|---|
| public default MonetaryAmount with(MonetaryOperator operator) Returns an operated object of the same type as this object with the operation made.
Hereby returning an instance of the same type is very important to prevent
uncontrolled mixup of implementations. Switching between implementations is still easily
possible, e.g. by using according
This converts this monetary amount according to the rules of the specified operator. A typical operator will change the amount and leave the currency unchanged. A more complex operator might also change the currency. Some example code indicating how and why this method is used:
Hereby also the method signature on the implementation type must return the concrete type, to enable a fluent API, e.g.
| |