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; }
/** * 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); }
@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; }