KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.schedules;
2
3 import java.util.Date JavaDoc;
4
5 import org.apache.log4j.Logger;
6
7 /**
8  * This is a base class for schedule which repeat at intervals.
9  *
10  * @author Rob Gordon
11  */

12
13 abstract public class RegularSchedule extends AbstractSchedule {
14
15     private static final long serialVersionUID = 20050226;
16     
17     private static Logger logger = Logger.getLogger(RegularSchedule.class);
18     
19     /**
20      * Given a date, return the date which is interval of this
21      * RegularScheudle later.
22      *
23      * @param from The date from which this schedule runs.
24      * @return The interval later.
25      */

26     abstract public Date JavaDoc next(Date JavaDoc from);
27     
28     /*
29      * (non-Javadoc)
30      * @see org.treesched.Schedule#nextDue(java.util.Date)
31      */

32     public Interval nextDue(ScheduleContext context) {
33         Date JavaDoc now = context.getDate();
34         if (now == null) {
35             return null;
36         }
37         Interval nextInterval = null;
38         if (getLimits() == null) {
39             // no limits so just return an interval later.
40
nextInterval = new Interval(now, DateUtils.oneMillisBefore(next(now)));
41         }
42         else {
43             // if there is limits then calculate the interval
44
// that might be due from the beginning.
45
Date JavaDoc start = getLimits().getFromDate();
46             Date JavaDoc end = getLimits().getToDate();
47             // if before start - take the first interval.
48
if (now.compareTo(start) < 0) {
49                 nextInterval = new Interval(start,
50                         DateUtils.oneMillisBefore(next(start)));
51             }
52             // if the given date is greater than the to time, the schedule is never due.
53
else if (now.compareTo(end) > 0) {
54                 return null;
55             }
56             else {
57                 // work up in incrememnts of interval to the one
58
// that's now due.
59
while (true) {
60                     Date JavaDoc next = next(start);
61                     if (now.compareTo(next) < 0) {
62                         nextInterval = new Interval(start, DateUtils.oneMillisBefore(next));
63                         break;
64                     }
65                     start = next;
66                 }
67             }
68         }
69         logger.debug(this + ": in date is " + now
70                     + ", next Interval is " + nextInterval);
71
72         if (nextInterval == null) {
73             return null;
74         }
75         Schedule child = getChildSchedule();
76         if (child == null) {
77             return nextInterval;
78         }
79         child.setLimits(nextInterval);
80         Interval childInterval = child.nextDue(context);
81         // if the child interval is null we must be passed
82
// the end of the child for this interval so try the
83
// next.
84
if (childInterval == null) {
85             return nextDue(context.spawn(DateUtils.oneMillisAfter(
86                     nextInterval.getToDate())));
87         }
88         return childInterval;
89     }
90     
91     /**
92      * Override toString to be more meaningful.
93      *
94      * @return a Description of this schedule.
95      */

96     public String JavaDoc toString() {
97         return this.getClass().getName()
98         + ", interval=" + next(new Date JavaDoc(0)).getTime()
99         + ", limits=" + getLimits();
100     }
101 }
102
Popular Tags