Search in sources :

Example 16 with ExchangeRate

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

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

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

the class MonetaryConversionTest method testGetExchangeRateProvider_Chained.

@Test
public void testGetExchangeRateProvider_Chained() throws InterruptedException {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider("IMF");
    assertTrue(Objects.nonNull(prov));
    assertEquals(IMFRateProvider.class, prov.getClass());
    // Test rate provided by IMF (derived)
    // wait for provider to load...
    Thread.sleep(5000L);
    ExchangeRate exchangeRate = prov.getExchangeRate(Monetary.getCurrency("USD"), Monetary.getCurrency("INR"));
    assertTrue(Objects.nonNull(exchangeRate));
    assertTrue(exchangeRate.isDerived());
    // Test rate provided by ECB
    exchangeRate = prov.getExchangeRate(Monetary.getCurrency("EUR"), Monetary.getCurrency("CHF"));
    assertTrue(Objects.nonNull(exchangeRate));
    assertTrue(exchangeRate.isDerived());
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) Test(org.testng.annotations.Test)

Example 19 with ExchangeRate

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

the class MonetaryConversionTest method testGetExchangeRateDefault.

@Test
public void testGetExchangeRateDefault() {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider();
    assertTrue(Objects.nonNull(prov));
    ExchangeRate rate = prov.getExchangeRate("CHF", "EUR");
    assertNotNull(rate);
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) Test(org.testng.annotations.Test)

Example 20 with ExchangeRate

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

the class DefaultExchangeRate method hashCode.

/*
     * (non-Javadoc)
     *
     * @see java.lang.Object#hashCode()
     */
@Override
public int hashCode() {
    // Numerically equal factors should produce the same hash code, so hash a normalized
    // version of the factor rather than the NumberValue object.
    BigDecimal normalizedFactor = factor.numberValue(BigDecimal.class).stripTrailingZeros();
    // The exchange rate chain includes a reference to "this" if the caller doesn't explicitly
    // set a chain in the builder, so we can't naively hash the chain or we'll get infinite
    // recursion.
    int chainHash = 0;
    for (ExchangeRate chainedRate : chain) {
        if (chainedRate == this) {
            // Use a constant to represent the presence of this object in the chain.
            chainHash = Objects.hash(chainHash, "this");
        } else {
            chainHash = Objects.hash(chainHash, chainedRate);
        }
    }
    return Objects.hash(base, conversionContext, normalizedFactor, term, chainHash);
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) BigDecimal(java.math.BigDecimal)

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