1 /***
2 * Created by IntelliJ IDEA.
3 * User: Lennart
4 * Date: 22-nov-2003
5 * Time: 16:17:51
6 */
7 package comics.core;
8
9 import comics.interfaces.DayRoller;
10
11 import java.util.Calendar;
12 import java.util.Vector;
13 import java.util.Date;
14 import java.util.GregorianCalendar;
15 import java.text.SimpleDateFormat;
16
17 /*
18 * CVS information:
19 *
20 * $Revision: 1.1 $
21 * $Date: 2003/12/04 14:29:44 $
22 */
23
24 /***
25 * This class provides a DayRoller implementation, based on the GregorianCalendar by default.
26 * The defaults for the starting date are the current date and for the applicable days to be all days.
27 *
28 * @author Lennart Martens
29 */
30 public class GregorianDayRoller implements DayRoller {
31
32 /***
33 * The heart of the implementation. It defaults to GregorianCalendar.
34 */
35 private Calendar iCalendar = null;
36
37 /***
38 * The Vector with Integers corresponding to the applicable days. This defaults to 'null', which is
39 * considered as all days are applicable.
40 */
41 private Vector iApplicableDays = null;
42
43 /***
44 * The default constructor creates a GregorianDayRoller using a GregorianCalendar for the
45 * Calendar implementation, the current date as the starting date and sets all days as applicable.
46 */
47 public GregorianDayRoller() {
48 this(new Date(), null);
49 }
50
51 /***
52 * This constructor allows the caller to set the GregorianDayRoller up in full.
53 *
54 * @param aStartDate Calendar with a Calendar implementation set at the desired start date.
55 * @param aApplicableDays int[] with the applicable days; the elements of the array should
56 * all be one of the day constants defined on the Calendar interface.
57 * This parameter can be 'null' if all days are applicable.
58 */
59 public GregorianDayRoller(Calendar aStartDate, int[] aApplicableDays) {
60 this.setStartDate(aStartDate);
61 this.setApplicableDays(aApplicableDays);
62 }
63
64 /***
65 * This constructor allows the caller to set the GregorianDayRoller up in full.
66 *
67 * @param aStartDate Date with the desired start date. The Calendar used here by default will be the GregorianCalendar.
68 * @param aApplicableDays int[] with the applicable days; the elements of the array should
69 * all be one of the day constants defined on the Calendar interface.
70 * This parameter can be 'null' if all days are applicable.
71 */
72 public GregorianDayRoller(Date aStartDate, int[] aApplicableDays) {
73 if(aStartDate == null) {
74 aStartDate = new Date();
75 }
76 this.setStartDate(aStartDate);
77 this.setApplicableDays(aApplicableDays);
78 }
79
80 /***
81 * This method returns the current Calendar instance rolled forward by the necessary amount of days
82 * to match the nearest future applicable day.
83 *
84 * @return Calendar with the nearest future applicable day
85 */
86 public Calendar getNextDay() {
87 // First set iCalendar to correct date.
88 rollUntillApplicable(1);
89 // Now return a reference(!) to iCalendar.
90 return this.iCalendar;
91 }
92
93 /***
94 * This method returns the current Calendar instance rolled back by the necessary amount of days
95 * to match the nearest previous applicable day.
96 *
97 * @return Calendar with the nearest previous applicable day
98 */
99 public Calendar getPreviousDay() {
100 // First set iCalendar to correct date.
101 rollUntillApplicable(-1);
102 // Now return a reference(!) to iCalendar.
103 return this.iCalendar;
104 }
105
106 /***
107 * This method allows the caller to specify which days should be reported.
108 *
109 * @param aDays int[] with the integer codes for days as specified by the constants
110 * in the Calendar interface. Can be 'null' if all days are applicable.
111 */
112 public void setApplicableDays(int[] aDays) {
113 if(aDays != null) {
114 Vector temp = new Vector(aDays.length);
115 for(int i = 0; i < aDays.length; i++) {
116 int lApplicableDay = aDays[i];
117 if(!(lApplicableDay == Calendar.MONDAY || lApplicableDay == Calendar.TUESDAY ||
118 lApplicableDay == Calendar.WEDNESDAY || lApplicableDay == Calendar.THURSDAY ||
119 lApplicableDay == Calendar.FRIDAY || lApplicableDay == Calendar.SATURDAY ||
120 lApplicableDay == Calendar.SUNDAY)) {
121 throw new IllegalArgumentException("The applicable day you passed (" + lApplicableDay + ") is not a day constant defined on the Calendar interface!");
122 }
123 temp.add(new Integer(lApplicableDay));
124 }
125 iApplicableDays = temp;
126 } else {
127 iApplicableDays = null;
128 }
129 }
130
131 /***
132 * This method allows the setting of a specific starting date via a specified Calendar.
133 * Note that this Calendar instance will subsequently be used for rolling through the days.
134 *
135 * @param aStartDate Calendar with the desired start date.
136 */
137 public void setStartDate(Calendar aStartDate) {
138 this.iCalendar = aStartDate;
139 }
140
141 /***
142 * This method allows the setting of a specific starting date via a specified Date.
143 * Note that a GregorianCalendar instance will be created for rolling through the days.
144 *
145 * @param aStartDate Calendar with the desired start date.
146 */
147 public void setStartDate(Date aStartDate) {
148 GregorianCalendar gc = new GregorianCalendar();
149 gc.setTime(aStartDate);
150 this.iCalendar = gc;
151 }
152
153 /***
154 * This method returns the current day of month (1-31)
155 *
156 * @return int with the current day of the month (1-31)
157 */
158 public int getCurrentDayOfMonth() {
159 return iCalendar.get(Calendar.DAY_OF_MONTH);
160 }
161
162 /***
163 * This method returns the current month (1-12)
164 *
165 * @return int with the current month (1-12)
166 */
167 public int getCurrentMonth() {
168 return iCalendar.get(Calendar.MONTH) + 1;
169 }
170
171 /***
172 * This method returns the current year (4-digit notation, eg. 2003)
173 *
174 * @return int with the current year (4-digit notation, eg. 2003)
175 */
176 public int getCurrentYear() {
177 return iCalendar.get(Calendar.YEAR);
178 }
179
180 /***
181 * This method returns the current date, or the first earlier applicable day.
182 *
183 * @return Calendar with the current date, or the first previous applicable day
184 */
185 public Calendar getCurrentDate() {
186 // See if we should check for applicable days.
187 if(iApplicableDays != null) {
188 // See if the current date is applicable.
189 if(!iApplicableDays.contains(new Integer(this.iCalendar.get(Calendar.DAY_OF_WEEK)))) {
190 // Roll back until we find the first previous applicable day.
191 this.rollUntillApplicable(-1);
192 }
193 }
194 return this.iCalendar;
195 }
196
197 /***
198 * This method returns the formatted current date, or the first earlier applicable day.
199 * Formatting will be applied according to the specified String using a SimpleDateFormatter.
200 *
201 * @return String with the formatted current date, or the first previous applicable day.
202 */
203 public String getFormattedCurrentDate(String aFormatter) {
204 this.getCurrentDate();
205 SimpleDateFormat sdf = new SimpleDateFormat(aFormatter);
206 return sdf.format(iCalendar.getTime());
207 }
208
209 /***
210 * This method rolls the calendar in iCalendar over the specified interval until an
211 * applicable day is found.
212 *
213 * @param aNumberOfDays int with the interval of days to roll (negative number to roll back).
214 */
215 private void rollUntillApplicable(int aNumberOfDays) {
216 boolean satisfied = false;
217 // Keep this up until we find an applicable day.
218 while(!satisfied) {
219 // Roll over the specified interval.
220 this.roll(aNumberOfDays);
221 // If the applicable days Vector is 'null', all days are applicable.
222 // Thus job done.
223 if(iApplicableDays == null) {
224 satisfied = true;
225 } else {
226 // See if this day is applicable.
227 Integer day = new Integer(iCalendar.get(Calendar.DAY_OF_WEEK));
228 if(iApplicableDays.contains(day)) {
229 satisfied = true;
230 }
231 }
232 }
233 }
234
235 /***
236 * This method rolls the calendar in iCalendar for the specified number of days.
237 *
238 * @param aNumberOfDays int with the umber of days to roll (negative number to roll back).
239 */
240 private void roll(int aNumberOfDays) {
241 this.iCalendar.add(Calendar.DATE, aNumberOfDays);
242 }
243 }
This page was automatically generated by Maven