|
From: <ma...@us...> - 2007-01-09 15:51:33
|
Revision: 242
http://svn.sourceforge.net/objectlabkit/?rev=242&view=rev
Author: marchy
Date: 2007-01-09 07:50:09 -0800 (Tue, 09 Jan 2007)
Log Message:
-----------
changed if-else's to use switch statements with enums, and added static imports for some constants, i think it makes the code look cleaner, whadayathink mr b?
we'll see what qalab has to say about all this ;-)
Modified Paths:
--------------
trunk/datecalc-common/src/main/java/net/objectlab/kit/datecalc/common/AbstractDateCalculator.java
trunk/datecalc-common/src/main/java/net/objectlab/kit/datecalc/common/PeriodCountBasis.java
trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarIMMDateCalculator.java
trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarKitCalculatorsFactory.java
trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarPeriodCountCalculator.java
trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/DateKitCalculatorsFactory.java
trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateIMMDateCalculator.java
trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateKitCalculatorsFactory.java
trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDatePeriodCountCalculator.java
trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/YearMonthDayKitCalculatorsFactory.java
Modified: trunk/datecalc-common/src/main/java/net/objectlab/kit/datecalc/common/AbstractDateCalculator.java
===================================================================
--- trunk/datecalc-common/src/main/java/net/objectlab/kit/datecalc/common/AbstractDateCalculator.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-common/src/main/java/net/objectlab/kit/datecalc/common/AbstractDateCalculator.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -32,6 +32,11 @@
*/
package net.objectlab.kit.datecalc.common;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.BACKWARD;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_FOLLOWING;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_PRECEEDING;
+
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -302,17 +307,13 @@
}
public DateCalculator<E> moveByBusinessDays(final int businessDays) {
- if (businessDays > 0
- && holidayHandler != null
- && (holidayHandler.getType().equals(HolidayHandlerType.BACKWARD) || holidayHandler.getType().equals(
- HolidayHandlerType.MODIFIED_PRECEEDING))) {
- throw new IllegalArgumentException("A " + HolidayHandlerType.MODIFIED_PRECEEDING + " or "
- + HolidayHandlerType.BACKWARD + " does not allow positive steps for moveByBusinessDays");
- } else if (businessDays < 0
- && holidayHandler != null
- && (holidayHandler.getType().equals(HolidayHandlerType.FORWARD) || holidayHandler.getType().equals(
- HolidayHandlerType.MODIFIED_FOLLOWING))) {
- throw new IllegalArgumentException("A " + HolidayHandlerType.MODIFIED_FOLLOWING + " or " + HolidayHandlerType.FORWARD
+ if (businessDays > 0 && holidayHandler != null
+ && (holidayHandler.getType().equals(BACKWARD) || holidayHandler.getType().equals(MODIFIED_PRECEEDING))) {
+ throw new IllegalArgumentException("A " + MODIFIED_PRECEEDING + " or " + BACKWARD
+ + " does not allow positive steps for moveByBusinessDays");
+ } else if (businessDays < 0 && holidayHandler != null
+ && (holidayHandler.getType().equals(FORWARD) || holidayHandler.getType().equals(MODIFIED_FOLLOWING))) {
+ throw new IllegalArgumentException("A " + MODIFIED_FOLLOWING + " or " + FORWARD
+ " does not allow negative steps for moveByBusinessDays");
}
@@ -405,7 +406,7 @@
public HolidayCalendar<E> getHolidayCalendar() {
return holidayCalendar;
}
-
+
protected abstract E clone(final E date);
}
Modified: trunk/datecalc-common/src/main/java/net/objectlab/kit/datecalc/common/PeriodCountBasis.java
===================================================================
--- trunk/datecalc-common/src/main/java/net/objectlab/kit/datecalc/common/PeriodCountBasis.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-common/src/main/java/net/objectlab/kit/datecalc/common/PeriodCountBasis.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -33,10 +33,13 @@
package net.objectlab.kit.datecalc.common;
/**
- * Defines some standard Day Count basis.
+ * Defines some standard Day Count bases.
*
- * You can refer to the following document to read about the most common Day Count conventions:
- * {@link http://www.fincad.com/support/developerfunc/mathref/Daycount.htm}
+ * <p/>
+ *
+ * To read about the most common Day Count Conventions, you can refer to the
+ * <a href="http://www.fincad.com/support/developerfunc/mathref/Daycount.htm">
+ * following document</a>.
*
* @author Benoit Xhenseval
* @author $LastChangedBy$
Modified: trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarIMMDateCalculator.java
===================================================================
--- trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarIMMDateCalculator.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarIMMDateCalculator.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -32,6 +32,15 @@
*/
package net.objectlab.kit.datecalc.jdk;
+import static java.util.Calendar.DAY_OF_MONTH;
+import static java.util.Calendar.DECEMBER;
+import static java.util.Calendar.JUNE;
+import static java.util.Calendar.MARCH;
+import static java.util.Calendar.MONTH;
+import static java.util.Calendar.SEPTEMBER;
+import static java.util.Calendar.WEDNESDAY;
+import static net.objectlab.kit.datecalc.common.IMMPeriod.QUARTERLY;
+
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
@@ -97,25 +106,38 @@
final int delta = (requestNextIMM ? 1 : -1);
do {
- cal.add(Calendar.MONTH, delta);
+ cal.add(MONTH, delta);
} while (!isIMMMonth(cal));
moveToIMMDay(cal);
- final int month = cal.get(Calendar.MONTH);
- if ((period == IMMPeriod.BI_ANNUALY_JUN_DEC && (Calendar.MARCH == month || Calendar.SEPTEMBER == month))
- || (period == IMMPeriod.BI_ANNUALY_MAR_SEP && (Calendar.JUNE == month || Calendar.DECEMBER == month))) {
- // need to move to the next one.
- cal = getNextIMMDate(requestNextIMM, cal, period);
- } else if (period == IMMPeriod.ANNUALLY) {
+ final int month = cal.get(MONTH);
+
+ switch (period) {
+ case BI_ANNUALY_JUN_DEC:
+ if (month == MARCH || month == SEPTEMBER) {
+ // need to move to the next one.
+ cal = getNextIMMDate(requestNextIMM, cal, period);
+ }
+ break;
+
+ case BI_ANNUALY_MAR_SEP:
+ if (month == JUNE || month == DECEMBER) {
+ // need to move to the next one.
+ cal = getNextIMMDate(requestNextIMM, cal, period);
+ }
+ break;
+
+ case ANNUALLY:
// second jump
- cal = getNextIMMDate(requestNextIMM, cal, IMMPeriod.QUARTERLY);
+ cal = getNextIMMDate(requestNextIMM, cal, QUARTERLY);
// third jump
- cal = getNextIMMDate(requestNextIMM, cal, IMMPeriod.QUARTERLY);
+ cal = getNextIMMDate(requestNextIMM, cal, QUARTERLY);
// fourth jump
- cal = getNextIMMDate(requestNextIMM, cal, IMMPeriod.QUARTERLY);
+ cal = getNextIMMDate(requestNextIMM, cal, QUARTERLY);
// fifth jump
- cal = getNextIMMDate(requestNextIMM, cal, IMMPeriod.QUARTERLY);
+ cal = getNextIMMDate(requestNextIMM, cal, QUARTERLY);
+ break;
}
return cal;
@@ -130,13 +152,13 @@
// -----------------------------------------------------------------------
private boolean isIMMMonth(final Calendar cal) {
- final int month = cal.get(Calendar.MONTH);
+ final int month = cal.get(MONTH);
switch (month) {
- case Calendar.MARCH:
- case Calendar.JUNE:
- case Calendar.SEPTEMBER:
- case Calendar.DECEMBER:
+ case MARCH:
+ case JUNE:
+ case SEPTEMBER:
+ case DECEMBER:
return true;
default:
return false;
@@ -149,18 +171,18 @@
* @param cal
*/
private void moveToIMMDay(final Calendar cal) {
- cal.set(Calendar.DAY_OF_MONTH, 1);
+ cal.set(DAY_OF_MONTH, 1);
// go to 1st wed
final int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
- if (dayOfWeek < Calendar.WEDNESDAY) {
- cal.add(Calendar.DAY_OF_MONTH, Calendar.WEDNESDAY - dayOfWeek);
- } else if (dayOfWeek > Calendar.WEDNESDAY) {
- cal.add(Calendar.DAY_OF_MONTH, (Calendar.WEDNESDAY + NUMBER_DAYS_IN_WEEK) - dayOfWeek);
+ if (dayOfWeek < WEDNESDAY) {
+ cal.add(DAY_OF_MONTH, WEDNESDAY - dayOfWeek);
+ } else if (dayOfWeek > WEDNESDAY) {
+ cal.add(DAY_OF_MONTH, (WEDNESDAY + NUMBER_DAYS_IN_WEEK) - dayOfWeek);
}
// go to 3rd wednesday - i.e. move 2 weeks forward
- cal.add(Calendar.DAY_OF_MONTH, NUMBER_DAYS_IN_WEEK * 2);
+ cal.add(DAY_OF_MONTH, NUMBER_DAYS_IN_WEEK * 2);
}
public boolean isIMMDate(final Calendar date) {
Modified: trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarKitCalculatorsFactory.java
===================================================================
--- trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarKitCalculatorsFactory.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarKitCalculatorsFactory.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -32,6 +32,12 @@
*/
package net.objectlab.kit.datecalc.jdk;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.BACKWARD;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_FOLLOWING;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_PRECEEDING;
+
import java.util.Calendar;
import net.objectlab.kit.datecalc.common.AbstractKitCalculatorsFactory;
@@ -81,15 +87,15 @@
if (holidayHandlerType == null) {
return cal;
- } else if (HolidayHandlerType.FORWARD.equals(holidayHandlerType)) {
+ } else if (FORWARD.equals(holidayHandlerType)) {
cal.setHolidayHandler(new CalendarForwardHandler());
- } else if (HolidayHandlerType.BACKWARD.equals(holidayHandlerType)) {
+ } else if (BACKWARD.equals(holidayHandlerType)) {
cal.setHolidayHandler(new CalendarBackwardHandler());
- } else if (HolidayHandlerType.MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
+ } else if (MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
cal.setHolidayHandler(new CalendarModifiedFollowingHandler());
- } else if (HolidayHandlerType.MODIFIED_PRECEEDING.equals(holidayHandlerType)) {
+ } else if (MODIFIED_PRECEEDING.equals(holidayHandlerType)) {
cal.setHolidayHandler(new CalendarModifiedPreceedingHandler());
- } else if (HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
+ } else if (FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
cal.setHolidayHandler(new CalendarForwardUnlessNegativeHandler());
} else {
throw new IllegalArgumentException("Unsupported HolidayHandler: " + holidayHandlerType);
Modified: trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarPeriodCountCalculator.java
===================================================================
--- trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarPeriodCountCalculator.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/CalendarPeriodCountCalculator.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -53,10 +53,13 @@
public int dayDiff(final Calendar start, final Calendar end, final PeriodCountBasis basis) {
int diff = 0;
-
- if (basis == PeriodCountBasis.CONV_30_360) {
- int dayStart = start.get(Calendar.DAY_OF_MONTH);
- int dayEnd = end.get(Calendar.DAY_OF_MONTH);
+ int dayStart;
+ int dayEnd;
+
+ switch (basis) {
+ case CONV_30_360:
+ dayStart = start.get(Calendar.DAY_OF_MONTH);
+ dayEnd = end.get(Calendar.DAY_OF_MONTH);
if (dayEnd == MONTH_31_DAYS && dayStart >= MONTH_30_DAYS) {
dayEnd = MONTH_30_DAYS;
}
@@ -65,10 +68,11 @@
}
diff = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * YEAR_360
+ (end.get(Calendar.MONTH) - start.get(Calendar.MONTH)) * MONTH_30_DAYS + dayEnd - dayStart;
+ break;
- } else if (basis == PeriodCountBasis.CONV_360E_ISDA) {
- int dayStart = start.get(Calendar.DAY_OF_MONTH);
- int dayEnd = end.get(Calendar.DAY_OF_MONTH);
+ case CONV_360E_ISDA:
+ dayStart = start.get(Calendar.DAY_OF_MONTH);
+ dayEnd = end.get(Calendar.DAY_OF_MONTH);
final int monthStart = start.get(Calendar.MONTH);
if ((monthStart == 2 && start.getActualMaximum(Calendar.DAY_OF_MONTH) == dayStart) || dayEnd == MONTH_31_DAYS) {
dayEnd = MONTH_30_DAYS;
@@ -79,10 +83,11 @@
diff = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * YEAR_360
+ (end.get(Calendar.MONTH) - start.get(Calendar.MONTH)) * MONTH_30_DAYS + dayEnd - dayStart;
+ break;
- } else if (basis == PeriodCountBasis.CONV_360E_ISMA) {
- int dayStart = start.get(Calendar.DAY_OF_MONTH);
- int dayEnd = end.get(Calendar.DAY_OF_MONTH);
+ case CONV_360E_ISMA:
+ dayStart = start.get(Calendar.DAY_OF_MONTH);
+ dayEnd = end.get(Calendar.DAY_OF_MONTH);
if (dayEnd == MONTH_31_DAYS) {
dayEnd = MONTH_30_DAYS;
}
@@ -91,9 +96,12 @@
}
diff = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * YEAR_360
+ (end.get(Calendar.MONTH) - start.get(Calendar.MONTH)) * MONTH_30_DAYS + dayEnd - dayStart;
- } else {
+ break;
+
+ default:
diff = dayDiff(start, end);
}
+
return diff;
}
@@ -117,7 +125,9 @@
public double yearDiff(final Calendar start, final Calendar end, final PeriodCountBasis basis) {
double diff = 0.0;
- if (basis == PeriodCountBasis.ACT_ACT) {
+
+ switch (basis) {
+ case ACT_ACT:
final int startYear = start.get(Calendar.YEAR);
final int endYear = end.get(Calendar.YEAR);
if (startYear != endYear) {
@@ -132,16 +142,24 @@
diff = (diff1 + 1.0) / start.getActualMaximum(Calendar.DAY_OF_YEAR) + (endYear - startYear - 1.0) + (diff2)
/ (double) end.getActualMaximum(Calendar.DAY_OF_YEAR);
}
+ break;
- } else if (basis == PeriodCountBasis.CONV_30_360 || basis == PeriodCountBasis.CONV_360E_ISDA
- || basis == PeriodCountBasis.CONV_360E_ISMA || basis == PeriodCountBasis.ACT_360) {
+ case CONV_30_360:
+ case CONV_360E_ISDA:
+ case CONV_360E_ISMA:
+ case ACT_360:
diff = (dayDiff(start, end, basis)) / YEAR_360_0;
+ break;
- } else if (basis == PeriodCountBasis.ACT_365 || basis == PeriodCountBasis.END_365) {
+ case ACT_365:
+ case END_365:
diff = (dayDiff(start, end, basis)) / YEAR_365_0;
- } else {
+ break;
+
+ default:
throw new UnsupportedOperationException("Sorry no ACT_UST yet");
}
+
return diff;
}
Modified: trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/DateKitCalculatorsFactory.java
===================================================================
--- trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/DateKitCalculatorsFactory.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-jdk/src/main/java/net/objectlab/kit/datecalc/jdk/DateKitCalculatorsFactory.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -32,11 +32,16 @@
*/
package net.objectlab.kit.datecalc.jdk;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.BACKWARD;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_FOLLOWING;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_PRECEEDING;
+
import java.util.Date;
import net.objectlab.kit.datecalc.common.AbstractKitCalculatorsFactory;
import net.objectlab.kit.datecalc.common.DateCalculator;
-import net.objectlab.kit.datecalc.common.HolidayHandlerType;
import net.objectlab.kit.datecalc.common.IMMDateCalculator;
import net.objectlab.kit.datecalc.common.PeriodCountCalculator;
@@ -81,15 +86,15 @@
if (holidayHandlerType == null) {
return cal;
- } else if (HolidayHandlerType.FORWARD.equals(holidayHandlerType)) {
+ } else if (FORWARD.equals(holidayHandlerType)) {
cal.setHolidayHandler(new DateForwardHandler());
- } else if (HolidayHandlerType.BACKWARD.equals(holidayHandlerType)) {
+ } else if (BACKWARD.equals(holidayHandlerType)) {
cal.setHolidayHandler(new DateBackwardHandler());
- } else if (HolidayHandlerType.MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
+ } else if (MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
cal.setHolidayHandler(new DateModifiedFollowingHandler());
- } else if (HolidayHandlerType.MODIFIED_PRECEEDING.equals(holidayHandlerType)) {
+ } else if (MODIFIED_PRECEEDING.equals(holidayHandlerType)) {
cal.setHolidayHandler(new DateModifiedPreceedingHandler());
- } else if (HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
+ } else if (FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
cal.setHolidayHandler(new DateForwardUnlessNegativeHandler());
} else {
throw new IllegalArgumentException("Unsupported HolidayHandler: " + holidayHandlerType);
Modified: trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateIMMDateCalculator.java
===================================================================
--- trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateIMMDateCalculator.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateIMMDateCalculator.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -32,6 +32,12 @@
*/
package net.objectlab.kit.datecalc.joda;
+import static net.objectlab.kit.datecalc.common.IMMPeriod.QUARTERLY;
+import static org.joda.time.DateTimeConstants.DECEMBER;
+import static org.joda.time.DateTimeConstants.JUNE;
+import static org.joda.time.DateTimeConstants.MARCH;
+import static org.joda.time.DateTimeConstants.SEPTEMBER;
+
import java.util.ArrayList;
import java.util.List;
@@ -95,19 +101,32 @@
LocalDate imm = calculate3rdWednesday(date);
final int immMonth = imm.getMonthOfYear();
- final boolean isMarchSept = DateTimeConstants.MARCH == immMonth || DateTimeConstants.SEPTEMBER == immMonth;
+ final boolean isMarchSept = immMonth == MARCH || immMonth == SEPTEMBER;
- if (period == IMMPeriod.BI_ANNUALY_JUN_DEC && isMarchSept || period == IMMPeriod.BI_ANNUALY_MAR_SEP && !isMarchSept) {
- imm = getNextIMMDate(requestNextIMM, imm, period);
- } else if (period == IMMPeriod.ANNUALLY) {
+ switch (period) {
+
+ case BI_ANNUALY_JUN_DEC:
+ if (isMarchSept) {
+ imm = getNextIMMDate(requestNextIMM, imm, period);
+ }
+ break;
+
+ case BI_ANNUALY_MAR_SEP:
+ if (!isMarchSept) {
+ imm = getNextIMMDate(requestNextIMM, imm, period);
+ }
+ break;
+
+ case ANNUALLY:
// second jump
- imm = getNextIMMDate(requestNextIMM, imm, IMMPeriod.QUARTERLY);
+ imm = getNextIMMDate(requestNextIMM, imm, QUARTERLY);
// third jump
- imm = getNextIMMDate(requestNextIMM, imm, IMMPeriod.QUARTERLY);
+ imm = getNextIMMDate(requestNextIMM, imm, QUARTERLY);
// fourth jump
- imm = getNextIMMDate(requestNextIMM, imm, IMMPeriod.QUARTERLY);
+ imm = getNextIMMDate(requestNextIMM, imm, QUARTERLY);
// fifth jump
- imm = getNextIMMDate(requestNextIMM, imm, IMMPeriod.QUARTERLY);
+ imm = getNextIMMDate(requestNextIMM, imm, QUARTERLY);
+ break;
}
return imm;
@@ -125,10 +144,10 @@
int monthOffset = 0;
switch (month) {
- case DateTimeConstants.MARCH:
- case DateTimeConstants.JUNE:
- case DateTimeConstants.SEPTEMBER:
- case DateTimeConstants.DECEMBER:
+ case MARCH:
+ case JUNE:
+ case SEPTEMBER:
+ case DECEMBER:
final LocalDate immDate = calculate3rdWednesday(date);
if (requestNextIMM && !date.isBefore(immDate)) {
date = date.plusMonths(MONTHS_IN_QUARTER);
@@ -176,7 +195,7 @@
public boolean isIMMDate(final LocalDate date) {
boolean same = false;
- final List<LocalDate> dates = getIMMDates(date.minusDays(1), date, IMMPeriod.QUARTERLY);
+ final List<LocalDate> dates = getIMMDates(date.minusDays(1), date, QUARTERLY);
if (!dates.isEmpty()) {
same = date.equals(dates.get(0));
Modified: trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateKitCalculatorsFactory.java
===================================================================
--- trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateKitCalculatorsFactory.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateKitCalculatorsFactory.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -32,6 +32,10 @@
*/
package net.objectlab.kit.datecalc.joda;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.BACKWARD;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_FOLLOWING;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_PRECEEDING;
import net.objectlab.kit.datecalc.common.AbstractKitCalculatorsFactory;
import net.objectlab.kit.datecalc.common.HolidayHandlerType;
import net.objectlab.kit.datecalc.common.IMMDateCalculator;
@@ -86,13 +90,13 @@
if (HolidayHandlerType.FORWARD.equals(holidayHandlerType)) {
cal.setHolidayHandler(new LocalDateForwardHandler());
- } else if (HolidayHandlerType.BACKWARD.equals(holidayHandlerType)) {
+ } else if (BACKWARD.equals(holidayHandlerType)) {
cal.setHolidayHandler(new LocalDateBackwardHandler());
- } else if (HolidayHandlerType.MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
+ } else if (MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
cal.setHolidayHandler(new LocalDateModifiedFollowingHandler());
- } else if (HolidayHandlerType.MODIFIED_PRECEEDING.equals(holidayHandlerType)) {
+ } else if (MODIFIED_PRECEEDING.equals(holidayHandlerType)) {
cal.setHolidayHandler(new LocalDateModifiedPreceedingHandler());
- } else if (HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
+ } else if (FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
cal.setHolidayHandler(new LocalDateForwardUnlessNegativeHandler());
} else if (holidayHandlerType != null) {
throw new IllegalArgumentException("Unsupported HolidayHandler: " + holidayHandlerType);
Modified: trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDatePeriodCountCalculator.java
===================================================================
--- trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDatePeriodCountCalculator.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDatePeriodCountCalculator.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -52,10 +52,13 @@
public int dayDiff(final LocalDate start, final LocalDate end, final PeriodCountBasis basis) {
int diff = 0;
+ int dayStart;
+ int dayEnd;
- if (basis == PeriodCountBasis.CONV_30_360) {
- int dayStart = start.getDayOfMonth();
- int dayEnd = end.getDayOfMonth();
+ switch (basis) {
+ case CONV_30_360:
+ dayStart = start.getDayOfMonth();
+ dayEnd = end.getDayOfMonth();
if (dayEnd == MONTH_31_DAYS && dayStart >= MONTH_30_DAYS) {
dayEnd = MONTH_30_DAYS;
}
@@ -64,10 +67,11 @@
}
diff = (end.getYear() - start.getYear()) * YEAR_360 + (end.getMonthOfYear() - start.getMonthOfYear()) * MONTH_30_DAYS
+ dayEnd - dayStart;
+ break;
- } else if (basis == PeriodCountBasis.CONV_360E_ISDA) {
- int dayStart = start.getDayOfMonth();
- int dayEnd = end.getDayOfMonth();
+ case CONV_360E_ISDA:
+ dayStart = start.getDayOfMonth();
+ dayEnd = end.getDayOfMonth();
final int monthStart = start.getMonthOfYear();
if ((monthStart == 2 && start.monthOfYear().getMaximumValue() == dayStart) || dayEnd == MONTH_31_DAYS) {
dayEnd = MONTH_30_DAYS;
@@ -78,10 +82,11 @@
diff = (end.getYear() - start.getYear()) * YEAR_360 + (end.getMonthOfYear() - start.getMonthOfYear()) * MONTH_30_DAYS
+ dayEnd - dayStart;
+ break;
- } else if (basis == PeriodCountBasis.CONV_360E_ISMA) {
- int dayStart = start.getDayOfMonth();
- int dayEnd = end.getDayOfMonth();
+ case CONV_360E_ISMA:
+ dayStart = start.getDayOfMonth();
+ dayEnd = end.getDayOfMonth();
if (dayEnd == MONTH_31_DAYS) {
dayEnd = MONTH_30_DAYS;
}
@@ -90,11 +95,12 @@
}
diff = (end.getYear() - start.getYear()) * YEAR_360 + (end.getMonthOfYear() - start.getMonthOfYear()) * MONTH_30_DAYS
+ dayEnd - dayStart;
- } else {
-
+ break;
+ default:
final Period p = new Period(start, end, PeriodType.days());
diff = p.getDays();
}
+
return diff;
}
@@ -106,14 +112,15 @@
//
// -----------------------------------------------------------------------
- public double monthDiff(final LocalDate start, final LocalDate end, final PeriodCountBasis basis) {
+ public double monthDiff(final LocalDate start, final LocalDate end, final PeriodCountBasis basis) {
return yearDiff(start, end, basis) * MONTHS_IN_YEAR;
}
public double yearDiff(final LocalDate start, final LocalDate end, final PeriodCountBasis basis) {
double diff = 0.0;
- if (basis == PeriodCountBasis.ACT_ACT) {
+ switch (basis) {
+ case ACT_ACT:
final int startYear = start.getYear();
final int endYear = end.getYear();
if (startYear != endYear) {
@@ -125,16 +132,24 @@
diff = ((diff1 + 1.0)) / start.dayOfYear().getMaximumValue() + ((endYear - startYear - 1.0)) + ((double) (diff2))
/ (double) end.dayOfYear().getMaximumValue();
}
+ break;
- } else if (basis == PeriodCountBasis.CONV_30_360 || basis == PeriodCountBasis.CONV_360E_ISDA
- || basis == PeriodCountBasis.CONV_360E_ISMA || basis == PeriodCountBasis.ACT_360) {
+ case CONV_30_360:
+ case CONV_360E_ISDA:
+ case CONV_360E_ISMA:
+ case ACT_360:
diff = (dayDiff(start, end, basis)) / YEAR_360_0;
+ break;
+
+ case ACT_365:
+ case END_365:
+ diff = (dayDiff(start, end, basis)) / YEAR_365_0;
+ break;
- } else if (basis == PeriodCountBasis.ACT_365 || basis == PeriodCountBasis.END_365) {
- diff = (dayDiff(start, end, basis)) / YEAR_365_0;
- } else {
- throw new UnsupportedOperationException("Sorry no ACT_UST yet");
+ default:
+ throw new UnsupportedOperationException("Sorry ACT_UST is not supported");
}
+
return diff;
}
}
Modified: trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/YearMonthDayKitCalculatorsFactory.java
===================================================================
--- trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/YearMonthDayKitCalculatorsFactory.java 2007-01-08 15:50:25 UTC (rev 241)
+++ trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/YearMonthDayKitCalculatorsFactory.java 2007-01-09 15:50:09 UTC (rev 242)
@@ -32,9 +32,13 @@
*/
package net.objectlab.kit.datecalc.joda;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.BACKWARD;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_FOLLOWING;
+import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_PRECEEDING;
import net.objectlab.kit.datecalc.common.AbstractKitCalculatorsFactory;
import net.objectlab.kit.datecalc.common.DateCalculator;
-import net.objectlab.kit.datecalc.common.HolidayHandlerType;
import net.objectlab.kit.datecalc.common.IMMDateCalculator;
import net.objectlab.kit.datecalc.common.KitCalculatorsFactory;
import net.objectlab.kit.datecalc.common.PeriodCountCalculator;
@@ -90,15 +94,15 @@
cal.setName(name);
setHolidays(name, cal);
- if (HolidayHandlerType.FORWARD.equals(holidayHandlerType)) {
+ if (FORWARD.equals(holidayHandlerType)) {
cal.setHolidayHandler(new YearMonthDayForwardHandler());
- } else if (HolidayHandlerType.BACKWARD.equals(holidayHandlerType)) {
+ } else if (BACKWARD.equals(holidayHandlerType)) {
cal.setHolidayHandler(new YearMonthDayBackwardHandler());
- } else if (HolidayHandlerType.MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
+ } else if (MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
cal.setHolidayHandler(new YearMonthDayModifiedFollowingHandler());
- } else if (HolidayHandlerType.MODIFIED_PRECEEDING.equals(holidayHandlerType)) {
+ } else if (MODIFIED_PRECEEDING.equals(holidayHandlerType)) {
cal.setHolidayHandler(new YearMonthDayModifiedPreceedingHandler());
- } else if (HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
+ } else if (FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
cal.setHolidayHandler(new YearMonthDayForwardUnlessNegativeHandler());
} else if (holidayHandlerType != null) {
throw new IllegalArgumentException("Unsupported HolidayHandler: " + holidayHandlerType);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|