Class CurrencyAmount
- java.lang.Object
-
- com.opengamma.strata.basics.currency.CurrencyAmount
-
- All Implemented Interfaces:
FxConvertible<CurrencyAmount>,Serializable,Comparable<CurrencyAmount>
public final class CurrencyAmount extends Object implements FxConvertible<CurrencyAmount>, Comparable<CurrencyAmount>, Serializable
An amount of a currency.This class represents a
doubleamount associated with a currency. It is specifically named "CurrencyAmount" and not "Money" to indicate that it simply holds a currency and an amount. By contrast, naming it "Money" would imply it was a suitable choice for accounting purposes, which it is not. This design approach has been chosen primarily for performance reasons. Using aBigDecimalis markedly slower. See theMoneyclass for the alternative that usesBigDecimal.A
doubleis a 64 bit floating point value suitable for most calculations. Floating point maths is inexact due to the conflict between binary and decimal arithmetic. As such, there is the potential for data loss at the margins. For example, adding thedoublevalues0.1dand0.2dresults in0.30000000000000004rather than0.3. As can be seen, the level of error is small, hence providing this class is used appropriately, the use ofdoubleis acceptable. For example, using this class to provide a meaningful result type after calculations have completed would be an appropriate use.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 intcompareTo(CurrencyAmount other)Compares this currency amount to another.CurrencyAmountconvertedTo(Currency resultCurrency, double fxRate)Converts this amount to an equivalent amount the specified currency.CurrencyAmountconvertedTo(Currency resultCurrency, FxRateProvider rateProvider)Converts this amount to an equivalent amount in the specified currency.booleanequals(Object obj)Checks if this currency amount equals another.doublegetAmount()Gets the amount of the currency.CurrencygetCurrency()Gets the currency.inthashCode()Returns a suitable hash code for the currency.booleanisNegative()Checks if the amount is negative.booleanisPositive()Checks if the amount is positive.booleanisZero()Checks if the amount is zero.CurrencyAmountmapAmount(DoubleUnaryOperator mapper)Applies an operation to the amount.CurrencyAmountminus(double amountToSubtract)Returns a copy of thisCurrencyAmountwith the specified amount subtracted.CurrencyAmountminus(CurrencyAmount amountToSubtract)Returns a copy of thisCurrencyAmountwith the specified amount subtracted.CurrencyAmountmultipliedBy(double valueToMultiplyBy)Returns a copy of thisCurrencyAmountwith the amount multiplied.CurrencyAmountnegated()Returns a copy of thisCurrencyAmountwith the amount negated.CurrencyAmountnegative()Returns a copy of thisCurrencyAmountwith a negative amount.static CurrencyAmountof(Currency currency, double amount)Obtains an instance ofCurrencyAmountfor the specified currency and amount.static CurrencyAmountof(String currencyCode, double amount)Obtains an instance ofCurrencyAmountfor the specified ISO-4217 three letter currency code and amount.static CurrencyAmountparse(String amountStr)Parses the string to produce aCurrencyAmount.CurrencyAmountplus(double amountToAdd)Returns a copy of thisCurrencyAmountwith the specified amount added.CurrencyAmountplus(CurrencyAmount amountToAdd)Returns a copy of thisCurrencyAmountwith the specified amount added.CurrencyAmountpositive()Returns a copy of thisCurrencyAmountwith a positive amount.BigMoneytoBigMoney()Converts this monetary amount to the equivalentBigMoney.MoneytoMoney()Converts this monetary amount to the equivalentMoney.StringtoString()Gets the amount as a string.static CurrencyAmountzero(Currency currency)Obtains a zero amount instance ofCurrencyAmountfor the specified currency.
-
-
-
Method Detail
-
zero
public static CurrencyAmount zero(Currency currency)
Obtains a zero amount instance ofCurrencyAmountfor the specified currency.- Parameters:
currency- the currency the amount is in- Returns:
- the zero amount instance
-
of
public static CurrencyAmount of(Currency currency, double amount)
Obtains an instance ofCurrencyAmountfor the specified currency and amount.If the negative form of zero is passed in, it will be converted to positive zero.
- Parameters:
currency- the currency the amount is inamount- the amount of the currency to represent- Returns:
- the currency amount
- Throws:
IllegalArgumentException- if the amount isNaN
-
of
public static CurrencyAmount of(String currencyCode, double amount)
Obtains an instance ofCurrencyAmountfor the specified ISO-4217 three letter currency code and amount.A currency is uniquely identified by ISO-4217 three letter code. This method creates the currency if it is not known.
If the negative form of zero is passed in, it will be converted to positive zero.
- Parameters:
currencyCode- the three letter currency code, ASCII and upper caseamount- the amount of the currency to represent- Returns:
- the currency amount
- Throws:
IllegalArgumentException- if the currency code is invalid, or if the amount isNaN
-
parse
public static CurrencyAmount parse(String amountStr)
Parses the string to produce aCurrencyAmount.This parses the
toStringformat of '${currency} ${amount}'.- Parameters:
amountStr- the amount string- Returns:
- the currency amount
- Throws:
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 double getAmount()
Gets the amount of the currency.For example, in the value 'GBP 12.34' the amount is 12.34.
- Returns:
- the amount
-
plus
public CurrencyAmount plus(CurrencyAmount amountToAdd)
Returns a copy of thisCurrencyAmountwith the specified amount added.This adds the specified amount to this monetary amount, returning a new object. The addition simply uses standard
doublearithmetic.This instance is immutable and unaffected by this method.
- Parameters:
amountToAdd- the amount to add, in the same currency- Returns:
- an amount based on this with the specified amount added
- Throws:
IllegalArgumentException- if the currencies are not equal
-
plus
public CurrencyAmount plus(double amountToAdd)
Returns a copy of thisCurrencyAmountwith the specified amount added.This adds the specified amount to this monetary amount, returning a new object. The addition simply uses standard
doublearithmetic.This instance is immutable and unaffected by this method.
- Parameters:
amountToAdd- the amount to add- Returns:
- an amount based on this with the specified amount added
-
minus
public CurrencyAmount minus(CurrencyAmount amountToSubtract)
Returns a copy of thisCurrencyAmountwith the specified amount subtracted.This subtracts the specified amount to this monetary amount, returning a new object. The addition simply uses standard
doublearithmetic.This instance is immutable and unaffected by this method.
- Parameters:
amountToSubtract- the amount to subtract, in the same currency- Returns:
- an amount based on this with the specified amount subtracted
- Throws:
IllegalArgumentException- if the currencies are not equal
-
minus
public CurrencyAmount minus(double amountToSubtract)
Returns a copy of thisCurrencyAmountwith the specified amount subtracted.This subtracts the specified amount to this monetary amount, returning a new object. The addition simply uses standard
doublearithmetic.This instance is immutable and unaffected by this method.
- Parameters:
amountToSubtract- the amount to subtract- Returns:
- an amount based on this with the specified amount subtracted
-
multipliedBy
public CurrencyAmount multipliedBy(double valueToMultiplyBy)
Returns a copy of thisCurrencyAmountwith the amount multiplied.This takes this amount and multiplies it by the specified value. The multiplication simply uses standard
doublearithmetic.This instance is immutable and unaffected by this method.
- Parameters:
valueToMultiplyBy- the scalar amount to multiply by- Returns:
- an amount based on this with the amount multiplied
-
mapAmount
public CurrencyAmount mapAmount(DoubleUnaryOperator mapper)
Applies an operation to the amount.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.
multiplied = base.mapAmount(value -> (value < 0 ? 0 : value * 3));
- Parameters:
mapper- the operator to be applied to the amount- Returns:
- a copy of this amount with the mapping applied to the original amount
-
isZero
public boolean isZero()
Checks if the amount is zero.- Returns:
- true if zero
-
isPositive
public boolean isPositive()
Checks if the amount is positive.Zero and negative amounts return false.
- Returns:
- true if positive
-
isNegative
public boolean isNegative()
Checks if the amount is negative.Zero and positive amounts return false.
- Returns:
- true if negative
-
negated
public CurrencyAmount negated()
Returns a copy of thisCurrencyAmountwith 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.
- Returns:
- an amount based on this with the amount negated
-
positive
public CurrencyAmount positive()
Returns a copy of thisCurrencyAmountwith a positive amount.The result of this method will always be positive, where the amount is equal to
Math.abs(amount).This instance is immutable and unaffected by this method.
- Returns:
- a currency amount based on this where the amount is positive
-
negative
public CurrencyAmount negative()
Returns a copy of thisCurrencyAmountwith a negative amount.The result of this method will always be negative, equal to
-Math.abs(amount).This instance is immutable and unaffected by this method.
- Returns:
- a currency amount based on this where the amount is negative
-
toMoney
public Money toMoney()
Converts this monetary amount to the equivalentMoney.An instance of
Moneyonly stores the amount to the number of decimal places of the currency. As such, this method may result in a loss of precision.- Returns:
- the equivalent
Money
-
toBigMoney
public BigMoney toBigMoney()
Converts this monetary amount to the equivalentBigMoney.- Returns:
- the equivalent
BigMoney
-
convertedTo
public CurrencyAmount convertedTo(Currency resultCurrency, double 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:
IllegalArgumentException- if the FX is not 1 when no conversion is required
-
convertedTo
public CurrencyAmount 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:
convertedToin interfaceFxConvertible<CurrencyAmount>- Parameters:
resultCurrency- the currency of the resultrateProvider- the provider of FX rates- Returns:
- the converted instance, in the specified currency
- Throws:
RuntimeException- if no FX rate could be found
-
compareTo
public int compareTo(CurrencyAmount other)
Compares this currency amount to another.This compares currencies alphabetically, then by amount.
- Specified by:
compareToin interfaceComparable<CurrencyAmount>- Parameters:
other- the other amount- Returns:
- negative if less, zero if equal, positive if greater
-
equals
public boolean equals(Object obj)
Checks if this currency amount equals another.
-
hashCode
public int hashCode()
Returns a suitable hash code for the currency.
-
-