use of javax.money.convert.RateType 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);
}
Aggregations