use of javax.money.MonetaryException in project jsr354-ri by JavaMoney.
the class IMFAbstractRateProvider 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();
LocalDate[] times = getQueryDates(conversionQuery);
ExchangeRate rate1 = getExchangeRate(currencyToSdr.get(base), times);
ExchangeRate rate2 = getExchangeRate(sdrToCurrency.get(term), times);
if (base.equals(SDR)) {
return rate2;
} else if (term.equals(SDR)) {
return rate1;
}
if (Objects.isNull(rate1) || Objects.isNull(rate2)) {
return null;
}
ConversionContext context = getExchangeContext("imf.digit.fraction");
ExchangeRateBuilder builder = new ExchangeRateBuilder(context);
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 by JavaMoney.
the class DefaultMonetaryConversionsSingletonSpi method getExchangeRateProvider.
@Override
public ExchangeRateProvider getExchangeRateProvider(String... providers) {
List<ExchangeRateProvider> provInstances = new ArrayList<>();
for (String provName : providers) {
ExchangeRateProvider prov = Optional.ofNullable(this.conversionProviders.get(provName)).orElseThrow(() -> new MonetaryException("Unsupported conversion/rate provider: " + provName));
provInstances.add(prov);
}
if (provInstances.size() == 1) {
return provInstances.get(0);
}
return new CompoundRateProvider(provInstances);
}
use of javax.money.MonetaryException in project jsr354-ri by JavaMoney.
the class DefaultMonetaryConversionsSingletonSpi method getExchangeRateProvider.
@Override
public ExchangeRateProvider getExchangeRateProvider(ConversionQuery conversionQuery) {
Collection<String> providers = getProvidersToUse(conversionQuery);
List<ExchangeRateProvider> provInstances = new ArrayList<>();
for (String provName : providers) {
ExchangeRateProvider prov = Optional.ofNullable(this.conversionProviders.get(provName)).orElseThrow(() -> new MonetaryException("Unsupported conversion/rate provider: " + provName));
provInstances.add(prov);
}
if (provInstances.isEmpty()) {
throw new MonetaryException("No such providers: " + conversionQuery);
}
if (provInstances.size() == 1) {
return provInstances.get(0);
}
return new CompoundRateProvider(provInstances);
}
use of javax.money.MonetaryException in project jsr354-ri-bp by JavaMoney.
the class ECBAbstractRateProvider method getExchangeRate.
@Override
public ExchangeRate getExchangeRate(ConversionQuery query) {
Objects.requireNonNull(query);
try {
if (loadLock.await(30, TimeUnit.SECONDS)) {
if (rates.isEmpty()) {
return null;
}
if (!isAvailable(query)) {
return null;
}
LocalDate selectedDate = null;
Map<String, ExchangeRate> targets = null;
for (LocalDate date : getTargetDates(query)) {
targets = this.rates.get(date);
if (targets != null) {
selectedDate = date;
break;
}
}
if (targets == null) {
return null;
}
ExchangeRateBuilder builder = getBuilder(query, selectedDate);
ExchangeRate sourceRate = targets.get(query.getBaseCurrency().getCurrencyCode());
ExchangeRate target = targets.get(query.getCurrency().getCurrencyCode());
return createExchangeRate(query, builder, sourceRate, target);
} 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 DefaultMonetaryConversionsSingletonSpi method getProvidersToUse.
private Collection<String> getProvidersToUse(ConversionQuery query) {
List<String> providersToUse = new ArrayList<>();
List<String> providers = query.getProviderNames();
if (providers.isEmpty()) {
providers = getDefaultProviderChain();
if (providers.isEmpty()) {
throw new IllegalStateException("No default provider chain available.");
}
}
for (String provider : providers) {
ExchangeRateProvider prov = this.conversionProviders.get(provider);
if (prov == null) {
throw new MonetaryException("Invalid ExchangeRateProvider (not found): " + provider);
}
providersToUse.add(provider);
}
return providersToUse;
}
Aggregations