public abstract class BaseMonetaryAmount extends Object implements MonetaryAmount
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 BaseMonetaryAmount provides a NumberValue, which allows easily to extract the
numeric value, of the amount. And finally MonetaryAmount.getFactory() provides a
MonetaryAmountFactory, which allows to of instances of BaseMonetaryAmount 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.
BaseMonetaryAmount.NumberValue.numberValue(Class), whereas the exact counterpart,
NumberValue.numberValueExact(Class), works similar to
BigDecimal.longValueExact().
with(javax.money.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(javax.money.MonetaryOperator)| Constructor and Description |
|---|
BaseMonetaryAmount() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
isNegative()
Checks if a
MonetaryAmount is negative. |
boolean |
isNegativeOrZero()
Checks if a
MonetaryAmount is negative or zero. |
boolean |
isPositive()
Checks if a
MonetaryAmount is positive. |
boolean |
isPositiveOrZero()
Checks if a
MonetaryAmount is positive or zero. |
boolean |
isZero()
Checks if an
MonetaryAmount is zero. |
<R> R |
query(MonetaryQuery<R> query)
Queries this monetary amount for a value.
|
MonetaryAmount |
with(MonetaryOperator operator)
Returns an operated object of the same type as this object with the operation made.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitabs, add, divide, divide, divide, divideAndRemainder, divideAndRemainder, divideAndRemainder, divideToIntegralValue, divideToIntegralValue, divideToIntegralValue, getContext, getFactory, isEqualTo, isGreaterThan, isGreaterThanOrEqualTo, isLessThan, isLessThanOrEqualTo, multiply, multiply, multiply, negate, plus, remainder, remainder, remainder, scaleByPowerOfTen, signum, stripTrailingZeros, subtractgetCurrencygetNumbercompareTopublic BaseMonetaryAmount()
public <R> R query(MonetaryQuery<R> query)
query in interface MonetaryAmountR - the type of the resultquery - the query to invoke, not nullpublic MonetaryAmount with(MonetaryOperator operator)
MonetaryAmountFactory instances:
// converting from Money to MyMoney
Money m = ...;
MonetaryAmountFactory<MyMoney> f = Monetary.queryAmountFactory(MyMoney.class);
MyMoney myMoney = f.setAmount(m).of();
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:
MonetaryAmount money = money.with(amountMultipliedBy(2));
money = money.with(amountRoundedToNearestWholeUnit());
Hereby also the method signature on the implementation type must return the concrete type, to
enable a fluent API, e.g.
public final class MyMoney implements MonetaryAmount{
...
public MyMoney with(MonetaryOperator operator){
...
}
...
}
with in interface MonetaryAmountoperator - the operator to use, not nullpublic boolean isNegative()
MonetaryAmount is negative.isNegative in interface MonetaryAmounttrue if MonetaryAmount.signum() < 0.public boolean isNegativeOrZero()
MonetaryAmount is negative or zero.isNegativeOrZero in interface MonetaryAmounttrue if MonetaryAmount.signum() <= 0.public boolean isPositive()
MonetaryAmount is positive.isPositive in interface MonetaryAmounttrue if MonetaryAmount.signum() > 0.public boolean isPositiveOrZero()
MonetaryAmount is positive or zero.isPositiveOrZero in interface MonetaryAmounttrue if MonetaryAmount.signum() >= 0.public boolean isZero()
MonetaryAmount is zero.isZero in interface MonetaryAmounttrue if MonetaryAmount.signum() == 0.Copyright © 2012–2020 JavaMoney. All rights reserved.