Search in sources :

Example 11 with MonetaryException

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);
    }
}
Also used : ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) CurrencyUnit(javax.money.CurrencyUnit) ExchangeRate(javax.money.convert.ExchangeRate) ConversionContext(javax.money.convert.ConversionContext) LocalDate(java.time.LocalDate) MonetaryException(javax.money.MonetaryException)

Example 12 with MonetaryException

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);
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) CompoundRateProvider(org.javamoney.moneta.spi.CompoundRateProvider) MonetaryException(javax.money.MonetaryException)

Example 13 with MonetaryException

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);
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) CompoundRateProvider(org.javamoney.moneta.spi.CompoundRateProvider) MonetaryException(javax.money.MonetaryException)

Example 14 with MonetaryException

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);
    }
}
Also used : ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) ExchangeRate(javax.money.convert.ExchangeRate) MonetaryException(javax.money.MonetaryException)

Example 15 with MonetaryException

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;
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) MonetaryException(javax.money.MonetaryException)

Aggregations

MonetaryException (javax.money.MonetaryException)18 ArrayList (java.util.ArrayList)8 CurrencyUnit (javax.money.CurrencyUnit)8 ExchangeRateProvider (javax.money.convert.ExchangeRateProvider)7 ExchangeRate (javax.money.convert.ExchangeRate)6 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)6 CompoundRateProvider (org.javamoney.moneta.spi.CompoundRateProvider)4 LocalDate (java.time.LocalDate)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Comparator (java.util.Comparator)2 Map (java.util.Map)2 Objects (java.util.Objects)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Monetary (javax.money.Monetary)2