| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
package net.objectlab.kit.datecalc.joda; |
| 34 | |
|
| 35 | |
import java.util.Collections; |
| 36 | |
import java.util.HashSet; |
| 37 | |
import java.util.Set; |
| 38 | |
|
| 39 | |
import net.objectlab.kit.datecalc.common.AbstractDateCalculator; |
| 40 | |
import net.objectlab.kit.datecalc.common.DateCalculator; |
| 41 | |
import net.objectlab.kit.datecalc.common.DefaultHolidayCalendar; |
| 42 | |
import net.objectlab.kit.datecalc.common.HolidayCalendar; |
| 43 | |
import net.objectlab.kit.datecalc.common.HolidayHandler; |
| 44 | |
import net.objectlab.kit.datecalc.common.WorkingWeek; |
| 45 | |
|
| 46 | |
import org.joda.time.LocalDate; |
| 47 | |
import org.joda.time.YearMonthDay; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 4562 | public class YearMonthDayDateCalculator extends AbstractDateCalculator<YearMonthDay> { |
| 60 | |
|
| 61 | |
private LocalDateCalculator delegate; |
| 62 | |
|
| 63 | |
@SuppressWarnings("unchecked") |
| 64 | |
public YearMonthDayDateCalculator() { |
| 65 | 367 | this(null, null, new DefaultHolidayCalendar<YearMonthDay>(Collections.EMPTY_SET), null); |
| 66 | 367 | } |
| 67 | |
|
| 68 | |
public YearMonthDayDateCalculator(final String name, final YearMonthDay startDate, final HolidayCalendar<YearMonthDay> nonWorkingDays, |
| 69 | |
final HolidayHandler<YearMonthDay> holidayHandler) { |
| 70 | 370 | super(name, nonWorkingDays, holidayHandler); |
| 71 | |
|
| 72 | 370 | final Set<LocalDate> dates = new HashSet<LocalDate>(); |
| 73 | 370 | for (final YearMonthDay d : nonWorkingDays.getHolidays()) { |
| 74 | 14 | dates.add(d.toLocalDate()); |
| 75 | |
} |
| 76 | |
|
| 77 | 370 | final YearMonthDay early = nonWorkingDays.getEarlyBoundary(); |
| 78 | 370 | final YearMonthDay late = nonWorkingDays.getLateBoundary(); |
| 79 | 370 | final DefaultHolidayCalendar<LocalDate> cal = new DefaultHolidayCalendar<LocalDate>(dates, early != null ? new LocalDate(early) : null, |
| 80 | |
late != null ? new LocalDate(late) : null); |
| 81 | |
|
| 82 | 370 | final HolidayHandler<LocalDate> locDate = new HolidayHandlerYearMonthDayWrapper(holidayHandler, this); |
| 83 | |
|
| 84 | 370 | delegate = new LocalDateCalculator(name, (startDate != null ? startDate.toLocalDate() : null), cal, locDate); |
| 85 | 370 | setStartDate(startDate); |
| 86 | 370 | } |
| 87 | |
|
| 88 | |
|
| 89 | |
public void setWorkingWeek(final WorkingWeek week) { |
| 90 | 15 | delegate.setWorkingWeek(week); |
| 91 | 15 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public boolean isWeekend(final YearMonthDay date) { |
| 105 | 1773 | if (date != null && delegate != null) { |
| 106 | 1770 | return delegate.isWeekend(date.toLocalDate()); |
| 107 | |
} |
| 108 | 3 | return false; |
| 109 | |
} |
| 110 | |
|
| 111 | |
public DateCalculator<YearMonthDay> moveByDays(final int days) { |
| 112 | 518 | setCurrentIncrement(days); |
| 113 | 518 | delegate.setCurrentIncrement(days); |
| 114 | 518 | delegate.setCurrentBusinessDate(getCurrentBusinessDate().toLocalDate()); |
| 115 | 518 | setCurrentBusinessDate(new YearMonthDay(delegate.moveByDays(days).getCurrentBusinessDate())); |
| 116 | 518 | return this; |
| 117 | |
} |
| 118 | |
|
| 119 | |
@Override |
| 120 | |
protected DateCalculator<YearMonthDay> createNewCalculator(final String name, final YearMonthDay startDate, final HolidayCalendar<YearMonthDay> holidays, |
| 121 | |
final HolidayHandler<YearMonthDay> handler) { |
| 122 | 3 | return new YearMonthDayDateCalculator(name, startDate, holidays, handler); |
| 123 | |
} |
| 124 | |
|
| 125 | |
@Override |
| 126 | |
public final void setStartDate(final YearMonthDay startDate) { |
| 127 | 873 | if (delegate != null) { |
| 128 | 873 | delegate.setStartDate(startDate != null ? startDate.toLocalDate() : null); |
| 129 | |
} |
| 130 | 873 | super.setStartDate(startDate); |
| 131 | 873 | } |
| 132 | |
|
| 133 | |
@Override |
| 134 | |
protected YearMonthDay getToday() { |
| 135 | 6 | return new YearMonthDay(); |
| 136 | |
} |
| 137 | |
|
| 138 | |
@Override |
| 139 | |
protected DateCalculator<YearMonthDay> moveByMonths(final int months) { |
| 140 | 156 | setCurrentIncrement(months); |
| 141 | 156 | delegate.setCurrentIncrement(months); |
| 142 | 156 | delegate.setCurrentBusinessDate(getCurrentBusinessDate().toLocalDate()); |
| 143 | 156 | setCurrentBusinessDate(new YearMonthDay(delegate.moveByMonths(months).getCurrentBusinessDate())); |
| 144 | 156 | return this; |
| 145 | |
} |
| 146 | |
|
| 147 | |
@Override |
| 148 | |
protected YearMonthDay compareDate(final YearMonthDay date1, final YearMonthDay date2, final boolean returnEarliest) { |
| 149 | 6 | if (date1 == null || date2 == null) { |
| 150 | 0 | return null; |
| 151 | |
} |
| 152 | 6 | if (returnEarliest) { |
| 153 | 3 | return date1.isAfter(date2) ? date2 : date1; |
| 154 | |
} else { |
| 155 | 3 | return date2.isAfter(date1) ? date2 : date1; |
| 156 | |
} |
| 157 | |
} |
| 158 | |
|
| 159 | |
@Override |
| 160 | |
protected void checkBoundary(final YearMonthDay date) { |
| 161 | 2265 | final YearMonthDay early = getHolidayCalendar().getEarlyBoundary(); |
| 162 | 2265 | if (early != null && early.isAfter(date)) { |
| 163 | 0 | throw new IndexOutOfBoundsException(date + " is before the early boundary " + early); |
| 164 | |
} |
| 165 | |
|
| 166 | 2265 | final YearMonthDay late = getHolidayCalendar().getLateBoundary(); |
| 167 | 2265 | if (late != null && late.isBefore(date)) { |
| 168 | 0 | throw new IndexOutOfBoundsException(date + " is after the late boundary " + late); |
| 169 | |
} |
| 170 | 2265 | } |
| 171 | |
|
| 172 | |
@Override |
| 173 | |
protected YearMonthDay clone(YearMonthDay date) { |
| 174 | 6 | return date; |
| 175 | |
} |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|