/*
 * (c) Copyright Christian P. Fries, Germany. All rights reserved. Contact: email@christianfries.com.
 *
 * Created on 15.09.2013
 */

package net.finmath.time.businessdaycalendar;

import java.util.Calendar;

/**
 * Base class for all business day calendars.
 * Implements date rolling.
 * 
 * @author Christian Fries
 */
public abstract class BusinessdayCalendar implements BusinessdayCalendarInterface {

	/* (non-Javadoc)
	 * @see net.finmath.time.BusinessdayCalendarInterface#getAdjustedDate(java.util.Calendar, net.finmath.time.BusinessdayCalendarInterface.DateRollConvention)
	 */
	@Override
	public Calendar getAdjustedDate(Calendar date, DateRollConvention dateRollConvention) {
		if(dateRollConvention == DateRollConvention.ACTUAL) {
			return date;
		}
		else if(dateRollConvention == DateRollConvention.MODIFIED_FOLLOWING) {
			Calendar adjustedDate = getAdjustedDate(date, DateRollConvention.FOLLOWING);
			if(adjustedDate.get(Calendar.MONTH) != date.get(Calendar.MONTH)) {
				return getAdjustedDate(date, DateRollConvention.PREVIOUS);
			}
		}
		else if(dateRollConvention == DateRollConvention.MODIFIED_PREVIOUS) {
			Calendar adjustedDate = getAdjustedDate(date, DateRollConvention.PREVIOUS);
			if(adjustedDate.get(Calendar.MONTH) != date.get(Calendar.MONTH)) {
				return getAdjustedDate(date, DateRollConvention.FOLLOWING);
			}
		}
		else if(dateRollConvention != DateRollConvention.FOLLOWING && dateRollConvention != DateRollConvention.PREVIOUS) {
			throw new IllegalArgumentException("Unknown date roll convention.");
		}

		int adjustment = dateRollConvention == DateRollConvention.FOLLOWING ? 1 : -1;
		Calendar adjustedDate = (Calendar)date.clone();
		while(!isBusinessday(adjustedDate)) {
			adjustedDate.add(Calendar.DAY_OF_YEAR, adjustment);
		}
		return adjustedDate;
	}
}

@@ -1043,32 +1043,61 @@
 PREVIOUS);%0A%09%09%09%7D%0A
+%09%09%09else return adjustedDate;%0A
 %09%09%7D%0A%09%09else if(da
@@ -1374,16 +1374,45 @@
 );%0A%09%09%09%7D%0A
+%09%09%09else return adjustedDate;%0A
 %09%09%7D%0A%09%09el
@@ -1428,33 +1428,33 @@
 eRollConvention 
-!
+=
 = DateRollConven
@@ -1472,10 +1472,10 @@
 ING 
-&&
+%7C%7C
  dat
@@ -1490,17 +1490,17 @@
 vention 
-!
+=
 = DateRo
@@ -1529,84 +1529,8 @@
  %7B%0A%09
-%09%09throw new IllegalArgumentException(%22Unknown date roll convention.%22);%0A%09%09%7D%0A%0A
 %09%09in
@@ -1607,16 +1607,17 @@
 : -1;%0A%09%09
+%09
 Calendar
@@ -1658,16 +1658,17 @@
 ne();%0A%09%09
+%09
 while(!i
@@ -1697,16 +1697,17 @@
 ate)) %7B%0A
+%09
 %09%09%09adjus
@@ -1755,18 +1755,20 @@
 ent);%0A%09%09
+%09
 %7D%0A
+%09
 %09%09return
@@ -1782,13 +1782,89 @@
 edDate;%0A
+%09%09%7D%0A%0A%09%09throw new IllegalArgumentException(%22Unknown date roll convention.%22);%0A
 %09%7D%0A%7D%0A

Corrected implementation of MODIFIED date rolling.