KickJava   Java API By Example, From Geeks To Geeks.

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


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.ScheduleContext;
11
12 /**
13  * @oddjob.description This schedule counts the occurence's
14  * of it's nested schedule
15  * and returns the required occurence.
16  *
17  * @oddjob.example
18  *
19  * Second tuesday of the month.
20  *
21  * <pre>
22  * &lt;dayofmonth from="1" to="0" &gt;
23  * &lt;occurrence occurrence="2" &gt;
24  * &lt;dayofweek on="tue" /&gt;
25  * &lt;/occurrence &gt;
26  * &lt;/dayofmonth&gt;
27  * </pre>
28  *
29  * @author Rob Gordon
30  */

31
32 final public class OccurrenceSchedule extends AbstractSchedule implements Serializable JavaDoc {
33
34     private static final long serialVersionUID = 20050226;
35     
36     private final static Logger logger = Logger.getLogger(OccurrenceSchedule.class);
37     
38     /**
39      * @oddjob.property
40      * @oddjob.description The number of the required occurrence.
41      * @oddjob.required Yes.
42      */

43     private int occurrence;
44     
45     /**
46      * Set the number of the occurrence for this schedule.
47      *
48      * @param occurrence The occurence.
49      */

50     public void setOccurrence(String JavaDoc occurrence) {
51         this.occurrence = Integer.parseInt(occurrence);
52     }
53
54     /**
55      * Return the number of the occurrence for this schedule.
56      *
57      * @return The occurrence.
58      */

59     
60     public String JavaDoc getOccurrence() {
61         return Integer.toString(occurrence);
62     }
63     
64     /**
65      * Return the next due interval which is the given occurrence of
66      * it's child schedules.
67      */

68     
69     public Interval nextDue(ScheduleContext context) {
70         Date JavaDoc now = context.getDate();
71         if (getChildSchedule() == null) {
72             throw new IllegalStateException JavaDoc("Occurence must have a child schedule.");
73         }
74
75         logger.debug(this + ": in Date is " + now);
76         
77         Date JavaDoc use = now;
78         // start from the beginning interval of our parent, if there is one.
79
if (getLimits() != null) {
80             use = getLimits().getFromDate();
81         }
82         Interval candidate = null;
83         
84         for (int i = 0; i < occurrence && use != null; ++i) {
85             logger.debug(this + ": use interval is " + use);
86             getChildSchedule().setLimits(getLimits());
87             candidate = getChildSchedule().nextDue(context.spawn(use));
88             
89             if (candidate != null) {
90                 use = DateUtils.oneMillisAfter(candidate.getToDate());
91             }
92             else {
93                 // break the cycle
94
use = null;
95             }
96         }
97         return candidate;
98     }
99
100     /**
101      * Override toString.
102      */

103     
104     public String JavaDoc toString() {
105         
106         return "Occurence Schedule, occurences " + getOccurrence();
107     }
108 }
109
Popular Tags