@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 575402001: // currency return ((CurrencyAmountArray) bean).getCurrency(); case -823812830: // values return ((CurrencyAmountArray) bean).getValues(); } return super.propertyGet(bean, propertyName, quiet); }
/** * 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)); }
/** * 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 CurrencyAmountArray minus(CurrencyAmountArray other) { if (other.size() != size()) { throw new IllegalArgumentException(Messages.format( "Sizes must be equal, this size is {}, other size is {}", size(), other.size())); } if (!other.currency.equals(currency)) { throw new IllegalArgumentException(Messages.format( "Currencies must be equal, this currency is {}, other currency is {}", currency, other.currency)); } return CurrencyAmountArray.of(currency, values.minus(other.values)); }
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 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)); }
@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); }
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 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 test_convertedTo_existingCurrency() { FxMatrix fxMatrix = FxMatrix.builder() .addRate(USD, GBP, 1 / 1.5) .addRate(EUR, GBP, 0.7) .build(); CurrencyAmountArray convertedArray = VALUES_ARRAY.convertedTo(Currency.GBP, fxMatrix); assertThat(convertedArray.getCurrency()).isEqualTo(Currency.GBP); double[] expected = new double[] { 20 + 30 / 1.5 + 40 * 0.7, 21 + 32 / 1.5 + 43 * 0.7, 22 + 33 / 1.5 + 44 * 0.7}; for (int i = 0; i < 3; i++) { assertThat(convertedArray.get(i).getAmount()).isEqualTo(expected[i], offset(1e-6)); } }
/** * Gets the currency. * * @return the currency */ public Currency getCurrency() { return amounts.getCurrency(); }
@Override public int getScenarioCount() { return amounts.size(); }
/** * 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)); }
@Override public CurrencyAmount get(int index) { return amounts.get(index); }
@Override public Stream<CurrencyAmount> stream() { return amounts.stream(); }
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 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 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); }
/** * 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)); }
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)); }
/** * Returns a collector which creates a multi currency amount array by combining a stream of * currency amount arrays. * <p> * The arrays in the stream must all have the same length. * * @return the collector */ public static Collector<CurrencyAmountArray, ?, MultiCurrencyAmountArray> toMultiCurrencyAmountArray() { return Collector.<CurrencyAmountArray, Map<Currency, CurrencyAmountArray>, MultiCurrencyAmountArray>of( // accumulate into a map HashMap::new, (map, ca) -> map.merge(ca.getCurrency(), ca, CurrencyAmountArray::plus), // combine two maps (map1, map2) -> { map2.values().forEach((ca2) -> map1.merge(ca2.getCurrency(), ca2, CurrencyAmountArray::plus)); return map1; }, // convert to MultiCurrencyAmountArray map -> { Map<Currency, DoubleArray> currencyArrayMap = MapStream.of(map).mapValues(caa -> caa.getValues()).toMap(); return MultiCurrencyAmountArray.of(currencyArrayMap); }, UNORDERED); }