- All Superinterfaces:
CurrencySupplier
public interface ExchangeRate extends CurrencySupplier
This class models an exchange rate, which defines the factor the numeric value of a base amount in some currency
'A' must be multiplied
to get the corresponding amount in the terminating currency 'B'. Hereby
- an exchange rate always models one rate from a base (source) to a term
(target)
CurrencyUnit
. - an exchange rate is always bound to a rate type, which typically matches the data source of the conversion data, e.g. different credit card providers may use different rates for the same conversion.
- an exchange rate may restrict its validity. In most of the use cases a rates' validity will be well defined, but it is also possible that the data provider is not able to support the rate's validity, leaving it undefined-
- an exchange rate has a provider, which is responsible for defining the rate. A provider hereby may be, but must not be the same as the rate's data source.
- an exchange rate can be a direct rate, where its factor is represented by a single conversion step. Or it can model a derived rate, where multiple conversion steps are required to define the overall base/term conversion. In case of derived rates the chained rates define the overall factor, by multiplying the individual chain rate factors. Of course, this also requires that each subsequent rate's base currency in the chain does match the previous term currency (and vice versa):
- Whereas the factor should be directly implied by the format rate chain for derived rates, this is obviously not the case for the validity range, since rates can have a undefined validity range. Nevertheless in many cases also the validity range can (but must not) be derived from the rate chain.
- Finally a conversion rate is always unidirectional. There might be cases
where the reciprocal value of
getFactor()
} matches the correct reverse rate. But in most use cases the reverse rate either has a different rate (not equal to the reciprocal value), or might not be defined at all. Therefore for reversing a ExchangeRate one must access anExchangeRateProvider
and query for the reverse rate.
The class also implements Comparable
to allow sorting of multiple
exchange rates using the following sorting order;
- Exchange rate type
- Exchange rate provider
- base currency
- term currency
Finally ExchangeRate is modeled as an immutable and thread safe type. Also
exchange rates are Serializable
, hereby serializing in the following
form and order:
- The base
CurrencyUnit
- The target
CurrencyUnit
- The factor (NumberValue)
- The
ConversionContext
- The rate chain
Implementation Specification
Implementations of this interface
- must be Comparable(with
ExchangeRate
) - must implement equals/hashCode considering #getBaseCurrency, #getCurrency, #getFactor and #getContext.
- should be thread-safe
- should be serializable
- should provide a fluent builder API for constructing new rate instances easily.
- Author:
- Werner Keil, Anatole Tresch
- See Also:
- Wikipedia: Exchange Rate (Quotations)
-
Method Summary
Modifier and Type Method Description CurrencyUnit
getBaseCurrency()
Get the base (source)CurrencyUnit
.ConversionContext
getContext()
Access theConversionContext
ofExchangeRate
.CurrencyUnit
getCurrency()
Get the term (target)CurrencyUnit
.java.util.List<ExchangeRate>
getExchangeRateChain()
Access the chain of exchange rates.NumberValue
getFactor()
Access the rate's bid factor.default boolean
isDerived()
Allows to evaluate if this exchange rate is a derived exchange rate.
-
Method Details
-
getContext
ConversionContext getContext()Access theConversionContext
ofExchangeRate
.- Returns:
- the conversion context, never null.
-
getBaseCurrency
CurrencyUnit getBaseCurrency()Get the base (source)CurrencyUnit
.- Returns:
- the base
CurrencyUnit
.
-
getCurrency
CurrencyUnit getCurrency()Get the term (target)CurrencyUnit
.- Specified by:
getCurrency
in interfaceCurrencySupplier
- Returns:
- the term
CurrencyUnit
.
-
getFactor
NumberValue getFactor()Access the rate's bid factor.- Returns:
- the bid factor for this exchange rate, or
null
.
-
getExchangeRateChain
java.util.List<ExchangeRate> getExchangeRateChain()Access the chain of exchange rates.- Returns:
- the chain of rates, in case of a derived rate, this may be
several instances. For a direct exchange rate, this equals to
new ExchangeRate[]{this}
.
-
isDerived
default boolean isDerived()Allows to evaluate if this exchange rate is a derived exchange rate. Derived exchange rates are defined by an ordered list of subconversions with intermediate steps, whereas a direct conversion is possible in one steps.This method always returns
true
, if the chain contains more than one rate. Direct rates, have also a chain, but with exact one rate.- Returns:
- true, if the exchange rate is derived.
-