KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.schedules;
5
6 import java.util.Date JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.TimeZone JavaDoc;
10
11 /**
12  * A schedule context provides a context for the evaluation of
13  * a schedules next due interval.
14  */

15 public class ScheduleContext {
16
17     /** The date for the evaluation */
18     private final Date JavaDoc date;
19     
20     /** The time zone the schedule is to be evaluated in. */
21     private TimeZone JavaDoc timeZone;
22     
23     /** A data map which allows schedules which maintain state to use
24      * to maintain that state.
25      */

26     private final Map JavaDoc data;
27         
28     /**
29      * Constructor for a new context with a date to evaluate from, and using
30      * the default time zone.
31      *
32      * @param now The date to evaluate from.
33      */

34     public ScheduleContext(Date JavaDoc now) {
35         this(now, null);
36     }
37     
38     /**
39      * Constructor for a new context with a date to evaluate from, and a
40      * time zone to evaluate the schedule with.
41      *
42      * @param now The date to evaluate from.
43      * @param timeZone The time zone.
44      */

45     public ScheduleContext(Date JavaDoc now, TimeZone JavaDoc timeZone) {
46         this(now, timeZone, new HashMap JavaDoc());
47     }
48     
49     /**
50      * Constructor with a data map.
51      *
52      * @param now The date to evaluate from. A null date is allowed
53      * and means the schedule will never be due.
54      * @param timeZone The time zone.
55      * @param data The data map.
56      */

57     public ScheduleContext(Date JavaDoc now, TimeZone JavaDoc timeZone, Map JavaDoc data) {
58         if (timeZone == null) {
59             timeZone = TimeZone.getDefault();
60         }
61         
62         this.date = now;
63         this.timeZone = timeZone;
64         this.data = data;
65     }
66     
67     /**
68      * Get the date to evaluate the schedule with.
69      *
70      * @return The date to evaluate the schedule with. Never null.
71      */

72     public Date JavaDoc getDate() {
73         return date;
74     }
75         
76     /**
77      * Get the time zone to evaluate the schdule in.
78      *
79      * @return The time zone, Never null.
80      */

81     public TimeZone JavaDoc getTimeZone() {
82         return timeZone;
83     }
84     
85     /**
86      * Add data to the context data map.
87      *
88      * @param key The key.
89      * @param value The value.
90      */

91     public void putData(Object JavaDoc key, Object JavaDoc value) {
92         data.put(key, value);
93     }
94     
95     /**
96      * Get data back from the context data map.
97      *
98      * @param key The key.
99      * @return The value.
100      */

101     public Object JavaDoc getData(Object JavaDoc key) {
102         return data.get(key);
103     }
104     
105     /**
106      * Create a new context with the existing time zone and data map.
107      *
108      * @param date The new evaluate date.
109      * @return A new context.
110      */

111     public ScheduleContext spawn(Date JavaDoc date) {
112         ScheduleContext newContext = new ScheduleContext(date,
113                 this.timeZone, this.data);
114         return newContext;
115     }
116 }
117
Popular Tags