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);
}
}
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);
}
}
}
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());
}
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);
}
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);
}
Aggregations