Search in sources :

Example 11 with ExchangeRateBuilder

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

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

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

the class IdentityRateProvider method getExchangeRate.

@Override
public ExchangeRate getExchangeRate(ConversionQuery query) {
    if (query.getBaseCurrency().getCurrencyCode().equals(query.getCurrency().getCurrencyCode())) {
        ExchangeRateBuilder builder = new ExchangeRateBuilder(getContext().getProviderName(), RateType.OTHER).setBase(query.getBaseCurrency());
        builder.setTerm(query.getCurrency());
        builder.setFactor(DefaultNumberValue.of(BigDecimal.ONE));
        return builder.build();
    }
    return null;
}
Also used : ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder)

Example 14 with ExchangeRateBuilder

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

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