/** * Converts a {@code MultipleCurrencyAmount} into an amount in the * specified currency using the rates in this matrix. * * @param amount the {@code MultipleCurrencyAmount} to be converted * @param targetCurrency the currency to convert all entries to * @return the total amount in the requested currency */ public CurrencyAmount convert(MultiCurrencyAmount amount, Currency targetCurrency) { ArgChecker.notNull(amount, "amount"); ArgChecker.notNull(targetCurrency, "targetCurrency"); // We could do this using the currency amounts but to // avoid creating extra objects we'll use doubles double total = amount.getAmounts() .stream() .mapToDouble(ca -> convert(ca.getAmount(), ca.getCurrency(), targetCurrency)) .sum(); return CurrencyAmount.of(targetCurrency, total); }
/** * Converts a {@code CurrencyAmount} into an amount in the specified * currency using the rates in this matrix. * * @param amount the {@code CurrencyAmount} to be converted * @param targetCurrency the currency to convert the {@code CurrencyAmount} to * @return the amount converted to the requested currency */ public CurrencyAmount convert(CurrencyAmount amount, Currency targetCurrency) { ArgChecker.notNull(amount, "amount"); ArgChecker.notNull(targetCurrency, "targetCurrency"); // Only do conversion if we need to Currency originalCurrency = amount.getCurrency(); if (originalCurrency.equals(targetCurrency)) { return amount; } return CurrencyAmount.of(targetCurrency, convert(amount.getAmount(), originalCurrency, targetCurrency)); }
public void convertCurrencyAmount() { FxMatrix matrix = FxMatrix.builder() .addRate(GBP, EUR, 1.4) .addRate(GBP, USD, 1.6) .build(); CurrencyAmount amount = CurrencyAmount.of(GBP, 1600); assertThat(matrix.convert(amount, GBP)).isEqualTo(amount); assertThat(matrix.convert(amount, USD)) .hasCurrency(USD) .hasAmount(2560); assertThat(matrix.convert(amount, EUR)) .hasCurrency(EUR) .hasAmount(2240); }
public void convertMultipleCurrencyAmountWithNoEntries() { FxMatrix matrix = FxMatrix.builder() .addRate(GBP, EUR, 1.4) .addRate(GBP, USD, 1.6) .build(); MultiCurrencyAmount amount = MultiCurrencyAmount.of(); assertThat(matrix.convert(amount, GBP)) .hasCurrency(GBP) .hasAmount(0); assertThat(matrix.convert(amount, USD)) .hasCurrency(USD) .hasAmount(0); assertThat(matrix.convert(amount, EUR)) .hasCurrency(EUR) .hasAmount(0); }
public void convertMultipleCurrencyAmountWithSingleEntry() { FxMatrix matrix = FxMatrix.builder() .addRate(GBP, EUR, 1.4) .addRate(GBP, USD, 1.6) .build(); MultiCurrencyAmount amount = MultiCurrencyAmount.of(CurrencyAmount.of(GBP, 1600)); assertThat(matrix.convert(amount, GBP)) .hasCurrency(GBP) .hasAmount(1600); assertThat(matrix.convert(amount, USD)) .hasCurrency(USD) .hasAmount(2560); assertThat(matrix.convert(amount, EUR)) .hasCurrency(EUR) .hasAmount(2240); }
public void convertMultipleCurrencyAmountWithMultipleEntries() { FxMatrix matrix = FxMatrix.builder() .addRate(GBP, EUR, 1.4) .addRate(GBP, USD, 1.6) .build(); MultiCurrencyAmount amount = MultiCurrencyAmount.of( CurrencyAmount.of(GBP, 1600), CurrencyAmount.of(EUR, 1200), CurrencyAmount.of(USD, 1500)); assertThat(matrix.convert(amount, GBP)) .hasCurrency(GBP) .hasAmount(1600d + (1200 / 1.4) + (1500 / 1.6), TOL); assertThat(matrix.convert(amount, USD)) .hasCurrency(USD) .hasAmount((1600d * 1.6) + ((1200 / 1.4) * 1.6) + 1500); assertThat(matrix.convert(amount, EUR)) .hasCurrency(EUR) .hasAmount((1600d * 1.4) + 1200 + ((1500 / 1.6) * 1.4)); }