use of javax.money.convert.ExchangeRate in project jsr354-ri-bp by JavaMoney.
the class ECBAbstractRateProvider method createExchangeRate.
private ExchangeRate createExchangeRate(ConversionQuery query, ExchangeRateBuilder builder, ExchangeRate sourceRate, ExchangeRate target) {
if (areBothBaseCurrencies(query)) {
builder.setFactor(DefaultNumberValue.ONE);
return builder.build();
} else if (BASE_CURRENCY_CODE.equals(query.getCurrency().getCurrencyCode())) {
if (sourceRate == null) {
return null;
}
return reverse(sourceRate);
} else if (BASE_CURRENCY_CODE.equals(query.getBaseCurrency().getCurrencyCode())) {
return target;
} else {
// Get Conversion base as derived rate: base -> EUR -> term
ExchangeRate rate1 = getExchangeRate(query.toBuilder().setTermCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE)).build());
ExchangeRate rate2 = getExchangeRate(query.toBuilder().setBaseCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE)).setTermCurrency(query.getCurrency()).build());
if (rate1 != null && rate2 != null) {
builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
builder.setRateChain(rate1, rate2);
return builder.build();
}
throw new CurrencyConversionException(query.getBaseCurrency(), query.getCurrency(), sourceRate.getContext());
}
}
use of javax.money.convert.ExchangeRate 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);
}
}
use of javax.money.convert.ExchangeRate 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);
}
use of javax.money.convert.ExchangeRate 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);
}
}
use of javax.money.convert.ExchangeRate 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");
}
Aggregations