@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case -879772901: // amounts return ((CurrencyScenarioArray) bean).getAmounts(); } return super.propertyGet(bean, propertyName, quiet); }
/** * 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)); }
/** * Obtains an instance from the specified currency and array of values. * * @param amounts the amounts, one for each scenario * @return an instance with the specified currency and values */ public static CurrencyScenarioArray of(CurrencyAmountArray amounts) { return new CurrencyScenarioArray(amounts); }
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 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); }
@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); }
/** * Returns a collector which creates a multi currency scenario array by combining a stream of * currency scenario arrays. * <p> * The arrays in the stream must all have the same length. * * @return the collector */ public static Collector<CurrencyScenarioArray, ?, MultiCurrencyScenarioArray> toMultiCurrencyScenarioArray() { return Collector.<CurrencyScenarioArray, Map<Currency, CurrencyAmountArray>, MultiCurrencyScenarioArray>of( // accumulate into a map HashMap::new, (map, ca) -> map.merge(ca.getCurrency(), ca.getAmounts(), CurrencyAmountArray::plus), // combine two maps (map1, map2) -> { map2.values().forEach((ca2) -> map1.merge(ca2.getCurrency(), ca2, CurrencyAmountArray::plus)); return map1; }, // convert to MultiCurrencyScenarioArray map -> { Map<Currency, DoubleArray> currencyArrayMap = MapStream.of(map).mapValues(caa -> caa.getValues()).toMap(); return MultiCurrencyScenarioArray.of(currencyArrayMap); }, UNORDERED); }
/** * 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 convertIntoAnExistingCurrency() { FxRateScenarioArray rates1 = FxRateScenarioArray.of(USD, GBP, DoubleArray.of(1 / 1.50, 1 / 1.51, 1 / 1.52)); FxRateScenarioArray rates2 = FxRateScenarioArray.of(EUR, GBP, DoubleArray.of(0.7, 0.7, 0.7)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates1, rates2); CurrencyScenarioArray convertedArray = VALUES_ARRAY.convertedTo(Currency.GBP, fxProvider); assertThat(convertedArray.getCurrency()).isEqualTo(Currency.GBP); double[] expected = new double[] { 20 + 30 / 1.50 + 40 * 0.7, 21 + 32 / 1.51 + 43 * 0.7, 22 + 33 / 1.52 + 44 * 0.7}; for (int i = 0; i < 3; i++) { assertThat(convertedArray.get(i).getAmount()).isEqualTo(expected[i], offset(1e-6)); } }
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)); }
/** * 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)); }
/** * 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)); }
/** * 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"); }
public void convert() { FxRateScenarioArray rates1 = FxRateScenarioArray.of(GBP, CAD, DoubleArray.of(2.00, 2.01, 2.02)); FxRateScenarioArray rates2 = FxRateScenarioArray.of(USD, CAD, DoubleArray.of(1.30, 1.31, 1.32)); FxRateScenarioArray rates3 = FxRateScenarioArray.of(EUR, CAD, DoubleArray.of(1.4, 1.4, 1.4)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates1, rates2, rates3); CurrencyScenarioArray convertedArray = VALUES_ARRAY.convertedTo(Currency.CAD, fxProvider); DoubleArray expected = DoubleArray.of( 20 * 2.00 + 30 * 1.30 + 40 * 1.4, 21 * 2.01 + 32 * 1.31 + 43 * 1.4, 22 * 2.02 + 33 * 1.32 + 44 * 1.4); assertThat(convertedArray.getAmounts().getValues()).isEqualTo(expected); }
@Override public CurrencyScenarioArray build() { return new CurrencyScenarioArray( 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 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); }
/** * 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)); }