JavaMoney / jsr354-ri

JSR 354 - Moneta: Reference Implementation
Other
344 stars 101 forks source link

Add possibility to retrieve all ExchangeRates from an ExchangeRateProvider #321

Closed Thibstars closed 4 years ago

Thibstars commented 4 years ago

Add a method (ExchangeRateProvider#getExchangeRates) that returns a collection of all the exchange rates from an ExchangeRateProvider.

Currently it is only possible to get an exchange rate of a specific currency.

keilw commented 4 years ago

Has this method be found not relevant, or did you come across another way to do it?

Thibstars commented 4 years ago

Has this method be found not relevant, or did you come across another way to do it?

I might have been a bit fast requesting this feature, I have found another way to achieve my goal.

Here is my current approach (I can use any provider with this solution): `

    ExchangeRateProvider rateProvider = MonetaryConversions.getExchangeRateProvider();

        Collection<CurrencyUnit> currencies = Monetary.getCurrencies();

        List<ExchangeRate> exchangeRates = currencies.stream()
            .filter(currency -> rateProvider.isAvailable(DEFAULT_BASE_CURRENCY_CODE, currency.getCurrencyCode()))
            .map(currency -> {
                try {
                    return rateProvider.getExchangeRate(DEFAULT_BASE_CURRENCY_CODE, currency.getCurrencyCode());
                } catch (Exception e) {
                    return null;
                }
            })
            .filter(Objects::nonNull)
            .sorted(Comparator.comparing(ExchangeRate::getFactor))
            .collect(Collectors.toList());`