Class Money
- java.lang.Object
-
- com.opengamma.strata.basics.currency.Money
-
- All Implemented Interfaces:
FxConvertible<Money>
,java.io.Serializable
,java.lang.Comparable<Money>
public class Money extends java.lang.Object implements FxConvertible<Money>, java.lang.Comparable<Money>, java.io.Serializable
An amount of a currency, rounded to match the currency specifications.This class is similar to
CurrencyAmount
, but only exposes the rounded amounts. The rounding is done usingBigDecimal
, 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.
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(Money other)
Compares this money to another.Money
convertedTo(Currency resultCurrency, FxRateProvider rateProvider)
Converts this amount to an equivalent amount in the specified currency.Money
convertedTo(Currency resultCurrency, java.math.BigDecimal fxRate)
Converts this amount to an equivalent amount the specified currency.boolean
equals(java.lang.Object obj)
Checks if this money equals another.java.math.BigDecimal
getAmount()
Gets the amount of the currency as an instance ofBigDecimal
.Currency
getCurrency()
Gets the currency.int
hashCode()
Returns a suitable hash code for the currency.static Money
of(CurrencyAmount currencyAmount)
Obtains an instance ofMoney
for the specifiedCurrencyAmount
.static Money
of(Currency currency, double amount)
Obtains an instance ofMoney
for the specified currency and amount.static Money
of(Currency currency, java.math.BigDecimal amount)
Obtains an instance ofMoney
for the specified currency and amount.static Money
parse(java.lang.String amountStr)
Parses the string to produce aMoney
.java.lang.String
toString()
Gets the amount as a string.
-
-
-
Method Detail
-
of
public static Money of(CurrencyAmount currencyAmount)
Obtains an instance ofMoney
for the specifiedCurrencyAmount
.- Parameters:
currencyAmount
- the instance ofCurrencyAmount
wrapping the currency and amount.- Returns:
- the currency amount
-
of
public static Money of(Currency currency, double amount)
Obtains an instance ofMoney
for the specified currency and amount.- Parameters:
currency
- the currency the amount is inamount
- the amount of the currency to represent- Returns:
- the currency amount
-
of
public static Money of(Currency currency, java.math.BigDecimal amount)
Obtains an instance ofMoney
for the specified currency and amount.- Parameters:
currency
- the currency the amount is inamount
- the amount of the currency to represent, as an instance ofBigDecimal
- Returns:
- the currency amount
-
parse
public static Money parse(java.lang.String amountStr)
Parses the string to produce aMoney
.This parses the
toString
format of '${currency} ${amount}'.- Parameters:
amountStr
- the amount string- Returns:
- the currency amount
- Throws:
java.lang.IllegalArgumentException
- if the amount cannot be parsed
-
getCurrency
public Currency getCurrency()
Gets the currency.For example, in the value 'GBP 12.34' the currency is 'GBP'.
- Returns:
- the currency
-
getAmount
public java.math.BigDecimal getAmount()
Gets the amount of the currency as an instance ofBigDecimal
.The amount will be rounded to the currency specifications.
For example, in the value 'GBP 12.34' the amount is 12.34.
- Returns:
- the amount
-
convertedTo
public Money convertedTo(Currency resultCurrency, java.math.BigDecimal fxRate)
Converts this amount to an equivalent amount the specified currency.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'.- Parameters:
resultCurrency
- the currency of the resultfxRate
- the FX rate from this currency to the result currency- Returns:
- the converted instance, which should be expressed in the specified currency
- Throws:
java.lang.IllegalArgumentException
- if the FX is not 1 when no conversion is required
-
convertedTo
public Money convertedTo(Currency resultCurrency, FxRateProvider rateProvider)
Converts this amount to an equivalent amount in the specified currency.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.
- Specified by:
convertedTo
in interfaceFxConvertible<Money>
- Parameters:
resultCurrency
- the currency of the resultrateProvider
- the provider of FX rates- Returns:
- the converted instance, in the specified currency
- Throws:
java.lang.RuntimeException
- if no FX rate could be found
-
compareTo
public int compareTo(Money other)
Compares this money to another.This compares currencies alphabetically, then by amount.
- Specified by:
compareTo
in interfacejava.lang.Comparable<Money>
- Parameters:
other
- the other amount- Returns:
- negative if less, zero if equal, positive if greater
-
equals
public boolean equals(java.lang.Object obj)
Checks if this money equals another.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- the other amount, null returns false- Returns:
- true if equal
-
hashCode
public int hashCode()
Returns a suitable hash code for the currency.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code
-
toString
public java.lang.String toString()
Gets the amount as a string.The format is the currency code, followed by a space, followed by the amount: '${currency} ${amount}'.
- Overrides:
toString
in classjava.lang.Object
- Returns:
- the currency amount
-
-