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.jdk;
34
35 import java.util.Calendar;
36 import java.util.Collections;
37
38 import net.objectlab.kit.datecalc.common.AbstractDateCalculator;
39 import net.objectlab.kit.datecalc.common.DateCalculator;
40 import net.objectlab.kit.datecalc.common.DefaultHolidayCalendar;
41 import net.objectlab.kit.datecalc.common.HolidayCalendar;
42 import net.objectlab.kit.datecalc.common.HolidayHandler;
43 import net.objectlab.kit.datecalc.common.Utils;
44 import net.objectlab.kit.datecalc.common.WorkingWeek;
45
46
47
48
49
50
51
52
53 public class CalendarDateCalculator extends AbstractDateCalculator<Calendar> {
54
55 private WorkingWeek workingWeek = WorkingWeek.DEFAULT;
56
57 public CalendarDateCalculator() {
58 this(null, null, new DefaultHolidayCalendar<Calendar>(Collections.<Calendar> emptySet()), null);
59 }
60
61 public CalendarDateCalculator(final String name, final Calendar startDate, final HolidayCalendar<Calendar> holidayCalendar,
62 final HolidayHandler<Calendar> holidayHandler) {
63 super(name, holidayCalendar, holidayHandler);
64 Calendar date = startDate;
65 if (date == null) {
66 date = getToday();
67 } else {
68 setStartDate(date);
69 }
70 }
71
72 @Override
73 public DateCalculator<Calendar> setWorkingWeek(final WorkingWeek week) {
74 workingWeek = week;
75 return this;
76 }
77
78
79
80
81 @Override
82 public boolean isWeekend(final Calendar date) {
83 assert workingWeek != null;
84 return !workingWeek.isWorkingDay(date);
85 }
86
87
88
89
90
91
92
93
94
95 @Override
96 public CalendarDateCalculator moveByDays(final int days) {
97 setCurrentIncrement(days);
98 getCurrentBusinessDate().add(Calendar.DAY_OF_MONTH, days);
99
100 if (getHolidayHandler() != null) {
101 setCurrentBusinessDate(getHolidayHandler().moveCurrentDate(this));
102 }
103
104 return this;
105 }
106
107 @Override
108 public DateCalculator<Calendar> moveByMonths(final int months) {
109 setCurrentIncrement(months);
110 final Calendar date = getCurrentBusinessDate();
111 date.add(Calendar.MONTH, months);
112
113 setCurrentBusinessDate(date);
114
115 if (getHolidayHandler() != null) {
116 setCurrentBusinessDate(getHolidayHandler().moveCurrentDate(this));
117 }
118
119 return this;
120 }
121
122 @Override
123 protected DateCalculator<Calendar> createNewCalculator(final String name, final Calendar startDate, final HolidayCalendar<Calendar> holidays,
124 final HolidayHandler<Calendar> handler) {
125 return new CalendarDateCalculator(name, startDate, holidays, handler);
126 }
127
128 @Override
129 protected final Calendar getToday() {
130 return Utils.blastTime(Calendar.getInstance());
131 }
132
133 @Override
134 protected Calendar compareDate(final Calendar date1, final Calendar date2, final boolean returnEarliest) {
135 if (date1 == null || date2 == null) {
136 return null;
137 }
138 if (returnEarliest) {
139 return date1.after(date2) ? date2 : date1;
140 } else {
141 return date2.after(date1) ? date2 : date1;
142 }
143 }
144
145 @Override
146 protected void checkBoundary(final Calendar date) {
147 final Calendar early = getHolidayCalendar().getEarlyBoundary();
148 if (early != null && early.after(date)) {
149 throw new IndexOutOfBoundsException(date + " is before the early boundary " + early);
150 }
151
152 final Calendar late = getHolidayCalendar().getLateBoundary();
153 if (late != null && late.before(date)) {
154 throw new IndexOutOfBoundsException(date + " is after the late boundary " + late);
155 }
156 }
157
158 @Override
159 protected Calendar clone(final Calendar date) {
160 final Calendar cal = Calendar.getInstance();
161 cal.setTime(date.getTime());
162 return cal;
163 }
164
165 @Override
166 public int getNumberOfBusinessDaysBetween(final Calendar d1, final Calendar d2) {
167 if (d1 == null || d2 == null) {
168 return 0;
169 }
170 final boolean d1B4d2 = !d1.after(d2);
171 Calendar start = d1B4d2 ? d1 : d2;
172 final Calendar end = d1B4d2 ? d2 : d1;
173 if (getHolidayHandler() != null) {
174 start = getHolidayHandler().adjustDate(start, 1, this);
175 }
176
177 int count = 0;
178
179 while (start.before(end)) {
180 if (!isNonWorkingDay(start)) {
181 count++;
182 }
183 start.add(Calendar.DATE, 1);
184 }
185 return d1B4d2 ? count : -count;
186 }
187
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205