public final class ExchangeRate extends Object implements Serializable
BigMoneyProvider
instances to be converted between amounts in
different CurrencyUnit
s. Exchange rate is an object that holds data about two CurrencyUnit
s and a
conversion rate, and basically mirrors the following equation:
1 major unit of target currency
= rate
major units of
source currency
.
ExchangeRateOperations
. A default implementation can be acquired using operations(int, RoundingMode)
.
Instances of this class are immutable.
Two instances of this class are considered equal when both target and source currencies are the same and the rates
compare equal according to BigDecimal.compareTo(BigDecimal)
.
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj) |
BigDecimal |
getRate()
Returns the conversion rate.
|
CurrencyUnit |
getSource()
Returns the source currency.
|
CurrencyUnit |
getTarget()
Returns the target currency.
|
int |
hashCode() |
static ExchangeRate |
identity(CurrencyUnit currency)
Creates an identity rate for the given
CurrencyUnit . |
static ExchangeRate |
of(BigDecimal rate,
CurrencyUnit source,
CurrencyUnit target)
Creates an Exchange rate with the given parameters.
|
ExchangeRateOperations |
operations()
Creates an instance of an object that knows how to perform operations on
ExchangeRate s with default
values for scale and roundingMode that will be used for calculations. |
ExchangeRateOperations |
operations(int scale,
RoundingMode roundingMode)
Creates an instance of an object that knows how to perform operations on
ExchangeRate s. |
static ExchangeRate |
parse(String exchangeRate) |
String |
toString() |
ExchangeRate |
withRate(BigDecimal rate)
Creates an ExchangeRate like this one with the given rate.
|
public static ExchangeRate parse(String exchangeRate)
public static ExchangeRate identity(CurrencyUnit currency)
CurrencyUnit
. The rate will be equal to 1.00 and both source and
target currency units will be the same as the given one.currency
- to be set for source and target currency units.NullPointerException
- if the given parameter is null
public static ExchangeRate of(BigDecimal rate, CurrencyUnit source, CurrencyUnit target)
rate
- the conversion ratesource
- source CurrencyUnit
target
- target CurrencyUnit
NullPointerException
- if any of the given parameters is null
IllegalArgumentException
- if the given rate is less or equal to 0 or when source and currency units are
equal and rate is not equal to 1public ExchangeRate withRate(BigDecimal rate)
rate
- the rate that the new ExchangeRate should haveof(BigDecimal, CurrencyUnit, CurrencyUnit)
public BigDecimal getRate()
public CurrencyUnit getSource()
public CurrencyUnit getTarget()
public ExchangeRateOperations operations(int scale, RoundingMode roundingMode)
ExchangeRate
s.scale
- scale that will be used for calculationsroundingMode
- rounding mode to use if rounding is necessaryExchangeRateOperations
public ExchangeRateOperations operations()
ExchangeRate
s with default
values for scale
and roundingMode
that will be used for calculations. The values used
as defaults are:
RoundingMode.HALF_EVEN
ExchangeRateOperations
Copyright © 2009-2012 Joda.org. All Rights Reserved.