Search in sources :

Example 16 with MonetaryException

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

Example 17 with MonetaryException

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

Example 18 with MonetaryException

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);
    }
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) 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