KickJava   Java API By Example, From Geeks To Geeks.

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


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.DateUtils;
8 import org.oddjob.schedules.Interval;
9 import org.oddjob.schedules.Schedule;
10 import org.oddjob.schedules.ScheduleContext;
11
12
13
14 /**
15  * @oddjob.description This schedule allows a normal schedule
16  * to be broken by the results of another
17  * schedule. This might be a list of bank holidays, or time of day, or any other
18  * schedule.
19  * <p>
20  * This schedule works by moving the schedule forward if the start time of the
21  * next interval falls within the next interval defined by the break. In the
22  * example below for a time of 12:00 on 24-dec-04 the logic is as follows:
23  * <ul>
24  * <li>The schedule is next due at 10:00 on the 25-dec-04.</li>
25  * <li>This schedule is within the break, move the schedule on.</li>
26  * <li>The schedule is next due at 10:00 on the 26-dec-04.</li>
27  * <li>This schedule is within the break, move the schedule on.</li>
28  * <li>The schedule is next due at 10:00 on the 27-dec-04.</li>
29  * <li>This schedule is outside the break, use this result.</li>
30  * </ul>
31  *
32  * @oddjob.example
33  *
34  * <pre>
35  * &lt;broken&gt;
36  * &lt;schedule&gt;
37  * &lt;time on="10:00" /&gt;
38  * &lt;/schedule&gt;
39  * &lt;breaks&gt;
40  * &lt;date from="25-dec-04" to="26-dec-04" /&gt;
41  * &lt;/breaks&gt;
42  * &lt;/broken&gt;
43  *
44  * @author Rob Gordon
45  */

46
47 public class BrokenSchedule implements Serializable JavaDoc, Schedule {
48
49     private static final long serialVersionUID = 20050226;
50     
51     private static final Logger logger = Logger.getLogger(BrokenSchedule.class);
52     
53     /** The schedule. */
54     private Schedule schedule;
55     
56     /** The breaks. */
57     private Schedule breaks;
58
59     /** The limits. */
60     private Interval limits;
61
62     /*
63      * (non-Javadoc)
64      * @see org.treesched.Schedule#setLimits(org.treesched.Interval)
65      */

66     public void setLimits(Interval limits) {
67         this.limits = limits;
68     }
69     
70     /**
71      * Get any limits set for this schedule.
72      *
73      * @return The limits, may be null.
74      */

75     public Interval getLimits() {
76         return limits;
77     }
78     
79     /**
80      * Set the schedule to break up.
81      *
82      * @param schedule The schedule to break up.
83      */

84     public void setSchedule(Schedule schedule) {
85         this.schedule = schedule;
86     }
87
88     /**
89      * Get the schedule to break up.
90      *
91      * @return The schedule to break up.
92      */

93     public Schedule getSchedule() {
94         return this.schedule;
95     }
96         
97     /**
98      * Set the breaks which will break up the schedule.
99      *
100      * @param breaks The breaks schedule.
101      */

102     public void setBreaks(Schedule breaks) {
103         this.breaks = breaks;
104     }
105     
106     /**
107      * Get the breaks which will break up the schedule.
108      *
109      * @return The break Schedule.
110      */

111     
112     public Schedule getBreaks() {
113         return this.breaks;
114     }
115     
116     /**
117      * Implement the schedule.
118      */

119     public Interval nextDue(ScheduleContext context) {
120         Date JavaDoc now = context.getDate();
121
122         logger.debug(this + ": in interval is " + now);
123
124         // sanity checks
125
if (schedule == null) {
126             return null;
127         }
128         schedule.setLimits(getLimits());
129         if (breaks == null) {
130             return schedule.nextDue(context);
131         }
132
133         Date JavaDoc use = now;
134
135         // loop until we get a valid interval
136
while (true) {
137             if (use == null) {
138                 return null;
139             }
140
141             Interval next = schedule.nextDue(context.spawn(use));
142             // if the next schedule is never due return.
143
if (next == null) {
144                 return null;
145             }
146
147             // find the first exclusion interval
148
Interval exclude = breaks.nextDue(context.spawn(next.getFromDate()));
149                 
150             if (exclude == null) {
151                 return next;
152             }
153             // if this interval is before the break
154
if (next.isBefore(exclude)) {
155                 return next;
156             }
157             
158             // if we got here the last interval is blocked by an exclude so move
159
// the interval on.
160
use = DateUtils.oneMillisAfter(next.getToDate());
161         }
162     }
163
164     /**
165      * Provide a simple string description.
166      */

167     public String JavaDoc toString() {
168         return "Broken Schedule";
169     }
170 }
171
Popular Tags