/** * Obtains an instance from the specified multi-currency amounts. * * @param amounts the amounts * @return an instance with the specified amounts */ public static MultiCurrencyAmountArray of(MultiCurrencyAmount... amounts) { return of(Arrays.asList(amounts)); }
/** * Returns an instance containing the values from the list of amounts. * * @param amounts the amounts, one for each scenario * @return an instance containing the values from the list of amounts */ public static MultiCurrencyScenarioArray of(List<MultiCurrencyAmount> amounts) { return new MultiCurrencyScenarioArray(MultiCurrencyAmountArray.of(amounts)); }
/** * Returns an instance containing the values from a map of amounts with the same number of elements in each array. * * @param values map of currencies to values * @return an instance containing the values from the map */ public static MultiCurrencyScenarioArray of(Map<Currency, DoubleArray> values) { return new MultiCurrencyScenarioArray(MultiCurrencyAmountArray.of(values)); }
/** * Obtains an instance using a function to create the entries. * <p> * The function is passed the scenario index and returns the value for that index. * * @param size the number of elements * @param amountFunction the function used to obtain each amount * @return an instance initialized using the function * @throws IllegalArgumentException is size is zero or less */ public static MultiCurrencyScenarioArray of(int size, IntFunction<MultiCurrencyAmount> amountFunction) { return new MultiCurrencyScenarioArray(MultiCurrencyAmountArray.of(size, amountFunction)); }
/** * 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); }
public void test_of_function_empty_amounts() { MultiCurrencyAmountArray test = MultiCurrencyAmountArray.of(3, i -> MultiCurrencyAmount.empty()); assertThat(test.size()).isEqualTo(3); }
/** * Returns a new array containing the values from this array with the values from the amount added. * <p> * The amount is added to each element in this array. * * @param amount the amount to add * @return a new array containing the values from this array added to the values in the other array */ public MultiCurrencyAmountArray plus(MultiCurrencyAmount amount) { ImmutableMap.Builder<Currency, DoubleArray> builder = ImmutableMap.builder(); for (Currency currency : Sets.union(values.keySet(), amount.getCurrencies())) { DoubleArray array = values.get(currency); if (array == null) { builder.put(currency, DoubleArray.filled(size, amount.getAmount(currency).getAmount())); } else if (!amount.contains(currency)) { builder.put(currency, array); } else { builder.put(currency, array.plus(amount.getAmount(currency).getAmount())); } } return MultiCurrencyAmountArray.of(builder.build()); }
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 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_plusDifferentSize() { MultiCurrencyAmountArray array1 = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.USD, DoubleArray.of(30, 32), Currency.EUR, DoubleArray.of(40, 43), Currency.CHF, DoubleArray.of(50, 54))); MultiCurrencyAmountArray array2 = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(20, 21, 22), Currency.EUR, DoubleArray.of(140, 143, 144), Currency.CHF, DoubleArray.of(250, 254, 256))); assertThrowsIllegalArg(() -> array1.plus(array2)); }
public void test_minusDifferentSize() { MultiCurrencyAmountArray array1 = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.USD, DoubleArray.of(30, 32), Currency.EUR, DoubleArray.of(40, 43), Currency.CHF, DoubleArray.of(50, 54))); MultiCurrencyAmountArray array2 = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(20, 21, 22), Currency.EUR, DoubleArray.of(140, 143, 144), Currency.CHF, DoubleArray.of(250, 254, 256))); assertThrowsIllegalArg(() -> array1.minus(array2)); }
public void test_minusArray() { MultiCurrencyAmountArray array1 = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(40, 43, 44), Currency.CHF, DoubleArray.of(50, 54, 56))); MultiCurrencyAmountArray array2 = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(20, 21, 22), Currency.EUR, DoubleArray.of(140, 143, 144), Currency.CHF, DoubleArray.of(250, 254, 256))); MultiCurrencyAmountArray expected = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(-20, -21, -22), Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(-100, -100, -100), Currency.CHF, DoubleArray.of(-200, -200, -200))); assertThat(array1.minus(array2)).isEqualTo(expected); }
public void test_plusArray() { MultiCurrencyAmountArray array1 = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(40, 43, 44), Currency.CHF, DoubleArray.of(50, 54, 56))); MultiCurrencyAmountArray array2 = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(20, 21, 22), Currency.EUR, DoubleArray.of(140, 143, 144), Currency.CHF, DoubleArray.of(250, 254, 256))); MultiCurrencyAmountArray expected = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(20, 21, 22), Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(180, 186, 188), Currency.CHF, DoubleArray.of(300, 308, 312))); assertThat(array1.plus(array2)).isEqualTo(expected); }
public void test_minusAmount() { MultiCurrencyAmountArray array = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(40, 43, 44), Currency.CHF, DoubleArray.of(50, 54, 56))); MultiCurrencyAmount amount = MultiCurrencyAmount.of( ImmutableMap.of( Currency.GBP, 21d, Currency.EUR, 143d, Currency.CHF, 254d)); MultiCurrencyAmountArray expected = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(-21, -21, -21), Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(-103, -100, -99), Currency.CHF, DoubleArray.of(-204, -200, -198))); assertThat(array.minus(amount)).isEqualTo(expected); }
public void test_plusAmount() { MultiCurrencyAmountArray array = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(40, 43, 44), Currency.CHF, DoubleArray.of(50, 54, 56))); MultiCurrencyAmount amount = MultiCurrencyAmount.of( ImmutableMap.of( Currency.GBP, 21d, Currency.EUR, 143d, Currency.CHF, 254d)); MultiCurrencyAmountArray expected = MultiCurrencyAmountArray.of( ImmutableMap.of( Currency.GBP, DoubleArray.of(21, 21, 21), Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(183, 186, 187), Currency.CHF, DoubleArray.of(304, 308, 310))); assertThat(array.plus(amount)).isEqualTo(expected); }
public void test_of_function() { MultiCurrencyAmount mca1 = MultiCurrencyAmount.of(CurrencyAmount.of(Currency.GBP, 10), CurrencyAmount.of(Currency.USD, 20)); MultiCurrencyAmount mca2 = MultiCurrencyAmount.of(CurrencyAmount.of(Currency.GBP, 10), CurrencyAmount.of(Currency.EUR, 30)); MultiCurrencyAmount mca3 = MultiCurrencyAmount.of(CurrencyAmount.of(Currency.USD, 40)); List<MultiCurrencyAmount> amounts = ImmutableList.of(mca1, mca2, mca3); MultiCurrencyAmountArray test = MultiCurrencyAmountArray.of(3, i -> amounts.get(i)); assertThat(test.get(0)).isEqualTo(mca1.plus(Currency.EUR, 0)); assertThat(test.get(1)).isEqualTo(mca2.plus(Currency.USD, 0)); assertThat(test.get(2)).isEqualTo(mca3.plus(Currency.GBP, 0).plus(Currency.EUR, 0)); }
public void collector() { List<CurrencyAmountArray> arrays = ImmutableList.of( CurrencyAmountArray.of(USD, DoubleArray.of(10, 20, 30)), CurrencyAmountArray.of(USD, DoubleArray.of(5, 6, 7)), CurrencyAmountArray.of(EUR, DoubleArray.of(2, 4, 6)), CurrencyAmountArray.of(GBP, DoubleArray.of(11, 12, 13)), CurrencyAmountArray.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)); MultiCurrencyAmountArray expected = MultiCurrencyAmountArray.of(expectedMap); assertThat(arrays.stream().collect(toMultiCurrencyAmountArray())).isEqualTo(expected); }
public void total() { List<CurrencyAmountArray> arrays = ImmutableList.of( CurrencyAmountArray.of(USD, DoubleArray.of(10, 20, 30)), CurrencyAmountArray.of(USD, DoubleArray.of(5, 6, 7)), CurrencyAmountArray.of(EUR, DoubleArray.of(2, 4, 6)), CurrencyAmountArray.of(GBP, DoubleArray.of(11, 12, 13)), CurrencyAmountArray.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)); MultiCurrencyAmountArray expected = MultiCurrencyAmountArray.of(expectedMap); assertThat(MultiCurrencyAmountArray.total(arrays)).isEqualTo(expected); }
public void test_equalsHashCode() { MultiCurrencyAmountArray array = MultiCurrencyAmountArray.of( ImmutableList.of( MultiCurrencyAmount.of( CurrencyAmount.of(Currency.GBP, 20), CurrencyAmount.of(Currency.USD, 30), CurrencyAmount.of(Currency.EUR, 40)), MultiCurrencyAmount.of( CurrencyAmount.of(Currency.GBP, 21), CurrencyAmount.of(Currency.USD, 32), CurrencyAmount.of(Currency.EUR, 43)), MultiCurrencyAmount.of( CurrencyAmount.of(Currency.GBP, 22), CurrencyAmount.of(Currency.USD, 33), CurrencyAmount.of(Currency.EUR, 44)))); assertThat(array).isEqualTo(VALUES_ARRAY); assertThat(array.hashCode()).isEqualTo(VALUES_ARRAY.hashCode()); }
public void coverage() { coverImmutableBean(VALUES_ARRAY); MultiCurrencyAmountArray test2 = MultiCurrencyAmountArray.of( MultiCurrencyAmount.of( CurrencyAmount.of(Currency.GBP, 21), CurrencyAmount.of(Currency.USD, 31), CurrencyAmount.of(Currency.EUR, 41)), MultiCurrencyAmount.of( CurrencyAmount.of(Currency.GBP, 22), CurrencyAmount.of(Currency.USD, 33), CurrencyAmount.of(Currency.EUR, 44))); coverBeanEquals(VALUES_ARRAY, test2); }