Search in sources :

Example 11 with ExchangeRate

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

Example 12 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri by JavaMoney.

the class ECBAbstractRateProvider method findExchangeRate.

private RateResult findExchangeRate(ConversionQuery conversionQuery) {
    LocalDate[] dates = getQueryDates(conversionQuery);
    if (dates == null) {
        Comparator<LocalDate> comparator = Comparator.naturalOrder();
        LocalDate date = this.rates.keySet().stream().sorted(comparator.reversed()).findFirst().orElseThrow(() -> new MonetaryException("There is not more recent exchange rate to  rate on ECBRateProvider."));
        return new RateResult(date, this.rates.get(date));
    } else {
        for (LocalDate localDate : dates) {
            Map<String, ExchangeRate> targets = this.rates.get(localDate);
            if (Objects.nonNull(targets)) {
                return new RateResult(localDate, targets);
            }
        }
        String datesOnErros = Stream.of(dates).map(date -> date.format(DateTimeFormatter.ISO_LOCAL_DATE)).collect(Collectors.joining(","));
        throw new MonetaryException("There is not exchange on day " + datesOnErros + " to rate to  rate on ECBRateProvider.");
    }
}
Also used : LoaderService(org.javamoney.moneta.spi.LoaderService) CurrencyUnit(javax.money.CurrencyUnit) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ProviderContext(javax.money.convert.ProviderContext) Level(java.util.logging.Level) LoaderListener(org.javamoney.moneta.spi.LoaderService.LoaderListener) Map(java.util.Map) SAXParser(javax.xml.parsers.SAXParser) ExchangeRate(javax.money.convert.ExchangeRate) AbstractRateProvider(org.javamoney.moneta.spi.AbstractRateProvider) Monetary(javax.money.Monetary) MathContext(java.math.MathContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MonetaryException(javax.money.MonetaryException) ConversionQuery(javax.money.convert.ConversionQuery) ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) DefaultNumberValue(org.javamoney.moneta.spi.DefaultNumberValue) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) CurrencyConversionException(javax.money.convert.CurrencyConversionException) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Comparator(java.util.Comparator) Bootstrap(javax.money.spi.Bootstrap) InputStream(java.io.InputStream) ExchangeRate(javax.money.convert.ExchangeRate) LocalDate(java.time.LocalDate) MonetaryException(javax.money.MonetaryException)

Example 13 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri by JavaMoney.

the class IMFHistoricRateProvider method combine.

private Map<CurrencyUnit, List<ExchangeRate>> combine(Map<CurrencyUnit, List<ExchangeRate>> source, Map<CurrencyUnit, List<ExchangeRate>> destination) {
    for (CurrencyUnit currency : source.keySet()) {
        destination.putIfAbsent(currency, new ArrayList<>());
        List<ExchangeRate> rates = source.get(currency);
        destination.merge(currency, rates, IMFHistoricRateProvider::merge);
    }
    return destination;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) ExchangeRate(javax.money.convert.ExchangeRate)

Example 14 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri by JavaMoney.

the class AbstractCurrencyConversion method apply.

/**
 * Method that converts the source {@link MonetaryAmount} to an
 * {@link MonetaryAmount} based on the {@link ExchangeRate} of this
 * conversion.
 * @param amount The source amount
 * @return The converted amount, never null.
 * @throws CurrencyConversionException if conversion failed, or the required data is not available.
 * @see #getExchangeRate(MonetaryAmount)
 */
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
    if (termCurrency.equals(Objects.requireNonNull(amount).getCurrency())) {
        return amount;
    }
    ExchangeRate rate = getExchangeRate(amount);
    if (Objects.isNull(rate) || !amount.getCurrency().equals(rate.getBaseCurrency())) {
        throw new CurrencyConversionException(amount.getCurrency(), this.termCurrency, null);
    }
    NumberValue factor = rate.getFactor();
    factor = roundFactor(amount, factor);
    Integer scale = rate.getContext().get(KEY_SCALE, Integer.class);
    if (Objects.isNull(scale) || scale < 0) {
        return amount.multiply(factor).getFactory().setCurrency(rate.getCurrency()).create();
    } else {
        return amount.multiply(factor).getFactory().setCurrency(rate.getCurrency()).create().with(MonetaryOperators.rounding(scale));
    }
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) NumberValue(javax.money.NumberValue) CurrencyConversionException(javax.money.convert.CurrencyConversionException)

Example 15 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri by JavaMoney.

the class IMFAbstractRateProvider method getExchangeRate.

private ExchangeRate getExchangeRate(List<ExchangeRate> rates, final LocalDate[] dates) {
    if (Objects.isNull(rates)) {
        return null;
    }
    if (Objects.isNull(dates)) {
        return rates.stream().sorted(COMPARATOR_EXCHANGE_BY_LOCAL_DATE.reversed()).findFirst().orElseThrow(() -> new MonetaryException("There is not more recent exchange rate to  rate on IMFRateProvider."));
    } else {
        for (LocalDate localDate : dates) {
            Predicate<ExchangeRate> filter = rate -> rate.getContext().get(LocalDate.class).equals(localDate);
            Optional<ExchangeRate> exchangeRateOptional = rates.stream().filter(filter).findFirst();
            if (exchangeRateOptional.isPresent()) {
                return exchangeRateOptional.get();
            }
        }
        String datesOnErros = Stream.of(dates).map(date -> date.format(DateTimeFormatter.ISO_LOCAL_DATE)).collect(Collectors.joining(","));
        throw new MonetaryException("There is not exchange on day " + datesOnErros + " to rate to  rate on IFMRateProvider.");
    }
}
Also used : CurrencyContextBuilder(javax.money.CurrencyContextBuilder) CurrencyUnit(javax.money.CurrencyUnit) HashMap(java.util.HashMap) Currency(java.util.Currency) ProviderContext(javax.money.convert.ProviderContext) LoaderListener(org.javamoney.moneta.spi.LoaderService.LoaderListener) Locale(java.util.Locale) RateIMFResult(org.javamoney.moneta.convert.internal.imf.IMFRateReadingHandler.RateIMFResult) Map(java.util.Map) ExchangeRate(javax.money.convert.ExchangeRate) AbstractRateProvider(org.javamoney.moneta.spi.AbstractRateProvider) Monetary(javax.money.Monetary) CurrencyUnitBuilder(org.javamoney.moneta.CurrencyUnitBuilder) Predicate(java.util.function.Predicate) MonetaryException(javax.money.MonetaryException) ConversionQuery(javax.money.convert.ConversionQuery) ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ConversionContext(javax.money.convert.ConversionContext) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) ExchangeRate(javax.money.convert.ExchangeRate) LocalDate(java.time.LocalDate) MonetaryException(javax.money.MonetaryException)

Aggregations

ExchangeRate (javax.money.convert.ExchangeRate)30 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)12 Test (org.testng.annotations.Test)12 BigDecimal (java.math.BigDecimal)9 CurrencyUnit (javax.money.CurrencyUnit)8 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)7 MonetaryException (javax.money.MonetaryException)6 CurrencyConversionException (javax.money.convert.CurrencyConversionException)5 LocalDate (java.time.LocalDate)4 RateType (javax.money.convert.RateType)4 Map (java.util.Map)3 InputStream (java.io.InputStream)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Objects (java.util.Objects)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2