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);
}
}
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.");
}
}
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;
}
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));
}
}
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.");
}
}
Aggregations