/** * Converts an amount in a currency to an amount in a different currency using a rate from this provider. * * @param amount an amount in {@code fromCurrency} * @param fromCurrency the currency of the amount * @param toCurrency the currency into which the amount should be converted * @param scenarioIndex the scenario index * @return the amount converted into {@code toCurrency} * @throws IllegalArgumentException if either of the currencies aren't included in the currency pair of this rate */ public default double convert(double amount, Currency fromCurrency, Currency toCurrency, int scenarioIndex) { return amount * fxRate(fromCurrency, toCurrency, scenarioIndex); }
public void fxRate() { assertThat(fxRateProvider.fxRate(Currency.GBP, Currency.USD, 0)).isEqualTo(1.4d); }
@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)); }
@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 specifySource() { ObservableSource testSource = ObservableSource.of("test"); ScenarioMarketData marketData = ImmutableScenarioMarketData.builder(LocalDate.of(2011, 3, 8)) .addValue(FxRateId.of(Currency.GBP, Currency.USD), FxRate.of(Currency.GBP, Currency.USD, 1.4d)) .addValue(FxRateId.of(Currency.GBP, Currency.USD, testSource), FxRate.of(Currency.GBP, Currency.USD, 1.41d)) .build(); ScenarioFxRateProvider defaultRateProvider = ScenarioFxRateProvider.of(marketData); ScenarioFxRateProvider sourceRateProvider = ScenarioFxRateProvider.of(marketData, testSource); assertThat(defaultRateProvider.fxRate(Currency.GBP, Currency.USD, 0)).isEqualTo(1.4d); assertThat(sourceRateProvider.fxRate(Currency.GBP, Currency.USD, 0)).isEqualTo(1.41d); } }