/** * 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); }
@Override public ScenarioArray<?> convertedTo(Currency reportingCurrency, ScenarioFxRateProvider fxRateProvider) { if (value instanceof FxConvertible<?>) { FxConvertible<?> convertible = (FxConvertible<?>) value; if (fxRateProvider.getScenarioCount() != scenarioCount) { throw new IllegalArgumentException(Messages.format( "Expected {} FX rates but received {}", scenarioCount, fxRateProvider.getScenarioCount())); } ImmutableList<Object> converted = IntStream.range(0, scenarioCount) .mapToObj(i -> convertible.convertedTo(reportingCurrency, fxRateProvider.fxRateProvider(i))) .collect(toImmutableList()); return DefaultScenarioArray.of(converted); } return this; }
@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 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); } }
private Object convert(Currency reportingCurrency, ScenarioFxRateProvider fxRateProvider, Object base, int index) { if (base instanceof FxConvertible<?>) { FxConvertible<?> convertible = (FxConvertible<?>) base; return convertible.convertedTo(reportingCurrency, fxRateProvider.fxRateProvider(index)); } return base; }
@Override public ScenarioArray<?> convertedTo(Currency resultCurrency, ScenarioFxRateProvider fxRateProvider) { int scenarioCount = getScenarioCount(); if (fxRateProvider.getScenarioCount() != scenarioCount) { throw new IllegalArgumentException(Messages.format( "Expected {} FX rates but received {}", scenarioCount, fxRateProvider.getScenarioCount())); } ImmutableList<Object> converted = zipWithIndex(values.stream()) .map(tp -> convert(resultCurrency, fxRateProvider, tp.getFirst(), tp.getSecond())) .collect(toImmutableList()); return DefaultScenarioArray.of(converted); }
public void convert() { assertThat(fxRateProvider.convert(10, Currency.GBP, Currency.USD, 0)).isEqualTo(14d); }
@BeforeClass public void setUp() throws Exception { ScenarioMarketData marketData = ImmutableScenarioMarketData.builder(LocalDate.of(2011, 3, 8)) .addValue(FxRateId.of(Currency.GBP, Currency.USD), FxRate.of(Currency.GBP, Currency.USD, 1.4d)) .build(); fxRateProvider = ScenarioFxRateProvider.of(marketData); }
@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); }
/** * Gets the FX rate for the specified currency pair and scenario index. * <p> * The rate returned is the rate from the base currency to the counter currency * as defined by this formula: {@code (1 * baseCurrency = fxRate * counterCurrency)}. * This will return 1 if the two input currencies are the same. * * @param baseCurrency the base currency, to convert from * @param counterCurrency the counter currency, to convert to * @param scenarioIndex the scenario index * @return the FX rate for the currency pair * @throws RuntimeException if no FX rate could be found */ public default double fxRate(Currency baseCurrency, Currency counterCurrency, int scenarioIndex) { return fxRateProvider(scenarioIndex).fxRate(baseCurrency, counterCurrency); }
public void fxRate() { assertThat(fxRateProvider.fxRate(Currency.GBP, Currency.USD, 0)).isEqualTo(1.4d); }