/** * 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; }
@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); }
/** * 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); }
@Override public CurrencyScenarioArray convertedTo(Currency reportingCurrency, ScenarioFxRateProvider fxRateProvider) { int size = getScenarioCount(); if (fxRateProvider.getScenarioCount() != size) { throw new IllegalArgumentException(Messages.format( "Expected {} FX rates but received {}", size, fxRateProvider.getScenarioCount())); } double[] singleCurrencyValues = new double[size]; for (Map.Entry<Currency, DoubleArray> entry : amounts.getValues().entrySet()) { Currency currency = entry.getKey(); DoubleArray currencyValues = entry.getValue(); for (int i = 0; i < size; i++) { double convertedValue = currencyValues.get(i) * fxRateProvider.fxRate(currency, reportingCurrency, i); singleCurrencyValues[i] += convertedValue; } } return CurrencyScenarioArray.of(reportingCurrency, DoubleArray.ofUnsafe(singleCurrencyValues)); }
public void test_getValues() { Map<Currency, DoubleArray> expected = ImmutableMap.of( Currency.GBP, DoubleArray.of(20, 21, 22), Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(40, 43, 44)); assertThat(VALUES_ARRAY.getValues()).isEqualTo(expected); }
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 getAllAmountsUnsafe() { Map<Currency, DoubleArray> expected = ImmutableMap.of( Currency.GBP, DoubleArray.of(20, 21, 22), Currency.USD, DoubleArray.of(30, 32, 33), Currency.EUR, DoubleArray.of(40, 43, 44)); assertThat(VALUES_ARRAY.getAmounts().getValues()).isEqualTo(expected); }