/** * 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 CurrencyScenarioArray plus(CurrencyAmount amount) { return CurrencyScenarioArray.of(amounts.plus(amount)); }
/** * 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 CurrencyScenarioArray minus(CurrencyAmount amount) { return CurrencyScenarioArray.of(amounts.minus(amount)); }
/** * Returns a new array containing the values from this array with the values from the other array subtracted. * <p> * The amounts are subtracted from the matching element in this array. * The currency must be the same as the currency of this array. * The arrays must have the same size. * * @param other another array of multiple currency values. * @return a new array containing the values from this array with the values from the other array subtracted * @throws IllegalArgumentException if the arrays have different sizes or different currencies */ public CurrencyScenarioArray minus(CurrencyScenarioArray other) { return CurrencyScenarioArray.of(amounts.minus(other.amounts)); }
/** * Returns a new array containing the values from this array added to the values in the other array. * <p> * The amounts are added to the matching element in this array. * The currency must be the same as the currency of this array. * The arrays must have the same size. * * @param other another array of multiple currency values. * @return a new array containing the values from this array added to the values in the other array * @throws IllegalArgumentException if the arrays have different sizes or different currencies */ public CurrencyScenarioArray plus(CurrencyScenarioArray other) { return CurrencyScenarioArray.of(amounts.plus(other.amounts)); }
/** * Test the plus() methods work as expected. */ public void plus() { CurrencyScenarioArray currencyScenarioArray = CurrencyScenarioArray.of(GBP, DoubleArray.of(1, 2, 3)); CurrencyScenarioArray arrayToAdd = CurrencyScenarioArray.of(GBP, DoubleArray.of(4, 5, 6)); CurrencyScenarioArray plusArraysResult = currencyScenarioArray.plus(arrayToAdd); assertThat(plusArraysResult).isEqualTo(CurrencyScenarioArray.of(GBP, DoubleArray.of(5, 7, 9))); CurrencyAmount amountToAdd = CurrencyAmount.of(Currency.GBP, 10); CurrencyScenarioArray plusAmountResult = currencyScenarioArray.plus(amountToAdd); assertThat(plusAmountResult).isEqualTo(CurrencyScenarioArray.of(GBP, DoubleArray.of(11, 12, 13))); }
/** * Test the minus() methods work as expected. */ public void minus() { CurrencyScenarioArray currencyScenarioArray = CurrencyScenarioArray.of(GBP, DoubleArray.of(1, 2, 3)); CurrencyScenarioArray arrayToSubtract = CurrencyScenarioArray.of(GBP, DoubleArray.of(3, 2, 1)); CurrencyScenarioArray minusArrayResult = currencyScenarioArray.minus(arrayToSubtract); assertThat(minusArrayResult).isEqualTo(CurrencyScenarioArray.of(GBP, DoubleArray.of(-2, 0, 2))); CurrencyAmount amountToSubtract = CurrencyAmount.of(Currency.GBP, 2); CurrencyScenarioArray minusAmountResult = currencyScenarioArray.minus(amountToSubtract); assertThat(minusAmountResult).isEqualTo(CurrencyScenarioArray.of(GBP, DoubleArray.of(-1, 0, 1))); }
public void collectorDifferentArrayLengths() { List<CurrencyScenarioArray> arrays = ImmutableList.of( CurrencyScenarioArray.of(USD, DoubleArray.of(10, 20, 30)), CurrencyScenarioArray.of(GBP, DoubleArray.of(1, 2))); assertThrowsIllegalArg(() -> arrays.stream().collect(toMultiCurrencyScenarioArray())); }
public void total() { List<CurrencyScenarioArray> arrays = ImmutableList.of( CurrencyScenarioArray.of(USD, DoubleArray.of(10, 20, 30)), CurrencyScenarioArray.of(USD, DoubleArray.of(5, 6, 7)), CurrencyScenarioArray.of(EUR, DoubleArray.of(2, 4, 6)), CurrencyScenarioArray.of(GBP, DoubleArray.of(11, 12, 13)), CurrencyScenarioArray.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)); MultiCurrencyScenarioArray expected = MultiCurrencyScenarioArray.of(expectedMap); assertThat(MultiCurrencyScenarioArray.total(arrays)).isEqualTo(expected); }
public void coverage() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyScenarioArray test = CurrencyScenarioArray.of(GBP, values); coverImmutableBean(test); DoubleArray values2 = DoubleArray.of(1, 2, 3); CurrencyScenarioArray test2 = CurrencyScenarioArray.of(USD, values2); coverBeanEquals(test, test2); }
public void collector() { List<CurrencyScenarioArray> arrays = ImmutableList.of( CurrencyScenarioArray.of(USD, DoubleArray.of(10, 20, 30)), CurrencyScenarioArray.of(USD, DoubleArray.of(5, 6, 7)), CurrencyScenarioArray.of(EUR, DoubleArray.of(2, 4, 6)), CurrencyScenarioArray.of(GBP, DoubleArray.of(11, 12, 13)), CurrencyScenarioArray.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)); MultiCurrencyScenarioArray expected = MultiCurrencyScenarioArray.of(expectedMap); assertThat(arrays.stream().collect(toMultiCurrencyScenarioArray())).isEqualTo(expected); }
public void create_fromFunction_mixedCurrency() { List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(USD, 2), CurrencyAmount.of(GBP, 3)); assertThrowsIllegalArg(() -> CurrencyScenarioArray.of(3, i -> values.get(i))); }
/** * Test that values are converted to the reporting currency using the rates in the market data. */ public void convert() { DoubleArray values = DoubleArray.of(1, 2, 3); FxRateScenarioArray rates = FxRateScenarioArray.of(GBP, USD, DoubleArray.of(1.61, 1.62, 1.63)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates); CurrencyScenarioArray test = CurrencyScenarioArray.of(GBP, values); CurrencyScenarioArray convertedList = test.convertedTo(USD, fxProvider); DoubleArray expectedValues = DoubleArray.of(1 * 1.61, 2 * 1.62, 3 * 1.63); CurrencyScenarioArray expectedList = CurrencyScenarioArray.of(USD, expectedValues); assertThat(convertedList).isEqualTo(expectedList); }
public void create_fromFunction() { List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(GBP, 2), CurrencyAmount.of(GBP, 3)); CurrencyScenarioArray test = CurrencyScenarioArray.of(3, i -> values.get(i)); assertThat(test.getCurrency()).isEqualTo(GBP); assertThat(test.getAmounts().getValues()).isEqualTo(DoubleArray.of(1d, 2d, 3d)); assertThat(test.getScenarioCount()).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 create() { DoubleArray values = DoubleArray.of(1, 2, 3); CurrencyScenarioArray test = CurrencyScenarioArray.of(GBP, values); assertThat(test.getCurrency()).isEqualTo(GBP); assertThat(test.getAmounts().getValues()).isEqualTo(values); assertThat(test.getScenarioCount()).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)); }
/** * Test that no conversion is done and no rates are used if the values are already in the reporting currency. */ public void noConversionNecessary() { DoubleArray values = DoubleArray.of(1, 2, 3); FxRateScenarioArray rates = FxRateScenarioArray.of(GBP, USD, DoubleArray.of(1.61, 1.62, 1.63)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates); CurrencyScenarioArray test = CurrencyScenarioArray.of(GBP, values); CurrencyScenarioArray convertedList = test.convertedTo(GBP, fxProvider); assertThat(convertedList).isEqualTo(test); }
public void create_fromList() { List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(GBP, 2), CurrencyAmount.of(GBP, 3)); CurrencyScenarioArray test = CurrencyScenarioArray.of(values); assertThat(test.getCurrency()).isEqualTo(GBP); assertThat(test.getAmounts().getValues()).isEqualTo(DoubleArray.of(1d, 2d, 3d)); assertThat(test.getScenarioCount()).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 create_fromList_mixedCurrency() { List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(GBP, 1), CurrencyAmount.of(USD, 2), CurrencyAmount.of(GBP, 3)); assertThrowsIllegalArg(() -> CurrencyScenarioArray.of(values)); }
@Override public CurrencyScenarioArray convertedTo(Currency reportingCurrency, ScenarioFxRateProvider fxRateProvider) { if (getCurrency().equals(reportingCurrency)) { return this; } if (fxRateProvider.getScenarioCount() != amounts.size()) { throw new IllegalArgumentException(Messages.format( "Expected {} FX rates but received {}", amounts.size(), fxRateProvider.getScenarioCount())); } DoubleArray convertedValues = amounts.getValues().mapWithIndex((i, v) -> v * fxRateProvider.fxRate(getCurrency(), reportingCurrency, i)); return of(reportingCurrency, convertedValues); }
/** * Test the expected exception is thrown if there are not the same number of rates as there are values. */ public void wrongNumberOfFxRates() { DoubleArray values = DoubleArray.of(1, 2, 3); FxRateScenarioArray rates = FxRateScenarioArray.of(GBP, USD, DoubleArray.of(1.61, 1.62)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates); CurrencyScenarioArray test = CurrencyScenarioArray.of(GBP, values); assertThrows( () -> test.convertedTo(USD, fxProvider), IllegalArgumentException.class, "Expected 3 FX rates but received 2"); }
/** * Test the expected exception is thrown when there are no FX rates available to convert the values. */ public void missingFxRates() { DoubleArray values = DoubleArray.of(1, 2, 3); FxRateScenarioArray rates = FxRateScenarioArray.of(EUR, USD, DoubleArray.of(1.61, 1.62, 1.63)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates); CurrencyScenarioArray test = CurrencyScenarioArray.of(GBP, values); assertThrows(() -> test.convertedTo(USD, fxProvider), IllegalArgumentException.class); }