KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.schedules;
2
3 import java.text.ParseException JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import org.apache.log4j.Logger;
7
8 /**
9  * A base class for a Schedule which has a from and a to
10  * date.
11  *
12  * @author Rob Gordon
13  */

14
15 abstract public class ConstrainedSchedule extends AbstractSchedule {
16
17     private static final long serialVersionUID = 20050226;
18     
19     private static Logger logger = Logger.getLogger(ConstrainedSchedule.class);
20
21     /**
22      * @oddjob.property from
23      * @oddjob.description The from date/time.
24      * @oddjob.required No.
25      *
26      * @param from The from date.
27      */

28     abstract public void setFrom(String JavaDoc from);
29
30     /**
31      * Get the from date/time as a String.
32      *
33      * @return The from date.
34      */

35     abstract public String JavaDoc getFrom();
36     
37     /**
38      * @oddjob.property to
39      * @oddjob.description The to date/time.
40      * @oddjob.required No.
41      *
42      * @param to The to date.
43      *
44      */

45     abstract public void setTo(String JavaDoc to);
46
47     /**
48      * Get the to date/time as a String.
49      *
50      * @return The to date.
51      */

52     abstract public String JavaDoc getTo();
53     
54     /**
55      * For the given time, calculate when this schedule
56      * should start or should have started.
57      *
58      * @param now The date/time now.
59      * @return The date/time the scheudle is/was due.
60      */

61     abstract public Date JavaDoc getStartDate(ScheduleContext context);
62     
63     /**
64      * For the given time, calculate when this schedule
65      * interval should finish.
66      *
67      * @param now The date/time now.
68      * @return The date/time the schedule should finish.
69      */

70     abstract public Date JavaDoc getEndDate(ScheduleContext context);
71     
72     /**
73      * @oddjob.property on
74      * @oddjob.description The on date/time. This has the same effect as
75      * setting from and to to the same thing.
76      * @oddjob.required No.
77      *
78      * @param in The on text.
79      * @throws ParseException If the string isn't a date.
80      */

81     public void setOn(String JavaDoc on) {
82         setFrom(on);
83         setTo(on);
84     }
85
86     /*
87      * (non-Javadoc)
88      * @see org.treesched.Schedule#nextDue(java.util.Date)
89      */

90     public Interval nextDue(ScheduleContext context) {
91         Date JavaDoc now = context.getDate();
92         if (now == null) {
93             return null;
94         }
95
96         Interval withoutLimits = new Interval(
97                 getStartDate(context), getEndDate(context));
98         Interval nextInterval = withoutLimits.limit(getLimits());
99         logger.debug(this + ": in date is " + now +
100                 ", next interval is " + nextInterval);
101         if (nextInterval == null) {
102             return null;
103         }
104         Schedule child = getChildSchedule();
105         if (child == null) {
106             return nextInterval;
107         }
108         child.setLimits(nextInterval);
109         Interval childInterval = null;
110         // if now is before the start of the next interval
111
// pass the start of the next interval to the child.
112
if (now.compareTo(nextInterval.getFromDate()) < 0) {
113             childInterval = child.nextDue(
114                     context.spawn(nextInterval.getFromDate()));
115         }
116         else {
117             childInterval = child.nextDue(context);
118         }
119         // if the child interval is null we must be passed
120
// the end of the child for this interval so try the
121
// next.
122
if (childInterval == null) {
123             return nextDue(context.spawn(DateUtils.oneMillisAfter(
124                     nextInterval.getToDate())));
125         }
126         return childInterval;
127     }
128
129     /**
130      * Override toString.
131      *
132      * @return A description of the schedule.
133      */

134     public String JavaDoc toString() {
135         return this.getClass().getName() + " from " + getFrom() + " to " + getTo();
136     }
137 }
138
Popular Tags