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