Search in sources :

Example 1 with ConversionContext

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

the class ExchangeRateBuilderTest method shouldReturnNPEWhenConversionContextIsNull.

@Test(expectedExceptions = NullPointerException.class)
public void shouldReturnNPEWhenConversionContextIsNull() {
    ConversionContext context = null;
    new ExchangeRateBuilder(context).setBase(CURRENCY).setTerm(CURRENCY).setFactor(NUMBER_FACTOR).build();
}
Also used : ConversionContext(javax.money.convert.ConversionContext) Test(org.testng.annotations.Test)

Example 2 with ConversionContext

use of javax.money.convert.ConversionContext 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 3 with ConversionContext

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

the class ExchangeRateBuilderTest method shouldReturnNPEWhenConversionContextIsNull.

@Test(expectedExceptions = NullPointerException.class)
public void shouldReturnNPEWhenConversionContextIsNull() {
    ConversionContext context = null;
    new ExchangeRateBuilder(context).setBase(CURRENCY).setTerm(CURRENCY).setFactor(NUMBER_FACTOR).build();
}
Also used : ConversionContext(javax.money.convert.ConversionContext) Test(org.testng.annotations.Test)

Aggregations

ConversionContext (javax.money.convert.ConversionContext)3 Test (org.testng.annotations.Test)2 LocalDate (java.time.LocalDate)1 CurrencyUnit (javax.money.CurrencyUnit)1 MonetaryException (javax.money.MonetaryException)1 ExchangeRate (javax.money.convert.ExchangeRate)1 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)1