KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > schedules > schedules > LastSchedule


1 package org.oddjob.schedules.schedules;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import org.apache.log4j.Logger;
7 import org.oddjob.schedules.AbstractSchedule;
8 import org.oddjob.schedules.DateUtils;
9 import org.oddjob.schedules.Interval;
10 import org.oddjob.schedules.Schedule;
11 import org.oddjob.schedules.ScheduleContext;
12
13 /**
14  * @oddjob.description This schedule will return it's last due nested
15  * schedule within the given parent interval.
16  *
17  * @oddjob.example
18  *
19  * Last tuesday or wednesday of the month, whichever is last.
20  *
21  * <pre>
22  * &lt;dayofmonth from="-6" to="0" &gt;
23  * &lt;last&gt;
24  * &lt;dayofweek on="tue" /&gt;
25  * &lt;dayofweek on="wed" /&gt;
26  * &lt;/last&gt;
27  * &lt;/dayofmonth&gt;
28  * </pre>
29   *
30  * @author Rob Gordon
31 */

32
33 final public class LastSchedule extends AbstractSchedule implements Serializable JavaDoc {
34
35     private static final long serialVersionUID = 20050226;
36     
37     private static final Logger logger = Logger.getLogger(LastSchedule.class);
38     
39     /**
40      * Calculate the next due interval within the given interval.
41      */

42     public Interval nextDue(ScheduleContext context) {
43         Date JavaDoc now = context.getDate();
44         if (now == null) {
45             return null;
46         }
47         if (getChildSchedule() == null) {
48             throw new IllegalStateException JavaDoc("Last must have a child schedule.");
49         }
50         
51         logger.debug(this + ": in date is " + now);
52         
53         Interval last = null;
54         Date JavaDoc use = now;
55         
56         Schedule child = getChildSchedule();
57         child.setLimits(getLimits());
58         while(true) {
59             logger.debug(this + ": use date is " + use);
60             Interval candidate = child.nextDue(context.spawn(use));
61             if (candidate == null) {
62                 break;
63             }
64             last = candidate;
65             use = DateUtils.oneMillisAfter(candidate.getToDate());
66         }
67         
68         return last;
69     }
70
71     /**
72      * Override toString.
73      */

74     
75     public String JavaDoc toString() {
76         return "Last Schedule";
77     }
78
79 }
80
Popular Tags