@Override public int getScenarioCount() { return amounts.size(); }
/** * 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)); }
/** * 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 CurrencyAmountArray plus(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.plus(other.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); }
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_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)); }