// Get the coupon stub type
final StubType stubType = cds.getStubType();
// Get the coupon frequency
final PeriodFrequency couponFrequency = cds.getCouponFrequency();
// Compute the number of cashflows in the premium leg schedule (based on the adjusted effective and maturity dates and the coupon frequency and stub type)
final int numberOfCashflows = calculateNumberOfPremiumLegCashflows(adjustedEffectiveDate, adjustedMaturityDate, couponFrequency, stubType);
//final int numberOfCashflows = calculateNumberOfPremiumLegCashflows(startDate, adjustedMaturityDate, couponFrequency, stubType);
// TODO : Need to check if there are less than two cashflow dates
// Build the cashflow schedule (include the node at the effective date even though there is no cashflow on this date)
final ZonedDateTime[] cashflowSchedule = new ZonedDateTime[numberOfCashflows + 1];
// -------------------------------------------------------------------------------
// The stub is at the front of the premium leg schedule
if (stubType == StubType.FRONTSHORT || stubType == StubType.FRONTLONG) {
// Start at the adjusted maturity of the contract
ZonedDateTime cashflowDate = adjustedMaturityDate;
/*
int i = 0;
cashflowSchedule[i] = cashflowDate;
while (cashflowDate.isAfter(startDate)) {
i++;
cashflowDate = cashflowDate.minus(couponFrequency.getPeriod());
cashflowSchedule[i] = cashflowDate;
}
*/
// Note the order of the loop and the termination condition (i > 0 not i = 0)
for (int i = numberOfCashflows; i > 0; i--) {
// Store the date (note this is at the top of the loop)
cashflowSchedule[i] = cashflowDate;
// Step back in time by the specified number of months
cashflowDate = cashflowDate.minus(couponFrequency.getPeriod());
}
// TODO : Sort this out
if (stubType == StubType.FRONTSHORT) {
// Append the timenode at the adjusted effective date at the beginning of the cashflow schedule vector
cashflowSchedule[0] = adjustedEffectiveDate;
}
if (stubType == StubType.FRONTLONG) {
cashflowSchedule[0] = cashflowDate.minus(couponFrequency.getPeriod());
}
}
// -------------------------------------------------------------------------------
// TODO : Test this code
// TODO : Add the appendage at the end
// The stub is at the back of the premium leg schedule
if (stubType == StubType.BACKSHORT || stubType == StubType.BACKLONG) {
// Start at the adjusted effective date of the contract
ZonedDateTime cashflowDate = adjustedEffectiveDate;
/*
int i = 0;
cashflowSchedule[i] = cashflowDate;
while (cashflowDate.isBefore(adjustedMaturityDate)) {
i++;
cashflowDate = cashflowDate.plus(couponFrequency.getPeriod());
cashflowSchedule[i] = cashflowDate;
}
*/
for (int i = 0; i < numberOfCashflows; i++) {
// Store the date (note this is at the top of the loop)
cashflowSchedule[i] = cashflowDate;
// Step forward in time by the specified number of months
cashflowDate = cashflowDate.plus(couponFrequency.getPeriod());
}
}
// -------------------------------------------------------------------------------