/** * Returns the number of currency values for each currency. * * @return the number of currency values for each currency */ @Override public int getScenarioCount() { return amounts.size(); }
/** * 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 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 */ public MultiCurrencyAmountArray plus(MultiCurrencyAmountArray other) { if (other.size() != size) { throw new IllegalArgumentException(Messages.format( "Sizes must be equal, this size is {}, other size is {}", size, other.size())); } Map<Currency, DoubleArray> addedValues = Stream.concat(values.entrySet().stream(), other.values.entrySet().stream()) .collect(toMap(e -> e.getKey(), e -> e.getValue(), (arr1, arr2) -> arr1.plus(arr2))); return MultiCurrencyAmountArray.of(addedValues); }
/** * 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 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 with the values from the other array subtracted * @throws IllegalArgumentException if the arrays have different sizes */ public MultiCurrencyAmountArray minus(MultiCurrencyAmountArray other) { if (other.size() != size) { throw new IllegalArgumentException(Messages.format( "Sizes must be equal, this size is {}, other size is {}", size, other.size())); } ImmutableMap.Builder<Currency, DoubleArray> builder = ImmutableMap.builder(); for (Currency currency : Sets.union(values.keySet(), other.values.keySet())) { DoubleArray array = values.get(currency); DoubleArray otherArray = other.values.get(currency); if (otherArray == null) { builder.put(currency, array); } else if (array == null) { builder.put(currency, otherArray.multipliedBy(-1)); } else { builder.put(currency, array.minus(otherArray)); } } return of(builder.build()); }
public void test_of_function_empty_amounts() { MultiCurrencyAmountArray test = MultiCurrencyAmountArray.of(3, i -> MultiCurrencyAmount.empty()); assertThat(test.size()).isEqualTo(3); }
public void serializeSize() throws Exception { MultiCurrencyAmountArray deserialized = serializedDeserialize(VALUES_ARRAY); assertThat(deserialized.size()).isEqualTo(3); MultiCurrencyAmountArray empty = MultiCurrencyAmountArray.of( MultiCurrencyAmount.empty(), MultiCurrencyAmount.empty()); MultiCurrencyAmountArray deserializedEmpty = serializedDeserialize(empty); assertThat(deserializedEmpty.size()).isEqualTo(2); }
public void test_empty_amounts() { MultiCurrencyAmountArray array = MultiCurrencyAmountArray.of( MultiCurrencyAmount.empty(), MultiCurrencyAmount.empty()); assertThat(array.size()).isEqualTo(2); assertThat(array.get(0)).isEqualTo(MultiCurrencyAmount.empty()); assertThat(array.get(1)).isEqualTo(MultiCurrencyAmount.empty()); }
public void test_of() { assertThat(VALUES_ARRAY.getValues(Currency.GBP)).isEqualTo(DoubleArray.of(20, 21, 22)); assertThat(VALUES_ARRAY.getValues(Currency.USD)).isEqualTo(DoubleArray.of(30, 32, 33)); assertThat(VALUES_ARRAY.getValues(Currency.EUR)).isEqualTo(DoubleArray.of(40, 43, 44)); MultiCurrencyAmountArray raggedArray = MultiCurrencyAmountArray.of( ImmutableList.of( MultiCurrencyAmount.of( CurrencyAmount.of(Currency.EUR, 4)), MultiCurrencyAmount.of( CurrencyAmount.of(Currency.GBP, 21), CurrencyAmount.of(Currency.USD, 32), CurrencyAmount.of(Currency.EUR, 43)), MultiCurrencyAmount.of( CurrencyAmount.of(Currency.EUR, 44)))); assertThat(raggedArray.size()).isEqualTo(3); assertThat(VALUES_ARRAY.getCurrencies()).containsExactlyInAnyOrder(Currency.GBP, Currency.USD, Currency.EUR); assertThat(raggedArray.getValues(Currency.GBP)).isEqualTo(DoubleArray.of(0, 21, 0)); assertThat(raggedArray.getValues(Currency.USD)).isEqualTo(DoubleArray.of(0, 32, 0)); assertThat(raggedArray.getValues(Currency.EUR)).isEqualTo(DoubleArray.of(4, 43, 44)); assertThrowsIllegalArg(() -> raggedArray.getValues(Currency.AUD)); }
public void test_of_map() { MultiCurrencyAmountArray array = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(20, 21, 22), Currency.EUR, DoubleArray.of(40, 43, 44))); MultiCurrencyAmountArray expected = MultiCurrencyAmountArray.of( ImmutableList.of( MultiCurrencyAmount.of( CurrencyAmount.of(Currency.GBP, 20), CurrencyAmount.of(Currency.EUR, 40)), MultiCurrencyAmount.of( CurrencyAmount.of(Currency.GBP, 21), CurrencyAmount.of(Currency.EUR, 43)), MultiCurrencyAmount.of( CurrencyAmount.of(Currency.GBP, 22), CurrencyAmount.of(Currency.EUR, 44)))); assertThat(array.size()).isEqualTo(3); assertThat(array).isEqualTo(expected); assertThrowsIllegalArg( () -> MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(20, 21), Currency.EUR, DoubleArray.of(40, 43, 44))), "Arrays must have the same size.*"); MultiCurrencyAmountArray empty = MultiCurrencyAmountArray.of(ImmutableMap.of()); assertThat(empty.size()).isEqualTo(0); }