/** * Obtains an instance from the specified list of amounts. * <p> * All amounts must have the same currency. * * @param amounts the amounts, one for each scenario * @return an instance with the specified amounts * @throws IllegalArgumentException if multiple currencies are found */ public static CurrencyScenarioArray of(List<CurrencyAmount> amounts) { return new CurrencyScenarioArray(CurrencyAmountArray.of(amounts)); }
/** * Obtains an instance from the specified currency and array of values. * * @param currency the currency of the values * @param values the values, one for each scenario * @return an instance with the specified currency and values */ public static CurrencyScenarioArray of(Currency currency, DoubleArray values) { return new CurrencyScenarioArray(CurrencyAmountArray.of(currency, values)); }
@Override public CurrencyAmountArray convertedTo(Currency resultCurrency, FxRateProvider fxRateProvider) { double[] singleCurrencyValues = new double[size]; for (Map.Entry<Currency, DoubleArray> entry : values.entrySet()) { Currency currency = entry.getKey(); DoubleArray currencyValues = entry.getValue(); for (int i = 0; i < size; i++) { singleCurrencyValues[i] += currencyValues.get(i) * fxRateProvider.fxRate(currency, resultCurrency); } } return CurrencyAmountArray.of(resultCurrency, DoubleArray.ofUnsafe(singleCurrencyValues)); }
public void test_convertedTo() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyAmountArray test = CurrencyAmountArray.of(GBP, values); FxRate fxRate = FxRate.of(GBP, USD, 1.61); CurrencyAmountArray convertedList = test.convertedTo(USD, fxRate); DoubleArray expectedValues = DoubleArray.of(1 * 1.61, 2 * 1.61, 3 * 1.61); CurrencyAmountArray expectedList = CurrencyAmountArray.of(USD, expectedValues); assertThat(convertedList).isEqualTo(expectedList); }
public void collector() { List<CurrencyAmountArray> arrays = ImmutableList.of( CurrencyAmountArray.of(USD, DoubleArray.of(10, 20, 30)), CurrencyAmountArray.of(USD, DoubleArray.of(5, 6, 7)), CurrencyAmountArray.of(EUR, DoubleArray.of(2, 4, 6)), CurrencyAmountArray.of(GBP, DoubleArray.of(11, 12, 13)), CurrencyAmountArray.of(GBP, DoubleArray.of(1, 2, 3))); Map<Currency, DoubleArray> expectedMap = ImmutableMap.of( USD, DoubleArray.of(15, 26, 37), EUR, DoubleArray.of(2, 4, 6), GBP, DoubleArray.of(12, 14, 16)); MultiCurrencyAmountArray expected = MultiCurrencyAmountArray.of(expectedMap); assertThat(arrays.stream().collect(toMultiCurrencyAmountArray())).isEqualTo(expected); }
public void test_minus_currencyAmount() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyAmountArray array = CurrencyAmountArray.of(GBP, values); CurrencyAmountArray result = array.minus(CurrencyAmount.of(GBP, 0.5)); assertThat(result).isEqualTo(CurrencyAmountArray.of(GBP, DoubleArray.of(0.5, 1.5, 2.5))); }
public void total() { List<CurrencyAmountArray> arrays = ImmutableList.of( CurrencyAmountArray.of(USD, DoubleArray.of(10, 20, 30)), CurrencyAmountArray.of(USD, DoubleArray.of(5, 6, 7)), CurrencyAmountArray.of(EUR, DoubleArray.of(2, 4, 6)), CurrencyAmountArray.of(GBP, DoubleArray.of(11, 12, 13)), CurrencyAmountArray.of(GBP, DoubleArray.of(1, 2, 3))); Map<Currency, DoubleArray> expectedMap = ImmutableMap.of( USD, DoubleArray.of(15, 26, 37), EUR, DoubleArray.of(2, 4, 6), GBP, DoubleArray.of(12, 14, 16)); MultiCurrencyAmountArray expected = MultiCurrencyAmountArray.of(expectedMap); assertThat(MultiCurrencyAmountArray.total(arrays)).isEqualTo(expected); }
public void test_convertedTo_noConversionNecessary() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyAmountArray test = CurrencyAmountArray.of(GBP, values); FxRate fxRate = FxRate.of(GBP, USD, 1.61); CurrencyAmountArray convertedList = test.convertedTo(GBP, fxRate); assertThat(convertedList).isEqualTo(test); }
public void test_serialization() { CurrencyAmountArray test = CurrencyAmountArray.of(GBP, DoubleArray.of(1, 2, 3)); assertSerialization(test); }
/** * Returns a new array containing the values from this array with the specified amount added. * <p> * The amount is added to each element in this array. * The currency must be the same as the currency of this array. * * @param amount the amount to add * @return a new array containing the values from this array with the specified amount added * @throws IllegalArgumentException if the array and the amount have different currencies */ public CurrencyAmountArray plus(CurrencyAmount amount) { if (!amount.getCurrency().equals(currency)) { throw new IllegalArgumentException(Messages.format( "Currencies must be equal, this currency is {}, other currency is {}", currency, amount.getCurrency())); } return CurrencyAmountArray.of(currency, values.plus(amount.getAmount())); }
/** * Returns a new array containing the values from this array with the specified amount subtracted. * <p> * The amount is subtracted from each element in this array. * The currency must be the same as the currency of this array. * * @param amount the amount to subtract * @return a new array containing the values from this array with the specified amount subtracted * @throws IllegalArgumentException if the array and the amount have different currencies */ public CurrencyAmountArray minus(CurrencyAmount amount) { if (!amount.getCurrency().equals(currency)) { throw new IllegalArgumentException(Messages.format( "Currencies must be equal, this currency is {}, other currency is {}", currency, amount.getCurrency())); } return CurrencyAmountArray.of(currency, values.minus(amount.getAmount())); }
public void coverage() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyAmountArray test = CurrencyAmountArray.of(GBP, values); coverImmutableBean(test); DoubleArray values2 = DoubleArray.of(1, 2, 3); CurrencyAmountArray test2 = CurrencyAmountArray.of(USD, values2); coverBeanEquals(test, test2); }
public void test_of_CurrencyDoubleArray() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyAmountArray test = CurrencyAmountArray.of(GBP, values); assertThat(test.getCurrency()).isEqualTo(GBP); assertThat(test.getValues()).isEqualTo(values); assertThat(test.size()).isEqualTo(3); assertThat(test.get(0)).isEqualTo(CurrencyAmount.of(GBP, 1)); assertThat(test.get(1)).isEqualTo(CurrencyAmount.of(GBP, 2)); assertThat(test.get(2)).isEqualTo(CurrencyAmount.of(GBP, 3)); assertThat(test.stream().collect(toList())).containsExactly( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(GBP, 2), CurrencyAmount.of(GBP, 3)); }
public void test_of_function_mixedCurrency() { List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(USD, 2), CurrencyAmount.of(GBP, 3)); assertThrowsIllegalArg(() -> CurrencyAmountArray.of(3, i -> values.get(i))); }
public void test_plus() { List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(USD, 2), CurrencyAmount.of(GBP, 3)); assertThrowsIllegalArg(() -> CurrencyAmountArray.of(3, i -> values.get(i))); }
public void test_of_List() { List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(GBP, 2), CurrencyAmount.of(GBP, 3)); CurrencyAmountArray test = CurrencyAmountArray.of(values); assertThat(test.getCurrency()).isEqualTo(GBP); assertThat(test.getValues()).isEqualTo(DoubleArray.of(1d, 2d, 3d)); assertThat(test.size()).isEqualTo(3); assertThat(test.get(0)).isEqualTo(CurrencyAmount.of(GBP, 1)); assertThat(test.get(1)).isEqualTo(CurrencyAmount.of(GBP, 2)); assertThat(test.get(2)).isEqualTo(CurrencyAmount.of(GBP, 3)); assertThat(test.stream().collect(toList())).containsExactly( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(GBP, 2), CurrencyAmount.of(GBP, 3)); }
public void test_of_function() { List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(GBP, 2), CurrencyAmount.of(GBP, 3)); CurrencyAmountArray test = CurrencyAmountArray.of(3, i -> values.get(i)); assertThat(test.getCurrency()).isEqualTo(GBP); assertThat(test.getValues()).isEqualTo(DoubleArray.of(1d, 2d, 3d)); assertThat(test.size()).isEqualTo(3); assertThat(test.get(0)).isEqualTo(CurrencyAmount.of(GBP, 1)); assertThat(test.get(1)).isEqualTo(CurrencyAmount.of(GBP, 2)); assertThat(test.get(2)).isEqualTo(CurrencyAmount.of(GBP, 3)); assertThat(test.stream().collect(toList())).containsExactly( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(GBP, 2), CurrencyAmount.of(GBP, 3)); }
public void test_of_CurrencyList_mixedCurrency() { List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(USD, 2), CurrencyAmount.of(GBP, 3)); assertThrowsIllegalArg(() -> CurrencyAmountArray.of(values)); }
public void test_convertedTo_missingFxRate() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyAmountArray test = CurrencyAmountArray.of(GBP, values); FxRate fxRate = FxRate.of(EUR, USD, 1.61); assertThrows(() -> test.convertedTo(USD, fxRate), IllegalArgumentException.class); }