Clover Coverage Report - KS LUM 1.2-M4-SNAPSHOT (Aggregated)
Coverage timestamp: Wed Jul 20 2011 13:25:47 EDT
../../../../../../../img/srcFileCovDistChart0.png 43% of files have more coverage
225   367   116   7.5
46   308   0.52   30
30     3.87  
1    
 
  GregorianCalendar       Line # 30 225 0% 116 301 0% 0.0
 
No Tests
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
14    */
15   
16    package org.kuali.student.lum.common.client.widgets;
17   
18    import java.util.Date;
19   
20    /**
21    * <dl>
22    * <dt><b>Title: </b><dd>GregorianCalendar</dd>
23    * <p>
24    * <dt><b>Description: </b><dd>java.util.GregorianCalendar replacment</dd>
25    * </dl>
26    * @author <a href="mailto:andre.freller@gmail.com">Andre Freller</a>
27    * @version $Revision: 0.1 $
28    */
29    ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// /////////
 
30    public class GregorianCalendar extends Calendar
31    {
32    public static final int BC = 0;
33    public static final int AD = 1;
34   
 
35  0 toggle public GregorianCalendar() {
36  0 super();
37    }
 
38  0 toggle public GregorianCalendar(Date date) {
39  0 setTime(date);
40    }
 
41  0 toggle public GregorianCalendar(int year, int month, int date) {
42  0 set(year, month, date);
43    }
 
44  0 toggle public GregorianCalendar(int year, int month, int date, int hour) {
45  0 set(year, month, date, hour);
46    }
 
47  0 toggle public GregorianCalendar(int year, int month, int date, int hour, int minute) {
48  0 set(year, month, date, hour, minute);
49    }
 
50  0 toggle public GregorianCalendar(int year, int month, int date, int hour, int minute, int second) {
51  0 set(year, month, date, hour, minute, second);
52    }
53   
54    protected static int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31 ,30, 31, 30, 31};
 
55  0 toggle public static int getMaxDaysInMonth(int year, int month) {
56  0 if (month == Calendar.FEBRUARY && isLeapYear(year))
57  0 return daysInMonth[month] + 1;
58    else
59  0 return daysInMonth[month];
60    }
 
61  0 toggle public int getMaxDaysInMonth() {
62  0 return getMaxDaysInMonth(this.year, this.month);
63    }
64   
 
65  0 toggle public static int getNumOfWeeksInMonth(int year, int month, int firstDayOfWeek) {
66  0 int day1 = getWeekDay(year, month, 1);
67  0 int maxDays = getMaxDaysInMonth(year, month);
68  0 if (month == Calendar.FEBRUARY && !isLeapYear(year) && day1 == firstDayOfWeek)
69  0 return 4;
70  0 if (maxDays == 30 && (day1 > firstDayOfWeek + 5 || day1 < firstDayOfWeek))
71  0 return 6;
72  0 if (maxDays == 31 && (day1 > firstDayOfWeek + 4 || day1 < firstDayOfWeek))
73  0 return 6;
74  0 return 5;
75    }
 
76  0 toggle public int getNumOfWeeksInMonth() {
77  0 return getNumOfWeeksInMonth(this.year, this.month, this.firstWeekDayOfWeek);
78    }
79   
 
80  0 toggle public static int getWeekDay(int year, int month, int dayOfMonth) {
81  0 int weekDay = year - 1900;
82  0 weekDay += weekDay/4;
83  0 weekDay %= 7;
84  0 if (month <= Calendar.FEBRUARY && isLeapYear(weekDay))
85  0 weekDay -= 1;
86  0 weekDay += dayOfMonth;
87  0 weekDay %= 7;
88  0 if (month == Calendar.MAY)
89  0 weekDay += 1;
90  0 else if (month == Calendar.AUGUST)
91  0 weekDay += 2;
92  0 else if (month == Calendar.FEBRUARY || month == Calendar.MARCH || month == Calendar.NOVEMBER)
93  0 weekDay += 3;
94  0 else if (month == Calendar.JUNE)
95  0 weekDay += 4;
96  0 else if (month == Calendar.SEPTEMBER || month == Calendar.DECEMBER)
97  0 weekDay += 5;
98  0 else if (month == Calendar.APRIL || month == Calendar.JULY)
99  0 weekDay += 6;
100  0 weekDay %= 7;
101   
102  0 return Calendar.SUNDAY + weekDay;
103    }
 
104  0 toggle public int getWeekDay() {
105  0 return getWeekDay(this.year, this.month, this.dayOfMonth);
106    }
 
107  0 toggle public int getFirstWeekDayOfMonth() {
108  0 return getWeekDay(this.year, this.month, 1);
109    }
110   
111    // Gets the minimum value for the given time field.
 
112  0 toggle public int getMinimum(int fieldCode) {
113  0 switch (fieldCode) {
114  0 case Calendar.MILLISECOND:
115  0 return 0;
116  0 case Calendar.SECOND:
117  0 return 0;
118  0 case Calendar.MINUTE:
119  0 return 0;
120  0 case Calendar.HOUR:
121  0 return 0;
122  0 case Calendar.HOUR_OF_DAY:
123  0 return 0;
124  0 case Calendar.DATE:
125  0 return 1;
126  0 case Calendar.MONTH:
127  0 return Calendar.JANUARY;
128  0 default:
129  0 return -1;
130    }
131    }
132    // Return the minimum value that this field could have, given the current date.
 
133  0 toggle public int getActualMinimum(int fieldCode) {
134  0 return getMinimum(fieldCode);
135    }
136    // Gets the highest minimum value for the given field if varies.
 
137  0 toggle public int getGreatestMinimum(int fieldCode) {
138  0 return getMinimum(fieldCode);
139    }
140   
141    // Gets the maximum value for the given time field.
 
142  0 toggle public int getMaximum(int fieldCode) {
143  0 switch (fieldCode) {
144  0 case Calendar.MILLISECOND:
145  0 return 999;
146  0 case Calendar.SECOND:
147  0 return 59;
148  0 case Calendar.MINUTE:
149  0 return 59;
150  0 case Calendar.HOUR:
151  0 return 11;
152  0 case Calendar.HOUR_OF_DAY:
153  0 return 23;
154  0 case Calendar.DATE:
155  0 return 31;
156  0 case Calendar.MONTH:
157  0 return Calendar.DECEMBER;
158  0 default:
159  0 return -1;
160    }
161    }
162    // Return the maximum value that this field could have, given the current date.
 
163  0 toggle public int getActualMaximum(int fieldCode) {
164  0 switch (fieldCode) {
165  0 case Calendar.MILLISECOND:
166  0 return 999;
167  0 case Calendar.SECOND:
168  0 return 59;
169  0 case Calendar.MINUTE:
170  0 return 59;
171  0 case Calendar.HOUR:
172  0 return 11;
173  0 case Calendar.HOUR_OF_DAY:
174  0 return 23;
175  0 case Calendar.DATE:
176  0 return getMaxDaysInMonth();
177  0 case Calendar.MONTH:
178  0 return Calendar.DECEMBER;
179  0 default:
180  0 return -1;
181    }
182    }
183    // Gets the lowest maximum value for the given field if varies.
 
184  0 toggle public int getLeastMaximum(int fieldCode) {
185  0 switch (fieldCode) {
186  0 case Calendar.MILLISECOND:
187  0 return 999;
188  0 case Calendar.SECOND:
189  0 return 59;
190  0 case Calendar.MINUTE:
191  0 return 59;
192  0 case Calendar.HOUR:
193  0 return 11;
194  0 case Calendar.HOUR_OF_DAY:
195  0 return 23;
196  0 case Calendar.DATE:
197  0 return 28;
198  0 case Calendar.MONTH:
199  0 return Calendar.DECEMBER;
200  0 default:
201  0 return -1;
202    }
203    }
204   
205   
 
206  0 toggle public void set(int fieldCode, int value) {
207  0 switch (fieldCode) {
208  0 case Calendar.SECOND:
209  0 this.second = value;
210  0 break;
211  0 case Calendar.MINUTE:
212  0 this.minute = value;
213  0 break;
214  0 case Calendar.HOUR:
215  0 this.hour = value;
216  0 break;
217  0 case Calendar.HOUR_OF_DAY:
218  0 if (value <= 12) {
219  0 this.hour = value;
220  0 this.amPm = Calendar.AM;
221    } else {
222  0 this.hour = value - 12;
223  0 this.amPm = Calendar.PM;
224    }
225  0 break;
226  0 case Calendar.DATE:
227  0 this.dayOfMonth = value;
228  0 break;
229  0 case Calendar.MONTH:
230  0 this.month = value;
231  0 if (this.dayOfMonth > getMaxDaysInMonth())
232  0 this.dayOfMonth = getMaxDaysInMonth();
233  0 break;
234  0 case Calendar.YEAR:
235  0 this.year = value;
236  0 break;
237  0 case Calendar.AM_PM:
238  0 this.amPm = value;
239  0 break;
240    }
241  0 computeTime();
242    }
243   
244   
245    // Date Arithmetic function.
 
246  0 toggle public void add(int fieldCode, int amount) {
247  0 switch (fieldCode) {
248  0 case Calendar.DATE:
249  0 amount *= 24;
250  0 case Calendar.HOUR:
251  0 amount *= 60;
252  0 case Calendar.MINUTE:
253  0 amount *= 60;
254  0 case Calendar.SECOND:
255  0 amount *= 1000;
256  0 case Calendar.MILLISECOND:
257  0 this.date.setTime(this.date.getTime() + amount);
258  0 computeFields();
259  0 break;
260  0 case Calendar.MONTH:
261  0 this.year += (this.month + amount) / 12;
262  0 this.month = (this.month + amount) % 12;
263  0 if (this.month < 0) {
264  0 this.year -= 1;
265  0 this.month += 12;
266    }
267  0 if (this.dayOfMonth > getMaxDaysInMonth())
268  0 this.dayOfMonth = getMaxDaysInMonth();
269  0 computeTime();
270  0 break;
271  0 case Calendar.YEAR:
272  0 this.year += amount;
273  0 computeTime();
274  0 break;
275    }
276    }
277   
278    // Time Field Rolling function.
 
279  0 toggle public void roll(int fieldCode, int amount) {
280  0 switch (fieldCode) {
281  0 case Calendar.SECOND:
282  0 this.second = (this.second + amount) % 60;
283  0 if (this.second < 0)
284  0 this.second += 60;
285  0 break;
286  0 case Calendar.MINUTE:
287  0 this.minute = (this.minute + amount) % 60;
288  0 if (this.minute < 0)
289  0 this.minute += 60;
290  0 break;
291  0 case Calendar.HOUR:
292  0 this.hour = (this.hour + amount) % 24;
293  0 if (this.hour < 0)
294  0 this.hour += 24;
295  0 break;
296  0 case Calendar.DATE: {
297  0 this.dayOfMonth = (this.dayOfMonth + amount) % getActualMaximum(Calendar.DATE);
298  0 if (this.dayOfMonth < 0)
299  0 this.dayOfMonth += getActualMaximum(Calendar.DATE);
300  0 break;
301    }
302  0 case Calendar.MONTH: {
303  0 this.month = (this.month + amount) % 12;
304  0 if (this.month < 0)
305  0 this.month += 12;
306  0 if (this.dayOfMonth > getActualMaximum(Calendar.DATE))
307  0 this.dayOfMonth = getActualMaximum(Calendar.DATE);
308  0 break;
309    }
310  0 case Calendar.YEAR:
311  0 this.year += amount;
312  0 break;
313    }
314  0 computeTime();
315    }
316   
317   
318    // Compares the time field records.
 
319  0 toggle public boolean before(GregorianCalendar when) {
320  0 return this.date.before(when.getTime());
321    }
 
322  0 toggle public boolean after(GregorianCalendar when) {
323  0 return this.date.after(when.getTime());
324    }
 
325  0 toggle public boolean equals(GregorianCalendar obj) {
326  0 return this.date.equals(obj.getTime());
327    }
328   
 
329  0 toggle public boolean isLeapYear() {
330  0 return isLeapYear(this.year);
331    }
 
332  0 toggle public static boolean isLeapYear(int year) {
333  0 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
334  0 return true;
335  0 return false;
336    }
337   
338   
339    // Converts the current millisecond time value time to field values in fields[].
 
340  0 toggle protected void computeFields() {
341  0 this.second = this.date.getSeconds();
342  0 this.minute = this.date.getMinutes();
343  0 this.hour = this.date.getHours();
344  0 if (this.hour < 12) {
345  0 this.amPm = Calendar.AM;
346    } else {
347  0 this.hour -= 12;
348  0 this.amPm = Calendar.PM;
349    }
350  0 this.dayOfMonth = this.date.getDate();
351  0 this.month = this.date.getMonth();
352  0 this.year = this.date.getYear() + 1900;
353   
354    }
355    // Converts the current field values in fields[] to the millisecond time value time.
 
356  0 toggle protected void computeTime() {
357  0 this.date = new Date(this.year - 1900, this.month, this.dayOfMonth, this.hour, this.minute, this.second);
358    }
359   
360    // Overrides Cloneable
 
361  0 toggle public Object clone() {
362  0 GregorianCalendar newCal = new GregorianCalendar();
363  0 newCal.setTime(this.date);
364  0 return newCal;
365    }
366   
367    } // end of class GregorianCalendar