use of org.javamoney.moneta.convert.ExchangeRateBuilder 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 org.javamoney.moneta.convert.ExchangeRateBuilder 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 org.javamoney.moneta.convert.ExchangeRateBuilder in project jsr354-ri-bp by JavaMoney.
the class IdentityRateProvider method getExchangeRate.
@Override
public ExchangeRate getExchangeRate(ConversionQuery query) {
if (query.getBaseCurrency().getCurrencyCode().equals(query.getCurrency().getCurrencyCode())) {
ExchangeRateBuilder builder = new ExchangeRateBuilder(getContext().getProviderName(), RateType.OTHER).setBase(query.getBaseCurrency());
builder.setTerm(query.getCurrency());
builder.setFactor(DefaultNumberValue.of(BigDecimal.ONE));
return builder.build();
}
return null;
}
use of org.javamoney.moneta.convert.ExchangeRateBuilder 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