Search in sources :

Example 6 with RateType

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);
}
Also used : ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) ExchangeRate(javax.money.convert.ExchangeRate) RateType(javax.money.convert.RateType)

Aggregations

RateType (javax.money.convert.RateType)6 ExchangeRate (javax.money.convert.ExchangeRate)4 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)4 ArrayList (java.util.ArrayList)2 ExchangeRateProvider (javax.money.convert.ExchangeRateProvider)2 ProviderContext (javax.money.convert.ProviderContext)2 ProviderContextBuilder (javax.money.convert.ProviderContextBuilder)2 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 LocalDate (java.time.LocalDate)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CurrencyUnit (javax.money.CurrencyUnit)1