/** * Returns the values for the specified currency, throws an exception if there are no values for the currency. * * @param currency the currency for which values are required * @return the values for the specified currency, throws an exception if there are none * @throws IllegalArgumentException if there are no values for the currency */ public DoubleArray getValues(Currency currency) { DoubleArray currencyValues = amounts.getValues(currency); if (currencyValues == null) { throw new IllegalArgumentException("No values available for " + currency); } return currencyValues; }
/** * 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 the number of currency values for each currency. * * @return the number of currency values for each currency */ @Override public int getScenarioCount() { return amounts.size(); }
@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 3530753: // size return ((MultiCurrencyAmountArray) bean).getSize(); case -823812830: // values return ((MultiCurrencyAmountArray) bean).getValues(); } return super.propertyGet(bean, propertyName, quiet); }
/** * Obtains an instance from a map of amounts. * <p> * Each currency is associated with an array of amounts. * All the arrays must have the same number of elements. * <p> * If the map is empty the returned array will have a size of zero. To create an empty array * with a non-zero size use one of the other {@code of} methods. * * @param values map of currencies to values * @return an instance containing the values from the map */ public static MultiCurrencyAmountArray of(Map<Currency, DoubleArray> values) { values.values().stream().reduce((a1, a2) -> checkSize(a1, a2)); // All of the values must have the same size so use the size of the first int size = values.isEmpty() ? 0 : values.values().iterator().next().size(); return new MultiCurrencyAmountArray(size, values); }
/** * Returns a {@link MultiCurrencyAmount} at the specified index. * <p> * This method is not very efficient for large sizes as a new object must be created at each index. * Consider using {@link #getValues(Currency)} instead. * * @param index the index that should be returned * @return a multi currency amount containing the currency values at the specified index * @throws IndexOutOfBoundsException if the index is invalid */ @Override public MultiCurrencyAmount get(int index) { List<CurrencyAmount> currencyAmounts = amounts.getCurrencies().stream() .map(ccy -> CurrencyAmount.of(ccy, amounts.getValues(ccy).get(index))) .collect(toList()); return MultiCurrencyAmount.of(currencyAmounts); }
/** * 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() { 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_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_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 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 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 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 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); }
/** * Returns a multi currency amount array representing the total of the input arrays. * <p> * If the input contains the same currency more than once, the amounts are added together. * * @param arrays the amount arrays * @return the total amounts */ public static MultiCurrencyAmountArray total(Iterable<CurrencyAmountArray> arrays) { return Guavate.stream(arrays).collect(toMultiCurrencyAmountArray()); }
/** * Returns a stream of {@link MultiCurrencyAmount} instances containing the values from this object. * <p> * This method is not very efficient for large sizes as a new object must be created for each value. * Consider using {@link #getValues(Currency)} instead. * * @return a stream of multi currency amounts containing the currency values from this object */ @Override public Stream<MultiCurrencyAmount> stream() { return amounts.stream(); }
/** * Returns the set of currencies for which this object contains values. * * @return the set of currencies for which this object contains values */ public Set<Currency> getCurrencies() { return amounts.getCurrencies(); }
@Override public MultiCurrencyAmountArray build() { return new MultiCurrencyAmountArray( size, values); }