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 public class YearMonthDayDateCalculator extends AbstractDateCalculator<YearMonthDay> {
58
59 private LocalDateCalculator delegate;
60
61 @SuppressWarnings("unchecked")
62 public YearMonthDayDateCalculator() {
63 this(null, null, new DefaultHolidayCalendar<YearMonthDay>(Collections.EMPTY_SET), null);
64 }
65
66 public YearMonthDayDateCalculator(final String name, final YearMonthDay startDate, final HolidayCalendar<YearMonthDay> nonWorkingDays,
67 final HolidayHandler<YearMonthDay> holidayHandler) {
68 super(name, nonWorkingDays, holidayHandler);
69
70 final Set<LocalDate> dates = new HashSet<LocalDate>();
71 for (final YearMonthDay d : nonWorkingDays.getHolidays()) {
72 dates.add(d.toLocalDate());
73 }
74
75 final YearMonthDay early = nonWorkingDays.getEarlyBoundary();
76 final YearMonthDay late = nonWorkingDays.getLateBoundary();
77 final DefaultHolidayCalendar<LocalDate> cal = new DefaultHolidayCalendar<LocalDate>(dates, early != null ? new LocalDate(early) : null,
78 late != null ? new LocalDate(late) : null);
79
80 final HolidayHandler<LocalDate> locDate = new HolidayHandlerYearMonthDayWrapper(holidayHandler, this);
81
82 delegate = new LocalDateCalculator(name, (startDate != null ? startDate.toLocalDate() : null), cal, locDate);
83 setStartDate(startDate);
84 }
85
86
87 public DateCalculator<YearMonthDay> setWorkingWeek(final WorkingWeek week) {
88 delegate.setWorkingWeek(week);
89 return this;
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103 public boolean isWeekend(final YearMonthDay date) {
104 if (date != null && delegate != null) {
105 return delegate.isWeekend(date.toLocalDate());
106 }
107 return false;
108 }
109
110 public DateCalculator<YearMonthDay> moveByDays(final int days) {
111 setCurrentIncrement(days);
112 delegate.setCurrentIncrement(days);
113 delegate.setCurrentBusinessDate(getCurrentBusinessDate().toLocalDate());
114 setCurrentBusinessDate(new YearMonthDay(delegate.moveByDays(days).getCurrentBusinessDate()));
115 return this;
116 }
117
118 @Override
119 protected DateCalculator<YearMonthDay> createNewCalculator(final String name, final YearMonthDay startDate, final HolidayCalendar<YearMonthDay> holidays,
120 final HolidayHandler<YearMonthDay> handler) {
121 return new YearMonthDayDateCalculator(name, startDate, holidays, handler);
122 }
123
124 @Override
125 public final DateCalculator<YearMonthDay> setStartDate(final YearMonthDay startDate) {
126 if (delegate != null) {
127 delegate.setStartDate(startDate != null ? startDate.toLocalDate() : null);
128 }
129 super.setStartDate(startDate);
130 return this;
131 }
132
133 @Override
134 protected YearMonthDay getToday() {
135 return new YearMonthDay();
136 }
137
138 @Override
139 protected DateCalculator<YearMonthDay> moveByMonths(final int months) {
140 setCurrentIncrement(months);
141 delegate.setCurrentIncrement(months);
142 delegate.setCurrentBusinessDate(getCurrentBusinessDate().toLocalDate());
143 setCurrentBusinessDate(new YearMonthDay(delegate.moveByMonths(months).getCurrentBusinessDate()));
144 return this;
145 }
146
147 @Override
148 protected YearMonthDay compareDate(final YearMonthDay date1, final YearMonthDay date2, final boolean returnEarliest) {
149 if (date1 == null || date2 == null) {
150 return null;
151 }
152 if (returnEarliest) {
153 return date1.isAfter(date2) ? date2 : date1;
154 } else {
155 return date2.isAfter(date1) ? date2 : date1;
156 }
157 }
158
159 @Override
160 protected void checkBoundary(final YearMonthDay date) {
161 final YearMonthDay early = getHolidayCalendar().getEarlyBoundary();
162 if (early != null && early.isAfter(date)) {
163 throw new IndexOutOfBoundsException(date + " is before the early boundary " + early);
164 }
165
166 final YearMonthDay late = getHolidayCalendar().getLateBoundary();
167 if (late != null && late.isBefore(date)) {
168 throw new IndexOutOfBoundsException(date + " is after the late boundary " + late);
169 }
170 }
171
172 @Override
173 protected YearMonthDay clone(YearMonthDay date) {
174 return date;
175 }
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193