Search in sources :

Example 26 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri-bp by JavaMoney.

the class ECBAbstractRateProvider method createExchangeRate.

private ExchangeRate createExchangeRate(ConversionQuery query, ExchangeRateBuilder builder, ExchangeRate sourceRate, ExchangeRate target) {
    if (areBothBaseCurrencies(query)) {
        builder.setFactor(DefaultNumberValue.ONE);
        return builder.build();
    } else if (BASE_CURRENCY_CODE.equals(query.getCurrency().getCurrencyCode())) {
        if (sourceRate == null) {
            return null;
        }
        return reverse(sourceRate);
    } else if (BASE_CURRENCY_CODE.equals(query.getBaseCurrency().getCurrencyCode())) {
        return target;
    } else {
        // Get Conversion base as derived rate: base -> EUR -> term
        ExchangeRate rate1 = getExchangeRate(query.toBuilder().setTermCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE)).build());
        ExchangeRate rate2 = getExchangeRate(query.toBuilder().setBaseCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE)).setTermCurrency(query.getCurrency()).build());
        if (rate1 != null && rate2 != null) {
            builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
            builder.setRateChain(rate1, rate2);
            return builder.build();
        }
        throw new CurrencyConversionException(query.getBaseCurrency(), query.getCurrency(), sourceRate.getContext());
    }
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) CurrencyConversionException(javax.money.convert.CurrencyConversionException)

Example 27 with ExchangeRate

use of javax.money.convert.ExchangeRate 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 28 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri-bp by JavaMoney.

the class ECBRateReader method addRate.

/**
 * Method to add a currency exchange rate.
 *
 * @param term      the term (target) currency, mapped from EUR.
 * @param localDate The target day.
 * @param rate      The rate.
 */
void addRate(CurrencyUnit term, LocalDate localDate, Number rate) {
    RateType rateType = RateType.HISTORIC;
    ExchangeRateBuilder builder;
    if (localDate != null) {
        // TODO check/test!
        if (localDate.equals(LocalDate.now())) {
            rateType = RateType.DEFERRED;
        }
        builder = new ExchangeRateBuilder(ConversionContextBuilder.create(context, rateType).set(localDate).build());
    } else {
        builder = new ExchangeRateBuilder(ConversionContextBuilder.create(context, rateType).build());
    }
    builder.setBase(ECBHistoricRateProvider.BASE_CURRENCY);
    builder.setTerm(term);
    builder.setFactor(DefaultNumberValue.of(rate));
    ExchangeRate exchangeRate = builder.build();
    Map<String, ExchangeRate> rateMap = this.historicRates.get(localDate);
    if (rateMap == null) {
        synchronized (this.historicRates) {
            rateMap = this.historicRates.get(localDate);
            if (rateMap == null) {
                rateMap = new ConcurrentHashMap<>();
                this.historicRates.put(localDate, rateMap);
            }
        }
    }
    rateMap.put(term.getCurrencyCode(), exchangeRate);
}
Also used : ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) ExchangeRate(javax.money.convert.ExchangeRate) RateType(javax.money.convert.RateType)

Example 29 with ExchangeRate

use of javax.money.convert.ExchangeRate 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 30 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri-bp by JavaMoney.

the class ExchangeRateSimpleTest method equalsTest.

@Test
public void equalsTest() {
    DefaultNumberValue factor = new DefaultNumberValue(1.1);
    DefaultNumberValue bigDecimalFactor = new DefaultNumberValue(new BigDecimal("1.1"));
    ExchangeRate rate1 = new ExchangeRateBuilder("myprovider", RateType.ANY).setBase(EUR).setTerm(GBP).setFactor(factor).build();
    ExchangeRate rate2 = new ExchangeRateBuilder("myprovider", RateType.ANY).setBase(EUR).setTerm(GBP).setFactor(factor).build();
    ExchangeRate rate3 = new ExchangeRateBuilder("myprovider", RateType.ANY).setBase(EUR).setTerm(GBP).setFactor(bigDecimalFactor).build();
    assertEquals(rate1, rate2, "Rates with same factor");
    assertEquals(rate1, rate3, "Rates with numerically equal factor");
    assertEquals(rate1.hashCode(), rate2.hashCode(), "Hashes with same factor");
    assertEquals(rate1.hashCode(), rate3.hashCode(), "Hashes with numerically equal factor");
}
Also used : DefaultNumberValue(org.javamoney.moneta.spi.DefaultNumberValue) ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) ExchangeRate(javax.money.convert.ExchangeRate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

ExchangeRate (javax.money.convert.ExchangeRate)30 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)12 Test (org.testng.annotations.Test)12 BigDecimal (java.math.BigDecimal)9 CurrencyUnit (javax.money.CurrencyUnit)8 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)7 MonetaryException (javax.money.MonetaryException)6 CurrencyConversionException (javax.money.convert.CurrencyConversionException)5 LocalDate (java.time.LocalDate)4 RateType (javax.money.convert.RateType)4 Map (java.util.Map)3 InputStream (java.io.InputStream)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Objects (java.util.Objects)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2