Search in sources :

Example 6 with ExchangeRateBuilder

use of org.javamoney.moneta.convert.ExchangeRateBuilder in project jsr354-ri by JavaMoney.

the class ECBAbstractRateProvider method getExchangeRate.

@Override
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
    Objects.requireNonNull(conversionQuery);
    try {
        if (loadLock.await(30, TimeUnit.SECONDS)) {
            if (rates.isEmpty()) {
                return null;
            }
            RateResult result = findExchangeRate(conversionQuery);
            ExchangeRateBuilder builder = getBuilder(conversionQuery, result.date);
            ExchangeRate sourceRate = result.targets.get(conversionQuery.getBaseCurrency().getCurrencyCode());
            ExchangeRate target = result.targets.get(conversionQuery.getCurrency().getCurrencyCode());
            return createExchangeRate(conversionQuery, builder, sourceRate, target);
        } else {
            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 7 with ExchangeRateBuilder

use of org.javamoney.moneta.convert.ExchangeRateBuilder 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 8 with ExchangeRateBuilder

use of org.javamoney.moneta.convert.ExchangeRateBuilder in project jsr354-ri by JavaMoney.

the class IMFRateReadingHandler method saveExchangeRate.

private void saveExchangeRate(Map<CurrencyUnit, List<ExchangeRate>> currencyToSdr, Map<CurrencyUnit, List<ExchangeRate>> sdrToCurrency, boolean isCurrencyToSdr, List<LocalDate> timestamps, CurrencyUnit currency, Double[] values) {
    for (int index = 0; index < values.length; index++) {
        if (Objects.isNull(values[index]) || Objects.isNull(getLocalDateFromTS(timestamps, index))) {
            continue;
        }
        LocalDate fromTS = getLocalDateFromTS(timestamps, index);
        RateType rateType = getRateType(fromTS);
        if (isCurrencyToSdr) {
            ExchangeRate rate = new ExchangeRateBuilder(ConversionContextBuilder.create(context, rateType).set(fromTS).build()).setBase(currency).setTerm(IMFAbstractRateProvider.SDR).setFactor(new DefaultNumberValue(1D / values[index])).build();
            List<ExchangeRate> rates = currencyToSdr.computeIfAbsent(currency, c -> new ArrayList<>(5));
            rates.add(rate);
        } else {
            ExchangeRate rate = new ExchangeRateBuilder(ConversionContextBuilder.create(context, rateType).set(fromTS).build()).setBase(IMFAbstractRateProvider.SDR).setTerm(currency).setFactor(DefaultNumberValue.of(1D / values[index])).build();
            List<ExchangeRate> rates = sdrToCurrency.computeIfAbsent(currency, (c) -> new ArrayList<>(5));
            rates.add(rate);
        }
    }
}
Also used : ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) DefaultNumberValue(org.javamoney.moneta.spi.DefaultNumberValue) ExchangeRate(javax.money.convert.ExchangeRate) RateType(javax.money.convert.RateType) LocalDate(java.time.LocalDate)

Example 9 with ExchangeRateBuilder

use of org.javamoney.moneta.convert.ExchangeRateBuilder in project jsr354-ri 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)

Example 10 with ExchangeRateBuilder

use of org.javamoney.moneta.convert.ExchangeRateBuilder 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)

Aggregations

ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)14 ExchangeRate (javax.money.convert.ExchangeRate)10 MonetaryException (javax.money.MonetaryException)4 RateType (javax.money.convert.RateType)4 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)4 CurrencyUnit (javax.money.CurrencyUnit)3 BigDecimal (java.math.BigDecimal)2 LocalDate (java.time.LocalDate)2 Test (org.testng.annotations.Test)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1