public class Money extends Object implements FxConvertible<Money>, Comparable<Money>, Serializable
This class is similar to CurrencyAmount, but only exposes the rounded amounts.
The rounding is done using BigDecimal, as BigDecimal.ROUND_HALF_UP. Given this operation,
it should be assumed that the numbers are an approximation, and not an exact figure.
This class is immutable and thread-safe.
| Modifier and Type | Method and Description |
|---|---|
int |
compareTo(Money other)
Compares this money to another.
|
Money |
convertedTo(Currency resultCurrency,
BigDecimal fxRate)
Converts this amount to an equivalent amount the specified currency.
|
Money |
convertedTo(Currency resultCurrency,
Decimal fxRate)
Converts this amount to an equivalent amount the specified currency.
|
Money |
convertedTo(Currency resultCurrency,
FxRateProvider rateProvider)
Converts this amount to an equivalent amount in the specified currency.
|
boolean |
equals(Object obj)
Checks if this money equals another.
|
BigDecimal |
getAmount()
Deprecated.
Use
getValue() |
Currency |
getCurrency()
Gets the currency.
|
FixedScaleDecimal |
getValue()
Gets the numeric amount of the money.
|
int |
hashCode()
Returns a suitable hash code for the currency.
|
boolean |
isNegative()
Checks if the amount is negative.
|
boolean |
isPositive()
Checks if the amount is positive.
|
boolean |
isZero()
Checks if the amount is zero.
|
Money |
map(UnaryOperator<Decimal> mapper)
Applies an operation to the amount.
|
Money |
mapAmount(UnaryOperator<BigDecimal> mapper)
Deprecated.
Use
map(UnaryOperator), potentially using a lambda decimal -> decimal.mapAsBigDecimal(mapper) |
Money |
minus(Money amountToSubtract)
Returns a copy of this
Money with the specified amount subtracted. |
Money |
multipliedBy(long valueToMultiplyBy)
Returns a copy of this
Money with the amount multiplied. |
Money |
negated()
Returns a copy of this
Money with the amount negated. |
Money |
negative()
Returns a copy of this
Money with a negative amount. |
static Money |
of(BigMoney money)
Obtains an instance of
Money for the specified BigMoney. |
static Money |
of(CurrencyAmount currencyAmount)
Obtains an instance of
Money for the specified CurrencyAmount. |
static Money |
of(Currency currency,
BigDecimal amount)
Obtains an instance of
Money for the specified currency and amount. |
static Money |
of(Currency currency,
Decimal amount)
Obtains an instance of
Money for the specified currency and amount. |
static Money |
of(Currency currency,
double amount)
Obtains an instance of
Money for the specified currency and amount. |
static Money |
parse(String amountStr)
Parses the string to produce a
Money. |
Money |
plus(Money amountToAdd)
Returns a copy of this
Money with the specified amount added. |
Money |
positive()
Returns a copy of this
Money with a positive amount. |
BigMoney |
toBigMoney()
Converts this monetary amount to the equivalent
BigMoney. |
CurrencyAmount |
toCurrencyAmount()
Converts this monetary amount to the equivalent
CurrencyAmount. |
String |
toString()
Gets the amount as a string.
|
static Money |
zero(Currency currency)
Obtains a zero amount instance of
Money for the specified currency. |
public static Money zero(Currency currency)
Money for the specified currency.currency - the currency the amount is inpublic static Money of(CurrencyAmount currencyAmount)
Money for the specified CurrencyAmount.currencyAmount - the instance of CurrencyAmount wrapping the currency and amount.public static Money of(BigMoney money)
Money for the specified BigMoney.money - the instance of BigMoney wrapping the currency and amount.public static Money of(Currency currency, double amount)
Money for the specified currency and amount.currency - the currency the amount is inamount - the amount of the currency to representpublic static Money of(Currency currency, BigDecimal amount)
Money for the specified currency and amount.currency - the currency the amount is inamount - the amount of the currency to represent, as an instance of BigDecimalpublic static Money of(Currency currency, Decimal amount)
Money for the specified currency and amount.currency - the currency the amount is inamount - the amount of the currency to represent, as an instance of Decimalpublic static Money parse(String amountStr)
Money.
This parses the toString format of '${currency} ${amount}'.
amountStr - the amount stringIllegalArgumentException - if the amount cannot be parsedpublic Currency getCurrency()
For example, in the value 'GBP 12.34' the currency is 'GBP'.
@Deprecated public BigDecimal getAmount()
getValue()BigDecimal.
For example, in the amount 'GBP 12.34' the decimal value returned is 12.34, and for 'GBP 13.40' the amount returned is '13.40'.
public FixedScaleDecimal getValue()
For example, in the amount 'GBP 12.34' the decimal value returned is '12.34', whereas for 'GBP 13.40' the amount returned is '13.40'.
public Money plus(Money amountToAdd)
Money with the specified amount added.
This adds the specified amount to this monetary amount, returning a new object.
This instance is immutable and unaffected by this method.
amountToAdd - the amount to add, in the same currencyIllegalArgumentException - if the currencies are not equalpublic Money minus(Money amountToSubtract)
Money with the specified amount subtracted.
This subtracts the specified amount to this monetary amount, returning a new object.
This instance is immutable and unaffected by this method.
amountToSubtract - the amount to subtract, in the same currencyIllegalArgumentException - if the currencies are not equalpublic Money multipliedBy(long valueToMultiplyBy)
Money with the amount multiplied.
This takes this amount and multiplies it by the specified value.
This instance is immutable and unaffected by this method.
valueToMultiplyBy - the scalar amount to multiply bypublic Money map(UnaryOperator<Decimal> mapper)
This is generally used to apply a mathematical operation to the amount. For example, the operator could multiply the amount by a constant, or take the inverse.
abs = base.mapAmount(value -> value.abs());
mapper - the operator to be applied to the amount@Deprecated public Money mapAmount(UnaryOperator<BigDecimal> mapper)
map(UnaryOperator), potentially using a lambda decimal -> decimal.mapAsBigDecimal(mapper)This is generally used to apply a mathematical operation to the amount. For example, the operator could multiply the amount by a constant, or take the inverse.
abs = base.mapAmount(value -> value.abs());
mapper - the operator to be applied to the amountpublic boolean isZero()
public boolean isPositive()
Zero and negative amounts return false.
public boolean isNegative()
Zero and positive amounts return false.
public Money negated()
Money with the amount negated.
This takes this amount and negates it. If the amount is 0.0 or -0.0 the negated amount is 0.0.
This instance is immutable and unaffected by this method.
public Money positive()
Money with a positive amount.
The result of this method will always be positive, where the amount is equal to abs(amount).
This instance is immutable and unaffected by this method.
public Money negative()
Money with a negative amount.
The result of this method will always be negative, equal to -.abs(amount).
This instance is immutable and unaffected by this method.
public CurrencyAmount toCurrencyAmount()
CurrencyAmount.CurrencyAmountpublic BigMoney toBigMoney()
BigMoney.BigMoneypublic Money convertedTo(Currency resultCurrency, BigDecimal fxRate)
The result will be expressed in terms of the given currency, converting using the specified FX rate.
For example, if this represents 'GBP 100' and this method is called with
arguments (USD, 1.6) then the result will be 'USD 160'.
resultCurrency - the currency of the resultfxRate - the FX rate from this currency to the result currencyIllegalArgumentException - if the FX is not 1 when no conversion is requiredpublic Money convertedTo(Currency resultCurrency, Decimal fxRate)
The result will be expressed in terms of the given currency, converting using the specified FX rate.
For example, if this represents 'GBP 100' and this method is called with
arguments (USD, 1.6) then the result will be 'USD 160'.
resultCurrency - the currency of the resultfxRate - the FX rate from this currency to the result currencyIllegalArgumentException - if the FX is not 1 when no conversion is requiredpublic Money convertedTo(Currency resultCurrency, FxRateProvider rateProvider)
The result will be expressed in terms of the given currency. If conversion is needed, the provider will be used to supply the FX rate.
convertedTo in interface FxConvertible<Money>resultCurrency - the currency of the resultrateProvider - the provider of FX ratesRuntimeException - if no FX rate could be foundpublic int compareTo(Money other)
This compares currencies alphabetically, then by amount.
compareTo in interface Comparable<Money>other - the other amountpublic boolean equals(Object obj)
public int hashCode()
Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.