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 static java.util.Calendar.DAY_OF_MONTH;
36 import static java.util.Calendar.DECEMBER;
37 import static java.util.Calendar.JUNE;
38 import static java.util.Calendar.MARCH;
39 import static java.util.Calendar.MONTH;
40 import static java.util.Calendar.SEPTEMBER;
41 import static java.util.Calendar.WEDNESDAY;
42 import static net.objectlab.kit.datecalc.common.IMMPeriod.QUARTERLY;
43
44 import java.util.ArrayList;
45 import java.util.Calendar;
46 import java.util.List;
47
48 import net.objectlab.kit.datecalc.common.AbstractIMMDateCalculator;
49 import net.objectlab.kit.datecalc.common.IMMPeriod;
50
51
52
53
54
55
56
57
58 public class CalendarIMMDateCalculator extends AbstractIMMDateCalculator<Calendar> {
59 private static final int NUMBER_DAYS_IN_WEEK = 7;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 @Override
75 public List<Calendar> getIMMDates(final Calendar start, final Calendar end, final IMMPeriod period) {
76
77 final List<Calendar> dates = new ArrayList<>();
78 Calendar cal = (Calendar) start.clone();
79 while (true) {
80 cal = getNextIMMDate(true, cal, period);
81 if (!cal.after(end)) {
82 dates.add(cal);
83 } else {
84 break;
85 }
86 }
87
88 return dates;
89 }
90
91 @Override
92 protected Calendar getNextIMMDate(final boolean requestNextIMM, final Calendar startDate, final IMMPeriod period) {
93
94 Calendar cal = (Calendar) startDate.clone();
95
96 if (isIMMMonth(cal)) {
97 moveToIMMDay(cal);
98 if (requestNextIMM && cal.after(startDate) || !requestNextIMM && cal.before(startDate)) {
99 return cal;
100 }
101 }
102
103 final int delta = requestNextIMM ? 1 : -1;
104 do {
105 cal.add(MONTH, delta);
106 } while (!isIMMMonth(cal));
107
108 moveToIMMDay(cal);
109
110 cal = handlePeriod(requestNextIMM, period, cal);
111
112 return cal;
113 }
114
115 private Calendar handlePeriod(final boolean requestNextIMM, final IMMPeriod period, final Calendar givenCal) {
116 Calendar cal = givenCal;
117 final int month = cal.get(MONTH);
118 switch (period) {
119 case BI_ANNUALY_JUN_DEC:
120 if (month == MARCH || month == SEPTEMBER) {
121
122 cal = getNextIMMDate(requestNextIMM, cal, period);
123 }
124 break;
125
126 case BI_ANNUALY_MAR_SEP:
127 if (month == JUNE || month == DECEMBER) {
128
129 cal = getNextIMMDate(requestNextIMM, cal, period);
130 }
131 break;
132
133 case ANNUALLY:
134
135 cal = getNextIMMDate(requestNextIMM, cal, QUARTERLY);
136
137 cal = getNextIMMDate(requestNextIMM, cal, QUARTERLY);
138
139 cal = getNextIMMDate(requestNextIMM, cal, QUARTERLY);
140
141 cal = getNextIMMDate(requestNextIMM, cal, QUARTERLY);
142 break;
143
144 case QUARTERLY:
145 default:
146 break;
147 }
148 return cal;
149 }
150
151
152
153
154
155
156
157
158
159 private static boolean isIMMMonth(final Calendar cal) {
160 final int month = cal.get(MONTH);
161
162 switch (month) {
163 case MARCH:
164 case JUNE:
165 case SEPTEMBER:
166 case DECEMBER:
167 return true;
168 default:
169 return false;
170 }
171 }
172
173
174
175
176
177
178 private static void moveToIMMDay(final Calendar cal) {
179 cal.set(DAY_OF_MONTH, 1);
180
181
182 final int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
183 if (dayOfWeek < WEDNESDAY) {
184 cal.add(DAY_OF_MONTH, WEDNESDAY - dayOfWeek);
185 } else if (dayOfWeek > WEDNESDAY) {
186 cal.add(DAY_OF_MONTH, WEDNESDAY + NUMBER_DAYS_IN_WEEK - dayOfWeek);
187 }
188
189
190 cal.add(DAY_OF_MONTH, NUMBER_DAYS_IN_WEEK * 2);
191 }
192
193 @Override
194 public boolean isIMMDate(final Calendar date) {
195
196 final Calendar cal = (Calendar) date.clone();
197 moveToIMMDay(cal);
198 return cal.equals(date);
199 }
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217