Class BigMoney
- java.lang.Object
-
- com.opengamma.strata.basics.currency.BigMoney
-
- All Implemented Interfaces:
FxConvertible<BigMoney>
,Serializable
,Comparable<BigMoney>
public class BigMoney extends Object implements FxConvertible<BigMoney>, Comparable<BigMoney>, Serializable
A monetary amount, held to a maximum of 12 decimal places.This class is similar to
Money
, but permits any number of decimal places. The amount will always have at least the number of decimal places of the currency. Trailing zeroes are stripped.This class is immutable and thread-safe.
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
compareTo(BigMoney other)
Compares this money to another.BigMoney
convertedTo(Currency resultCurrency, FxRateProvider rateProvider)
Converts this amount to an equivalent amount in the specified currency.BigMoney
convertedTo(Currency resultCurrency, Decimal fxRate)
Converts this amount to an equivalent amount the specified currency.BigMoney
convertedTo(Currency resultCurrency, BigDecimal fxRate)
Converts this amount to an equivalent amount the specified currency.boolean
equals(Object obj)
Checks if this money equals another.BigDecimal
getAmount()
Deprecated.UsegetValue()
Currency
getCurrency()
Gets the currency.Decimal
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.BigMoney
map(UnaryOperator<Decimal> mapper)
Applies an operation to the amount.BigMoney
mapAmount(UnaryOperator<BigDecimal> mapper)
Deprecated.Usemap(UnaryOperator)
, potentially using a lambdadecimal -> decimal.mapAsBigDecimal(mapper)
BigMoney
minus(BigMoney amountToSubtract)
Returns a copy of thisBigMoney
with the specified amount subtracted.BigMoney
multipliedBy(long valueToMultiplyBy)
Returns a copy of thisBigMoney
with the amount multiplied.BigMoney
negated()
Returns a copy of thisBigMoney
with the amount negated.BigMoney
negative()
Returns a copy of thisBigMoney
with a negative amount.static BigMoney
of(CurrencyAmount currencyAmount)
Obtains an instance ofBigMoney
for the specifiedCurrencyAmount
.static BigMoney
of(Currency currency, double amount)
Obtains an instance ofBigMoney
for the specified currency and amount.static BigMoney
of(Currency currency, Decimal amount)
Obtains an instance ofBigMoney
for the specified currency and amount.static BigMoney
of(Currency currency, BigDecimal amount)
Obtains an instance ofBigMoney
for the specified currency and amount.static BigMoney
of(Money money)
Obtains an instance ofBigMoney
for the specifiedMoney
.static BigMoney
parse(String amountStr)
Parses the string to produce aBigMoney
.BigMoney
plus(BigMoney amountToAdd)
Returns a copy of thisBigMoney
with the specified amount added.BigMoney
positive()
Returns a copy of thisBigMoney
with a positive amount.CurrencyAmount
toCurrencyAmount()
Converts this monetary amount to the equivalentCurrencyAmount
.Money
toMoney()
Converts this monetary amount to the equivalentMoney
.String
toString()
Gets the amount as a string.static BigMoney
zero(Currency currency)
Obtains a zero amount instance ofBigMoney
for the specified currency.
-
-
-
Method Detail
-
zero
public static BigMoney zero(Currency currency)
Obtains a zero amount instance ofBigMoney
for the specified currency.- Parameters:
currency
- the currency the amount is in- Returns:
- the zero amount instance
-
of
public static BigMoney of(CurrencyAmount currencyAmount)
Obtains an instance ofBigMoney
for the specifiedCurrencyAmount
.- Parameters:
currencyAmount
- the instance ofCurrencyAmount
wrapping the currency and amount.- Returns:
- the currency amount
-
of
public static BigMoney of(Money money)
Obtains an instance ofBigMoney
for the specifiedMoney
.- Parameters:
money
- the instance ofMoney
wrapping the currency and amount.- Returns:
- the currency amount
-
of
public static BigMoney of(Currency currency, double amount)
Obtains an instance ofBigMoney
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 BigMoney of(Currency currency, BigDecimal amount)
Obtains an instance ofBigMoney
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
-
of
public static BigMoney of(Currency currency, Decimal amount)
Obtains an instance ofBigMoney
for the specified currency and amount.- Parameters:
currency
- the currency the amount is inamount
- the amount of the currency to represent, as an instance ofDecimal
- Returns:
- the currency amount
-
parse
public static BigMoney parse(String amountStr)
Parses the string to produce aBigMoney
.This parses the
toString
format 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
@Deprecated public BigDecimal getAmount()
Deprecated.UsegetValue()
Gets the numeric amount of the money, as aBigDecimal
.For example, in the amount 'GBP 12.345' the decimal value returned is '12.345'.
- Returns:
- the amount, with a scale from 0 to 12
-
getValue
public Decimal getValue()
Gets the numeric amount of the money.For example, in the amount 'GBP 12.345' the decimal value returned is '12.345'.
- Returns:
- the amount, with a scale from 0 to 12
-
plus
public BigMoney plus(BigMoney amountToAdd)
Returns a copy of thisBigMoney
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.
- 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
-
minus
public BigMoney minus(BigMoney amountToSubtract)
Returns a copy of thisBigMoney
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.
- 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
-
multipliedBy
public BigMoney multipliedBy(long valueToMultiplyBy)
Returns a copy of thisBigMoney
with the amount multiplied.This takes this amount and multiplies it by the specified value.
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
-
map
public BigMoney map(UnaryOperator<Decimal> 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.
abs = base.mapAmount(value -> value.abs());
- Parameters:
mapper
- the operator to be applied to the amount- Returns:
- a copy of this amount with the mapping applied to the original amount
-
mapAmount
@Deprecated public BigMoney mapAmount(UnaryOperator<BigDecimal> mapper)
Deprecated.Usemap(UnaryOperator)
, potentially using a lambdadecimal -> decimal.mapAsBigDecimal(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.
abs = base.mapAmount(value -> value.abs());
- 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 BigMoney negated()
Returns a copy of thisBigMoney
with the amount negated.This takes this amount and negates it.
This instance is immutable and unaffected by this method.
- Returns:
- an amount based on this with the amount negated
-
positive
public BigMoney positive()
Returns a copy of thisBigMoney
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.
- Returns:
- an amount based on this where the amount is positive
-
negative
public BigMoney negative()
Returns a copy of thisBigMoney
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.
- Returns:
- an amount based on this where the amount is negative
-
toCurrencyAmount
public CurrencyAmount toCurrencyAmount()
Converts this monetary amount to the equivalentCurrencyAmount
.- Returns:
- the equivalent
CurrencyAmount
-
toMoney
public Money toMoney()
Converts this monetary amount to the equivalentMoney
.- Returns:
- the equivalent
Money
-
convertedTo
public BigMoney convertedTo(Currency resultCurrency, 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:
IllegalArgumentException
- if the FX is not 1 when no conversion is required
-
convertedTo
public BigMoney convertedTo(Currency resultCurrency, Decimal 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 BigMoney 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<BigMoney>
- 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(BigMoney other)
Compares this money to another.This compares currencies alphabetically, then by amount.
- Specified by:
compareTo
in interfaceComparable<BigMoney>
- Parameters:
other
- the other amount- Returns:
- negative if less, zero if equal, positive if greater
-
equals
public boolean equals(Object obj)
Checks if this money equals another.
-
hashCode
public int hashCode()
Returns a suitable hash code for the currency.
-
-