if (numSwaps == 0) {
return;
}
final GenerateCreditDefaultSwapPremiumLegSchedule swapMaturities = new GenerateCreditDefaultSwapPremiumLegSchedule();
final ZonedDateTime lastStubDate = cashDates[cashDates.length - 1];
// Need to implement this if want to have swaps with maturities less than MM instruments
// numSwaps = getNumberOfActiveSwaps(lastStubDate, swapDates, numSwaps);
final ZonedDateTime[] unadjustedSwapDates = new ZonedDateTime[swapDates.length];
final ZonedDateTime[] adjustedSwapDates = new ZonedDateTime[swapDates.length];
final boolean[] onCycleSwapDates = new boolean[swapDates.length];
final ZonedDateTime[] previousSwapDates = new ZonedDateTime[swapDates.length];
for (int i = 0; i < numSwaps; i++) {
unadjustedSwapDates[i] = swapDates[i];
adjustedSwapDates[i] = swapMaturities.businessDayAdjustDate(swapDates[i], calendar, businessdayAdjustmentConvention);
}
int numIntervals = 0;
int extraDays = 0;
int compoundInterval = 0;
int multiplier = 0;
double intervalYears = 0.0;
// Need to fix this - bit of a hack
/*if (swapFixedLegCouponFrequency == PeriodFrequency.SEMI_ANNUAL)*/
{
intervalYears = 0.5;
multiplier = 6;
}
/*if (swapFixedLegCouponFrequency == PeriodFrequency.QUARTERLY)*/
{
intervalYears = 0.25;
multiplier = 3;
}
// -------------------------------------------
for (int i = 0; i < numSwaps; i++) {
boolean onCycle = false;
if (valueDate.getDayOfMonth() <= 28 && unadjustedSwapDates[i].getDayOfMonth() <= 28) {
final ZonedDateTime fromDate = valueDate;
final ZonedDateTime toDate = unadjustedSwapDates[i];
final double fromToYears = TimeCalculator.getTimeBetween(fromDate, toDate, ACT_365);
final int lowNumIntervals = Math.max(0, (int) Math.floor(Math.abs(fromToYears / intervalYears)) - 2);
int index = lowNumIntervals;
compoundInterval = index * multiplier;
ZonedDateTime currDate = fromDate.plusMonths(compoundInterval);
ZonedDateTime lastDate = currDate;
while (currDate.isAfter(fromDate) && !currDate.isAfter(toDate)) {
++index;
lastDate = currDate;
currDate = valueDate.plusMonths(index * multiplier);
}
numIntervals = index - 1;
extraDays = (int) Math.abs(TimeCalculator.getTimeBetween(toDate, lastDate));
if (extraDays == 0) {
onCycle = true;
} // end if extraDays
} // end if dom <= 28
onCycleSwapDates[i] = onCycle;
ZonedDateTime prevDate;
if (onCycleSwapDates[i]) {
prevDate = valueDate.plusMonths(multiplier * (numIntervals - 1));
} else {
prevDate = unadjustedSwapDates[i].plusMonths(6 * (-1));
}
previousSwapDates[i] = prevDate;
} // end loop over i
// -------------------------------------------
final boolean oneAlreadyAdded = false;
boolean isEndStub = false;
int numDates;
for (int i = 0; i < numSwaps; i++) {
if (adjustedSwapDates[i].isAfter(cashDates[cashDates.length - 1])) {
if (onCycleSwapDates[i]) {
isEndStub = true;
} else {
// Need to fill this in - jpmcdsisendstub
} // end if
// need to add rate = 0 case
if (isEndStub) {
final ZonedDateTime fromDate = valueDate;
final ZonedDateTime toDate = unadjustedSwapDates[i];
final double fromToYears = TimeCalculator.getTimeBetween(fromDate, toDate, ACT_365);
final int lowNumIntervals = Math.max(0, (int) Math.floor(Math.abs(fromToYears / intervalYears)) - 2);
int index = lowNumIntervals;
compoundInterval = index * multiplier;
ZonedDateTime currDate = fromDate.plusMonths(compoundInterval);
ZonedDateTime lastDate = currDate;
while (currDate.isAfter(fromDate) && !currDate.isAfter(toDate)) {
++index;
lastDate = currDate;
currDate = valueDate.plusMonths(index * multiplier);
}
numIntervals = index - 1;
extraDays = (int) Math.abs(TimeCalculator.getTimeBetween(toDate, lastDate));
} else {
// Need to add this
}
if (extraDays > 0) {
numDates = numIntervals + 2;
} else {
numDates = numIntervals + 1;
}
final ZonedDateTime[] dateList = new ZonedDateTime[numDates];
if (isEndStub) {
for (int j = 0; j < numDates - 1; j++) {
dateList[j] = valueDate.plusMonths(j * multiplier);
}
dateList[numDates - 1] = unadjustedSwapDates[i];
} else {
// Need to add this
}
for (int j = 0; j < numDates - 1; j++) {
dateList[j] = dateList[j + 1];
}
numDates--;
//
final ZonedDateTime[] adjustedDateList = new ZonedDateTime[numDates];
for (int idx = 0; idx < numDates; idx++) {
adjustedDateList[idx] = swapMaturities.businessDayAdjustDate(dateList[idx], calendar, businessdayAdjustmentConvention);
}
final double[] cashflowList = new double[numDates];
ZonedDateTime prevDate = valueDate;