java.lang.Object
javax.money.convert.MonetaryConversions
public final class MonetaryConversions
extends java.lang.Object
This singleton defines access to the exchange and currency conversion logic
of JavaMoney. It allows to evaluate the currently available exchange rate
type instances and provides access to the corresponding
ExchangeRateProvider
and CurrencyConversion
instances.
This class is thread safe.
This class is designed to support also contextual behaviour, e.g. in Java EE
containers each application may provide its own ExchangeRateProvider
instances, e.g. by registering them as CDI beans. An EE container can
register an according MonetaryConversionsSingletonSpi
that manages the
different application contexts transparently. In a SE environment this class
is expected to behave like an ordinary singleton, loading its SPIs e.g. from the
JDK ServiceLoader
or an alternate component and service provider.
This class is thread-safe. Hereby it is important to know that it delegates
to the registered MonetaryConversionsSingletonSpi
SPI, which also is required
to be thread-safe.
- Author:
- Anatole Tresch, Werner Keil
-
Method Summary
Modifier and Type Method Description static CurrencyConversion
getConversion(java.lang.String termCurrencyCode, java.lang.String... providers)
Access an instance ofCurrencyConversion
for the given providers.static CurrencyConversion
getConversion(ConversionQuery conversionQuery)
Access an instance ofCurrencyConversion
for the given providers.static CurrencyConversion
getConversion(CurrencyUnit termCurrency, java.lang.String... providers)
Access an instance ofCurrencyConversion
for the given providers.static java.util.Collection<java.lang.String>
getConversionProviderNames()
Return the (non localized) names of all providers available in the current context.static java.util.List<java.lang.String>
getDefaultConversionProviderChain()
Get the default provider used.static ExchangeRateProvider
getExchangeRateProvider(java.lang.String... providers)
Access an instance ofCurrencyConversion
using the given providers as a provider chain.static ExchangeRateProvider
getExchangeRateProvider(ConversionQuery conversionQuery)
Access an instance ofCurrencyConversion
using the given providers as a provider chain.static ExchangeRateProvider
getExchangeRateProvider(ExchangeRateProviderSupplier provider, ExchangeRateProviderSupplier... providers)
Access an instance ofCurrencyConversion
using theExchangeRateProviderSupplier
.static boolean
isConversionAvailable(java.lang.String currencyCode, java.lang.String... providers)
Checks if aCurrencyConversion
is available for the given parameters.static boolean
isConversionAvailable(ConversionQuery conversionQuery)
Checks if aCurrencyConversion
is available for the given parameters.static boolean
isConversionAvailable(CurrencyUnit termCurrency, java.lang.String... providers)
Checks if aCurrencyConversion
is available for the given parameters.static boolean
isExchangeRateProviderAvailable(ConversionQuery conversionQuery)
Checks if aExchangeRateProvider
is available for the given parameters.
-
Method Details
-
getConversion
public static CurrencyConversion getConversion(CurrencyUnit termCurrency, java.lang.String... providers)Access an instance ofCurrencyConversion
for the given providers. UsegetConversionProviderNames()
to check, which are available.- Parameters:
termCurrency
- the terminating or target currency, notnull
providers
- Additional providers, for building a provider chain- Returns:
- the exchange rate type if this instance.
- Throws:
java.lang.IllegalArgumentException
- if no suchExchangeRateProvider
is available.
-
getConversion
public static CurrencyConversion getConversion(java.lang.String termCurrencyCode, java.lang.String... providers)Access an instance ofCurrencyConversion
for the given providers. UsegetConversionProviderNames()
} to check, which are available.- Parameters:
termCurrencyCode
- the terminating or target currency code, notnull
providers
- Additional providers, for building a provider chain- Returns:
- the exchange rate type if this instance.
- Throws:
MonetaryException
- if no suchExchangeRateProvider
is available or if noCurrencyUnit
was matching the given currency code.
-
getConversion
Access an instance ofCurrencyConversion
for the given providers. UsegetConversionProviderNames()
} to check, which are available.- Parameters:
conversionQuery
- TheConversionQuery
required, notnull
- Returns:
- the
CurrencyConversion
instance matching. - Throws:
java.lang.IllegalArgumentException
- if the query definesExchangeRateProvider
s that are not available.
-
isConversionAvailable
Checks if aCurrencyConversion
is available for the given parameters.- Parameters:
conversionQuery
- theConversionQuery
, not null.- Returns:
- true, if a conversion is accessible from
getConversion(ConversionQuery)
.
-
isConversionAvailable
public static boolean isConversionAvailable(java.lang.String currencyCode, java.lang.String... providers)Checks if aCurrencyConversion
is available for the given parameters.- Parameters:
currencyCode
- The currency code, resolvable by(String, String...)
providers
- Additional providers, for building a provider chain- Returns:
- true, if a conversion is accessible from
getConversion(String, String...)
.
-
isConversionAvailable
public static boolean isConversionAvailable(CurrencyUnit termCurrency, java.lang.String... providers)Checks if aCurrencyConversion
is available for the given parameters.- Parameters:
termCurrency
- the terminating or target currency, notnull
providers
- Additional providers, for building a provider chain- Returns:
- true, if a conversion is accessible from
getConversion(String, String...)
.
-
getExchangeRateProvider
Access an instance ofCurrencyConversion
using the given providers as a provider chain. UsegetConversionProviderNames()
s to check, which are available.- Parameters:
providers
- Additional providers, for building a provider chain- Returns:
- the exchange rate provider.
- Throws:
java.lang.IllegalArgumentException
- if no suchExchangeRateProvider
is available.
-
getExchangeRateProvider
public static ExchangeRateProvider getExchangeRateProvider(ExchangeRateProviderSupplier provider, ExchangeRateProviderSupplier... providers)Access an instance ofCurrencyConversion
using theExchangeRateProviderSupplier
.- Parameters:
provider
- the exchange rate provider.providers
- the exchange rate provider.- Returns:
- the exchange rate provider.
- Throws:
java.lang.IllegalArgumentException
- if no suchExchangeRateProvider
is available.
-
getExchangeRateProvider
Access an instance ofCurrencyConversion
using the given providers as a provider chain. UsegetConversionProviderNames()
to check, which are available.- Parameters:
conversionQuery
- theConversionQuery
, not null.- Returns:
- the exchange rate provider.
- Throws:
java.lang.IllegalArgumentException
- if no suchExchangeRateProvider
is available.
-
isExchangeRateProviderAvailable
Checks if aExchangeRateProvider
is available for the given parameters.- Parameters:
conversionQuery
- theConversionQuery
, not null.- Returns:
- true, if a rate provider is accessible from
getExchangeRateProvider(ConversionQuery)
}.
-
getConversionProviderNames
public static java.util.Collection<java.lang.String> getConversionProviderNames()Return the (non localized) names of all providers available in the current context. Each id can be used to obtainExchangeRateProvider
orCurrencyConversion
instances.- Returns:
- all supported provider ids, never
null
.
-
getDefaultConversionProviderChain
public static java.util.List<java.lang.String> getDefaultConversionProviderChain()Get the default provider used.- Returns:
- the default provider, never
null
.
-