/** * Access a {@link CurrencyConversion} that can be applied as a * {@link MonetaryOperator} to an amount. * * @param termCode terminal/target currency code, not {@code null} * @return a new instance of a corresponding {@link CurrencyConversion}, * never {@code null}. * @throws MonetaryException if one of the currency codes passed is not valid. */ default CurrencyConversion getCurrencyConversion(String termCode){ return getCurrencyConversion(Monetary.getCurrency(termCode)); }
/** * Access an instance of {@link CurrencyConversion}. * * @param conversionQuery the {@link javax.money.convert.ConversionQuery} determining the type of conversion * required, not null. * @return the corresponding conversion, not null. * @throws javax.money.MonetaryException if no matching conversion could be found. * @see #isConversionAvailable(javax.money.convert.ConversionQuery) */ default CurrencyConversion getConversion(ConversionQuery conversionQuery) { return getExchangeRateProvider(conversionQuery).getCurrencyConversion( Objects.requireNonNull(conversionQuery.getCurrency(), "Terminating Currency is required.") ); }
/** * Access a {@link CurrencyConversion} that can be applied as a * {@link MonetaryOperator} to an amount. * * @param term term {@link CurrencyUnit}, not {@code null} * @return a new instance of a corresponding {@link CurrencyConversion}, * never {@code null}. */ default CurrencyConversion getCurrencyConversion(CurrencyUnit term){ return getCurrencyConversion(ConversionQueryBuilder.of().setTermCurrency(term).build()); }
public static void main(String[] args) { ExchangeRateProvider imfRateProvider = MonetaryConversions .getExchangeRateProvider("IMF"); ExchangeRateProvider ecbRateProvider = MonetaryConversions .getExchangeRateProvider("ECB"); CurrencyUnit real = Monetary.getCurrency("BRL"); CurrencyUnit dollar = Monetary.getCurrency(Locale.US); CurrencyConversion ecbDollarConvertion = ecbRateProvider .getCurrencyConversion(dollar); CurrencyConversion imfDollarConvertion = imfRateProvider .getCurrencyConversion(dollar); MonetaryAmount money = Money.of(10, real); System.out.println(money.with(ecbDollarConvertion)); System.out.println(money.with(imfDollarConvertion)); }
/** * comparator to sort the {@link MonetaryAmount} considering the * {@link ExchangeRate} * @param provider the rate provider to be used, not null. * @return the sort of {@link MonetaryAmount} using {@link ExchangeRate} */ public static Comparator<? super MonetaryAmount> sortValuable( ExchangeRateProvider provider) { return (m1, m2) -> { CurrencyConversion conversion = provider.getCurrencyConversion(m1 .getCurrency()); return m1.compareTo(conversion.apply(m2)); }; }
/** * return the sum and convert all values to specific currency using the * provider, if necessary * @param provider the rate provider to be used, not null. * @param currency * currency * @return the list convert to specific currency unit */ public static BinaryOperator<MonetaryAmount> sum( ExchangeRateProvider provider, CurrencyUnit currency) { CurrencyConversion currencyConversion = provider .getCurrencyConversion(currency); return (m1, m2) -> currencyConversion.apply(m1).add( currencyConversion.apply(m2)); }
/** * return the minimum value, if the monetary amounts have different * currencies, will converter first using the given ExchangeRateProvider * @param provider * the ExchangeRateProvider to convert the currencies * @return the minimum value */ public static BinaryOperator<MonetaryAmount> min( ExchangeRateProvider provider) { return (m1, m2) -> { CurrencyConversion conversion = provider.getCurrencyConversion(m1 .getCurrency()); if (m1.isGreaterThan(conversion.apply(m2))) { return m2; } return m1; }; }
/** * return the maximum value, if the monetary amounts have different * currencies, will converter first using the given ExchangeRateProvider * @param provider * the ExchangeRateProvider to convert the currencies * @return the maximum value */ public static BinaryOperator<MonetaryAmount> max( ExchangeRateProvider provider) { return (m1, m2) -> { CurrencyConversion conversion = provider .getCurrencyConversion(m1.getCurrency()); if (m1.isGreaterThan(conversion.apply(m2))) { return m1; } return m2; }; }