final HolidaySource holidaySource = OpenGammaExecutionContext.getHolidaySource(executionContext);
final Clock snapshotClock = executionContext.getValuationClock();
final ValueRequirement desiredValue = desiredValues.iterator().next();
final String curveName = desiredValue.getConstraint(ValuePropertyNames.CURVE);
final ZonedDateTime now = ZonedDateTime.now(snapshotClock);
final DoubleArrayList expiries = new DoubleArrayList();
final DoubleArrayList forwards = new DoubleArrayList();
final Currency currency = target.getValue(PrimitiveComputationTargetType.CURRENCY);
final ForwardSwapCurveDefinition definition = curveDefinitionSource.getDefinition(curveName, currency.toString());
if (definition == null) {
throw new OpenGammaRuntimeException("Couldn't find a forward swap curve definition called " + curveName + " for target " + target);
}
final ForwardSwapCurveSpecification specification = curveSpecificationSource.getSpecification(curveName, currency.toString());
if (specification == null) {
throw new OpenGammaRuntimeException("Couldn't find a forward swap curve specification called " + curveName + " for target " + target);
}
final ForwardSwapCurveInstrumentProvider provider = (ForwardSwapCurveInstrumentProvider) specification.getCurveInstrumentProvider();
final Object dataObject = inputs.getValue(ValueRequirementNames.FORWARD_SWAP_CURVE_MARKET_DATA);
if (dataObject == null) {
throw new OpenGammaRuntimeException("Could not get market data");
}
@SuppressWarnings("unchecked")
final Map<ExternalId, Double> data = (Map<ExternalId, Double>) dataObject;
final String interpolatorName = desiredValue.getConstraint(PROPERTY_FORWARD_CURVE_INTERPOLATOR);
final String leftExtrapolatorName = desiredValue.getConstraint(PROPERTY_FORWARD_CURVE_LEFT_EXTRAPOLATOR);
final String rightExtrapolatorName = desiredValue.getConstraint(PROPERTY_FORWARD_CURVE_RIGHT_EXTRAPOLATOR);
final String forwardTenorName = desiredValue.getConstraint(ForwardSwapCurveMarketDataFunction.PROPERTY_FORWARD_TENOR);
final String conventionName = currency.getCode() + "_SWAP";
final ConventionBundle convention = conventionSource.getConventionBundle(ExternalId.of(InMemoryConventionBundleMaster.SIMPLE_NAME_SCHEME, conventionName));
if (convention == null) {
throw new OpenGammaRuntimeException("Could not get convention named " + conventionName);
}
final DayCount dayCount = convention.getSwapFloatingLegDayCount();
if (dayCount == null) {
throw new OpenGammaRuntimeException("Could not get daycount");
}
final Integer settlementDays = convention.getSwapFloatingLegSettlementDays();
if (settlementDays == null) {
throw new OpenGammaRuntimeException("Could not get number of settlement days");
}
final Calendar calendar = new HolidaySourceCalendarAdapter(holidaySource, currency);
final LocalDate localNow = now.toLocalDate();
final Period forwardPeriod = Period.parse(forwardTenorName);
final Tenor forwardTenor = Tenor.of(forwardPeriod);
final LocalDate forwardStart = ScheduleCalculator.getAdjustedDate(localNow.plus(forwardPeriod), settlementDays, calendar); //TODO check adjustments
for (final Tenor tenor : definition.getTenors()) {
final ExternalId identifier = provider.getInstrument(localNow, tenor, forwardTenor);
if (data.containsKey(identifier)) {
final LocalDate expiry = ScheduleCalculator.getAdjustedDate(forwardStart.plus(tenor.getPeriod()), settlementDays, calendar);
expiries.add(dayCount.getDayCountFraction(localNow, expiry));
forwards.add(data.get(identifier));
}
}
if (expiries.size() == 0) {
throw new OpenGammaRuntimeException("Could not get any values for forward swaps");
}