|
From: <be...@us...> - 2006-09-01 12:02:27
|
Revision: 70
http://svn.sourceforge.net/objectlabkit/?rev=70&view=rev
Author: benoitx
Date: 2006-09-01 05:02:20 -0700 (Fri, 01 Sep 2006)
Log Message:
-----------
Added an implementation (untested) for Joda YearMonthDay based as much as possible on the LocalDate one.
Modified Paths:
--------------
trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/DefaultDateCalculatorFactory.java
Added Paths:
-----------
trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/HolidayHandlerYearMonthDayWrapper.java
trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateCalculator.java
trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/YearMonthDayDateCalculator.java
Removed Paths:
-------------
trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/BaseDateCalculator.java
Deleted: trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/BaseDateCalculator.java
===================================================================
--- trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/BaseDateCalculator.java 2006-08-27 09:47:25 UTC (rev 69)
+++ trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/BaseDateCalculator.java 2006-09-01 12:02:20 UTC (rev 70)
@@ -1,171 +0,0 @@
-/*
- * Copyright 2006 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-package net.objectlab.kit.datecalc.joda;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
-
-import net.objectlab.kit.datecalc.common.AbstractDateCalculator;
-import net.objectlab.kit.datecalc.common.DateCalculator;
-import net.objectlab.kit.datecalc.common.HolidayHandler;
-import net.objectlab.kit.datecalc.common.WorkingWeek;
-
-import org.joda.time.DateTimeConstants;
-import org.joda.time.LocalDate;
-
-/**
- * This class is used via the DateCalculator interface, it enables the handling
- * of different HolidayHandler, if no HolidayHandler is defined, the calendar
- * will NOT move a date, even if it falls on a holiday or weekend.
- *
- * @author Benoit Xhenseval
- */
-public class BaseDateCalculator extends AbstractDateCalculator<LocalDate> {
-
- private JodaWorkingWeek workingWeek = JodaWorkingWeek.DEFAULT;
-
- @SuppressWarnings("unchecked")
- public BaseDateCalculator() {
- this(null, null, Collections.EMPTY_SET, null);
- }
-
- public BaseDateCalculator(final String name, final LocalDate startDate, final Set<LocalDate> nonWorkingDays,
- final HolidayHandler<LocalDate> holidayHandler) {
- super(name, nonWorkingDays, holidayHandler);
- setStartDate(startDate);
- }
-
- public void setWorkingWeek(final WorkingWeek week) {
- if (week instanceof JodaWorkingWeek) {
- workingWeek = (JodaWorkingWeek) week;
- }
- }
-
- /**
- * is the date a non-working day according to the WorkingWeek?
- */
- public boolean isWeekend(final LocalDate date) {
- assert workingWeek != null;
- return !workingWeek.isWorkingDay(date);
- }
-
- public DateCalculator<LocalDate> moveByDays(final int days) {
- if (getCurrentBusinessDate() == null) {
- initialise();
- }
- setCurrentBusinessDate(getCurrentBusinessDate().plusDays(days));
-
- if (getHolidayHandler() != null) {
- setCurrentBusinessDate(getHolidayHandler().moveCurrentDate(this));
- }
-
- return this;
- }
-
- private void initialise() {
- if (getStartDate() == null) {
- setStartDate(new LocalDate());
- } else if (getCurrentBusinessDate() == null) {
- setCurrentBusinessDate(new LocalDate());
- }
- }
-
- @Override
- protected DateCalculator<LocalDate> createNewCalcultaor(final String name, final LocalDate startDate,
- final Set<LocalDate> holidays, final HolidayHandler<LocalDate> handler) {
- return new BaseDateCalculator(name, startDate, holidays, handler);
- }
-
- public List<LocalDate> getIMMDates(final LocalDate start, final LocalDate end) {
- final List<LocalDate> dates = new ArrayList<LocalDate>();
-
- LocalDate date = start;
- while (true) {
- date = getNextIMMDate(true, date);
- if (!date.isAfter(end)) {
- dates.add(date);
- } else {
- break;
- }
- }
-
- return dates;
- }
-
- @Override
- protected LocalDate getNextIMMDate(final boolean forward, final LocalDate start) {
- LocalDate date = start;
-
- final int month = date.getMonthOfYear();
- int monthOffset = 0;
-
- switch (month) {
- case DateTimeConstants.MARCH:
- case DateTimeConstants.JUNE:
- case DateTimeConstants.SEPTEMBER:
- case DateTimeConstants.DECEMBER:
- final LocalDate immDate = calculate3rdWednesday(date);
- if (forward && !date.isBefore(immDate)) {
- date = date.plusMonths(MONTHS_IN_QUARTER);
- } else if (!forward && !date.isAfter(immDate)) {
- date = date.minusMonths(MONTHS_IN_QUARTER);
- }
- break;
-
- default:
- // Jan 1 -> 2
- // Feb 2 -> 1
- // Mar 3 -> 0
- // Apr 4 -> 2
- // May 5 -> 1
- // Jun 6 -> 0
- // Jul 7 -> 2
- // Aug 8 -> 1
- // Sep 9 -> 0
- // Oct 10 -> 2
- // Nov 11 -> 1
- // Dec 12 -> 0
- if (forward) {
- monthOffset = (MONTH_IN_YEAR - month) % MONTHS_IN_QUARTER;
- date = date.plusMonths(monthOffset);
- } else {
- monthOffset = month % MONTHS_IN_QUARTER;
- date = date.minusMonths(monthOffset);
- }
- break;
- }
-
- return calculate3rdWednesday(date);
- }
-
- /**
- * Assumes that the month is correct, get the day for the 2rd wednesday.
- *
- * @param original
- * the start date
- * @return the 3rd Wednesday of the month
- */
- private LocalDate calculate3rdWednesday(final LocalDate original) {
- final LocalDate firstOfMonth = original.withDayOfMonth(1);
- LocalDate firstWed = firstOfMonth.withDayOfWeek(MONTHS_IN_QUARTER);
- if (firstWed.isBefore(firstOfMonth)) {
- firstWed = firstWed.plusWeeks(1);
- }
- return firstWed.plusWeeks(2);
- }
-}
Modified: trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/DefaultDateCalculatorFactory.java
===================================================================
--- trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/DefaultDateCalculatorFactory.java 2006-08-27 09:47:25 UTC (rev 69)
+++ trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/DefaultDateCalculatorFactory.java 2006-09-01 12:02:20 UTC (rev 70)
@@ -46,7 +46,7 @@
* @return a new DateCalculator
*/
public DateCalculator<LocalDate> getDateCalculator(final String name, final String holidayHandlerType) {
- final BaseDateCalculator cal = new BaseDateCalculator();
+ final LocalDateCalculator cal = new LocalDateCalculator();
cal.setName(name);
if (holidays.containsKey(name)) {
cal.setNonWorkingDays(holidays.get(name));
Added: trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/HolidayHandlerYearMonthDayWrapper.java
===================================================================
--- trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/HolidayHandlerYearMonthDayWrapper.java (rev 0)
+++ trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/HolidayHandlerYearMonthDayWrapper.java 2006-09-01 12:02:20 UTC (rev 70)
@@ -0,0 +1,57 @@
+/*
+ * $Id: org.eclipse.jdt.ui.prefs 59 2006-08-26 09:06:39Z marchy $
+ *
+ * Copyright 2006 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package net.objectlab.kit.datecalc.joda;
+
+import net.objectlab.kit.datecalc.common.DateCalculator;
+import net.objectlab.kit.datecalc.common.HolidayHandler;
+
+import org.joda.time.LocalDate;
+import org.joda.time.YearMonthDay;
+
+/**
+ *
+ * @author xhensevb
+ * @author $LastChangedBy: marchy $
+ * @version $Revision: 59 $ $Date: 2006-08-26 11:06:39 +0200 (Sat, 26 Aug 2006) $
+ *
+ */
+public class HolidayHandlerYearMonthDayWrapper implements HolidayHandler<LocalDate> {
+
+ private HolidayHandler<YearMonthDay> delegate;
+ private DateCalculator<YearMonthDay> calculator;
+
+ protected HolidayHandlerYearMonthDayWrapper(HolidayHandler<YearMonthDay> delegate, DateCalculator<YearMonthDay> calculator) {
+ this.delegate = delegate;
+ this.calculator = calculator;
+ }
+
+ /* (non-Javadoc)
+ * @see net.objectlab.kit.datecalc.common.HolidayHandler#getType()
+ */
+ public String getType() {
+ return delegate.getType();
+ }
+
+ /* (non-Javadoc)
+ * @see net.objectlab.kit.datecalc.common.HolidayHandler#moveCurrentDate(net.objectlab.kit.datecalc.common.DateCalculator)
+ */
+ public LocalDate moveCurrentDate(DateCalculator<LocalDate> calendar) {
+ return delegate.moveCurrentDate(calculator).toLocalDate();
+ }
+
+}
Added: trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateCalculator.java
===================================================================
--- trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateCalculator.java (rev 0)
+++ trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/LocalDateCalculator.java 2006-09-01 12:02:20 UTC (rev 70)
@@ -0,0 +1,171 @@
+/*
+ * Copyright 2006 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package net.objectlab.kit.datecalc.joda;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import net.objectlab.kit.datecalc.common.AbstractDateCalculator;
+import net.objectlab.kit.datecalc.common.DateCalculator;
+import net.objectlab.kit.datecalc.common.HolidayHandler;
+import net.objectlab.kit.datecalc.common.WorkingWeek;
+
+import org.joda.time.DateTimeConstants;
+import org.joda.time.LocalDate;
+
+/**
+ * This class is used via the DateCalculator interface, it enables the handling
+ * of different HolidayHandler, if no HolidayHandler is defined, the calendar
+ * will NOT move a date, even if it falls on a holiday or weekend.
+ *
+ * @author Benoit Xhenseval
+ */
+public class LocalDateCalculator extends AbstractDateCalculator<LocalDate> {
+
+ private JodaWorkingWeek workingWeek = JodaWorkingWeek.DEFAULT;
+
+ @SuppressWarnings("unchecked")
+ public LocalDateCalculator() {
+ this(null, null, Collections.EMPTY_SET, null);
+ }
+
+ public LocalDateCalculator(final String name, final LocalDate startDate, final Set<LocalDate> nonWorkingDays,
+ final HolidayHandler<LocalDate> holidayHandler) {
+ super(name, nonWorkingDays, holidayHandler);
+ setStartDate(startDate);
+ }
+
+ public void setWorkingWeek(final WorkingWeek week) {
+ if (week instanceof JodaWorkingWeek) {
+ workingWeek = (JodaWorkingWeek) week;
+ }
+ }
+
+ /**
+ * is the date a non-working day according to the WorkingWeek?
+ */
+ public boolean isWeekend(final LocalDate date) {
+ assert workingWeek != null;
+ return !workingWeek.isWorkingDay(date);
+ }
+
+ public DateCalculator<LocalDate> moveByDays(final int days) {
+ if (getCurrentBusinessDate() == null) {
+ initialise();
+ }
+ setCurrentBusinessDate(getCurrentBusinessDate().plusDays(days));
+
+ if (getHolidayHandler() != null) {
+ setCurrentBusinessDate(getHolidayHandler().moveCurrentDate(this));
+ }
+
+ return this;
+ }
+
+ private void initialise() {
+ if (getStartDate() == null) {
+ setStartDate(new LocalDate());
+ } else if (getCurrentBusinessDate() == null) {
+ setCurrentBusinessDate(new LocalDate());
+ }
+ }
+
+ @Override
+ protected DateCalculator<LocalDate> createNewCalcultaor(final String name, final LocalDate startDate,
+ final Set<LocalDate> holidays, final HolidayHandler<LocalDate> handler) {
+ return new LocalDateCalculator(name, startDate, holidays, handler);
+ }
+
+ public List<LocalDate> getIMMDates(final LocalDate start, final LocalDate end) {
+ final List<LocalDate> dates = new ArrayList<LocalDate>();
+
+ LocalDate date = start;
+ while (true) {
+ date = getNextIMMDate(true, date);
+ if (!date.isAfter(end)) {
+ dates.add(date);
+ } else {
+ break;
+ }
+ }
+
+ return dates;
+ }
+
+ @Override
+ protected LocalDate getNextIMMDate(final boolean forward, final LocalDate start) {
+ LocalDate date = start;
+
+ final int month = date.getMonthOfYear();
+ int monthOffset = 0;
+
+ switch (month) {
+ case DateTimeConstants.MARCH:
+ case DateTimeConstants.JUNE:
+ case DateTimeConstants.SEPTEMBER:
+ case DateTimeConstants.DECEMBER:
+ final LocalDate immDate = calculate3rdWednesday(date);
+ if (forward && !date.isBefore(immDate)) {
+ date = date.plusMonths(MONTHS_IN_QUARTER);
+ } else if (!forward && !date.isAfter(immDate)) {
+ date = date.minusMonths(MONTHS_IN_QUARTER);
+ }
+ break;
+
+ default:
+ // Jan 1 -> 2
+ // Feb 2 -> 1
+ // Mar 3 -> 0
+ // Apr 4 -> 2
+ // May 5 -> 1
+ // Jun 6 -> 0
+ // Jul 7 -> 2
+ // Aug 8 -> 1
+ // Sep 9 -> 0
+ // Oct 10 -> 2
+ // Nov 11 -> 1
+ // Dec 12 -> 0
+ if (forward) {
+ monthOffset = (MONTH_IN_YEAR - month) % MONTHS_IN_QUARTER;
+ date = date.plusMonths(monthOffset);
+ } else {
+ monthOffset = month % MONTHS_IN_QUARTER;
+ date = date.minusMonths(monthOffset);
+ }
+ break;
+ }
+
+ return calculate3rdWednesday(date);
+ }
+
+ /**
+ * Assumes that the month is correct, get the day for the 2rd wednesday.
+ *
+ * @param original
+ * the start date
+ * @return the 3rd Wednesday of the month
+ */
+ private LocalDate calculate3rdWednesday(final LocalDate original) {
+ final LocalDate firstOfMonth = original.withDayOfMonth(1);
+ LocalDate firstWed = firstOfMonth.withDayOfWeek(MONTHS_IN_QUARTER);
+ if (firstWed.isBefore(firstOfMonth)) {
+ firstWed = firstWed.plusWeeks(1);
+ }
+ return firstWed.plusWeeks(2);
+ }
+}
Added: trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/YearMonthDayDateCalculator.java
===================================================================
--- trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/YearMonthDayDateCalculator.java (rev 0)
+++ trunk/datecalc-joda/src/main/java/net/objectlab/kit/datecalc/joda/YearMonthDayDateCalculator.java 2006-09-01 12:02:20 UTC (rev 70)
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2006 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package net.objectlab.kit.datecalc.joda;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import net.objectlab.kit.datecalc.common.AbstractDateCalculator;
+import net.objectlab.kit.datecalc.common.DateCalculator;
+import net.objectlab.kit.datecalc.common.HolidayHandler;
+import net.objectlab.kit.datecalc.common.WorkingWeek;
+
+import org.joda.time.LocalDate;
+import org.joda.time.YearMonthDay;
+
+/**
+ * This class is used via the DateCalculator interface, it enables the handling
+ * of different HolidayHandler, if no HolidayHandler is defined, the calendar
+ * will NOT move a date, even if it falls on a holiday or weekend.
+ *
+ * @author Benoit Xhenseval
+ */
+public class YearMonthDayDateCalculator extends AbstractDateCalculator<YearMonthDay> {
+
+ private LocalDateCalculator delegate;
+
+ @SuppressWarnings("unchecked")
+ public YearMonthDayDateCalculator() {
+ this(null, null, Collections.EMPTY_SET, null);
+ }
+
+ public YearMonthDayDateCalculator(final String name, final YearMonthDay startDate, final Set<YearMonthDay> nonWorkingDays,
+ final HolidayHandler<YearMonthDay> holidayHandler) {
+ super(name, nonWorkingDays, holidayHandler);
+ setStartDate(startDate);
+
+ Set<LocalDate> dates = new HashSet<LocalDate>();
+ for (YearMonthDay d : nonWorkingDays) {
+ dates.add(d.toLocalDate());
+ }
+
+ HolidayHandler<LocalDate> locDate = new HolidayHandlerYearMonthDayWrapper(holidayHandler, this);
+
+ delegate = new LocalDateCalculator(name, startDate.toLocalDate(), dates, locDate);
+ }
+
+ public void setWorkingWeek(final WorkingWeek week) {
+ delegate.setWorkingWeek(week);
+ }
+
+ /**
+ * is the date a non-working day according to the WorkingWeek?
+ */
+ public boolean isWeekend(final YearMonthDay date) {
+ return delegate.isWeekend(date.toLocalDate());
+ }
+
+ public DateCalculator<YearMonthDay> moveByDays(final int days) {
+ delegate.setCurrentBusinessDate(getCurrentBusinessDate().toLocalDate());
+ setCurrentBusinessDate(new YearMonthDay(delegate.moveByDays(days).getCurrentBusinessDate()));
+ return this;
+ // if (getCurrentBusinessDate() == null) {
+ // initialise();
+ // }
+ // setCurrentBusinessDate(getCurrentBusinessDate().plusDays(days));
+ //
+ // if (getHolidayHandler() != null) {
+ // setCurrentBusinessDate(getHolidayHandler().moveCurrentDate(this));
+ // }
+ //
+ // return this;
+ }
+
+ // private void initialise() {
+ // if (getStartDate() == null) {
+ // setStartDate(new YearMonthDay());
+ // } else if (getCurrentBusinessDate() == null) {
+ // setCurrentBusinessDate(new YearMonthDay());
+ // }
+ // }
+ //
+ @Override
+ protected DateCalculator<YearMonthDay> createNewCalcultaor(final String name, final YearMonthDay startDate,
+ final Set<YearMonthDay> holidays, final HolidayHandler<YearMonthDay> handler) {
+ return new YearMonthDayDateCalculator(name, startDate, holidays, handler);
+ }
+
+ public List<YearMonthDay> getIMMDates(final YearMonthDay start, final YearMonthDay end) {
+ final List<YearMonthDay> dates = new ArrayList<YearMonthDay>();
+ final List<LocalDate> localDates = delegate.getIMMDates(start.toLocalDate(), end.toLocalDate());
+
+ for (LocalDate d : localDates) {
+ dates.add(new YearMonthDay(d));
+ }
+
+ return dates;
+ }
+
+ @Override
+ protected YearMonthDay getNextIMMDate(final boolean forward, final YearMonthDay start) {
+ return new YearMonthDay(delegate.getNextIMMDate(forward, start.toLocalDate()));
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|