use of javax.money.MonetaryException in project jsr354-ri-bp by JavaMoney.
the class IMFRateProvider method getExchangeRate.
@Override
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
try {
if (loadLock.await(30, TimeUnit.SECONDS)) {
if (currencyToSdr.isEmpty()) {
return null;
}
if (!isAvailable(conversionQuery)) {
return null;
}
CurrencyUnit base = conversionQuery.getBaseCurrency();
CurrencyUnit term = conversionQuery.getCurrency();
Calendar timestamp = conversionQuery.get(Calendar.class);
if (timestamp == null) {
timestamp = conversionQuery.get(GregorianCalendar.class);
}
ExchangeRate rate1;
ExchangeRate rate2;
LocalDate localDate;
if (timestamp == null) {
localDate = LocalDate.yesterday();
rate1 = lookupRate(currencyToSdr.get(base), localDate);
rate2 = lookupRate(sdrToCurrency.get(term), localDate);
if (rate1 == null || rate2 == null) {
localDate = LocalDate.beforeDays(2);
}
rate1 = lookupRate(currencyToSdr.get(base), localDate);
rate2 = lookupRate(sdrToCurrency.get(term), localDate);
if (rate1 == null || rate2 == null) {
localDate = LocalDate.beforeDays(3);
rate1 = lookupRate(currencyToSdr.get(base), localDate);
rate2 = lookupRate(sdrToCurrency.get(term), localDate);
}
} else {
localDate = LocalDate.from(timestamp);
rate1 = lookupRate(currencyToSdr.get(base), localDate);
rate2 = lookupRate(sdrToCurrency.get(term), localDate);
}
if (rate1 == null || rate2 == null) {
return null;
}
if (base.equals(SDR)) {
return rate2;
} else if (term.equals(SDR)) {
return rate1;
}
ExchangeRateBuilder builder = new ExchangeRateBuilder(ConversionContext.of(CONTEXT.getProviderName(), RateType.HISTORIC));
builder.setBase(base);
builder.setTerm(term);
builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
builder.setRateChain(rate1, rate2);
return builder.build();
} else {
// Lets wait for a successful load only once, then answer requests as data is present.
loadLock.countDown();
throw new MonetaryException("Failed to load currency conversion data: " + loadState);
}
} catch (InterruptedException e) {
throw new MonetaryException("Failed to load currency conversion data: Load task has been interrupted.", e);
}
}
use of javax.money.MonetaryException in project jsr354-ri-bp by JavaMoney.
the class BaseMonetaryConversionsSingletonSpi method getExchangeRateProviders.
/**
* Access the current registered {@link javax.money.convert.ExchangeRateProvider} instances. If no provider
* names are passed ALL current registered providers are returned in undefined order.
*
* @param providers the provider names of hte providers to be accessed
* @return the list of providers, in the same order as requested.
* @throws javax.money.MonetaryException if a provider could not be resolved.
*/
public List<ExchangeRateProvider> getExchangeRateProviders(String... providers) {
List<ExchangeRateProvider> provInstances = new ArrayList<>();
Collection<String> providerNames = Arrays.asList(providers);
if (providerNames.isEmpty()) {
providerNames = getProviderNames();
}
for (String provName : providerNames) {
ExchangeRateProvider provider = getExchangeRateProvider(provName);
if (provider == null) {
throw new MonetaryException("Unsupported conversion/rate provider: " + provName);
}
provInstances.add(provider);
}
return provInstances;
}
use of javax.money.MonetaryException in project jsr354-ri-bp by JavaMoney.
the class MoneyUtils method checkAmountParameter.
/**
* Method to check if a currency is compatible with this amount instance.
*
* @param amount The monetary amount to be compared to, never null.
* @param currencyUnit the currency unit to compare, never null.
* @throws MonetaryException If the amount is null, or the amount's {@link CurrencyUnit} is not
* compatible, meaning has a different value of
* {@link CurrencyUnit#getCurrencyCode()}).
*/
public static void checkAmountParameter(MonetaryAmount amount, CurrencyUnit currencyUnit) {
Objects.requireNonNull(amount, "Amount must not be null.");
final CurrencyUnit amountCurrency = amount.getCurrency();
if (!(currencyUnit.getCurrencyCode().equals(amountCurrency.getCurrencyCode()))) {
throw new MonetaryException("Currency mismatch: " + currencyUnit + '/' + amountCurrency);
}
}
Aggregations