KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jobs > SequenceJob


1 package org.oddjob.jobs;
2
3 import java.io.Serializable JavaDoc;
4
5 import org.apache.log4j.Logger;
6
7
8 /**
9  * @oddjob.description Provide a sequence number which is
10  * incremented each time the job is executed.
11  *
12  * @oddjob.example
13  *
14  * <pre><code>
15  * &lt;sequential name="Sequence of Files&gt;
16  * &lt;sequence id="seqnum"/&gt;
17  * &lt;variables id="vars"&gt;
18  * &lt;format name="seqnum-formatted" number="${seqnum.current}" format="0000"/&gt;
19  * &lt;/variables&gt;
20  * &lt;exists file="balances${vars.seqnum-formatted}"/&gt;
21  * &lt;/sequential&gt;
22  * </code>
23  */

24
25 public class SequenceJob implements Runnable JavaDoc, Serializable JavaDoc {
26     private static final long serialVersionUID=20060109;
27
28     private static final Logger logger = Logger.getLogger(SequenceJob.class);
29     
30     /**
31      * @oddjob.property
32      * @oddjob.description The name of this job.
33      * @oddjob.required No.
34      */

35     private String JavaDoc name;
36     
37     /**
38      * @oddjob.property
39      * @oddjob.description The current sequence number.
40      * @oddjob.required Read only.
41      */

42     private Integer JavaDoc current;
43     
44     /**
45      * @oddjob.property
46      * @oddjob.description The current sequence number.
47      * @oddjob.required No, defaults to 0.
48      */

49     private int from;
50     
51     /**
52      * @oddjob.property
53      * @oddjob.description This can be any object which
54      * will be watched, and when it changes the sequence
55      * will be reset. This will most likely be a date.
56      * @oddjob.required. No.
57      */

58     private Object JavaDoc watch;
59     
60     /**
61      * Get the name.
62      *
63      * @return The name.
64      */

65     public String JavaDoc getName() {
66         return name;
67     }
68     
69     /**
70      * Set the name
71      *
72      * @param name The name.
73      */

74     public void setName(String JavaDoc name) {
75         this.name = name;
76     }
77     
78     /**
79      * Get the current sequence number.
80      *
81      * @return The current sequence number.
82      */

83     public Integer JavaDoc getCurrent() {
84         return current;
85     }
86
87     public void setFrom(int from) {
88         this.from = from;
89     }
90     
91     public int getFrom() {
92         return from;
93     }
94     
95     /*
96      * (non-Javadoc)
97      * @see java.lang.Runnable#run()
98      */

99     public void run() {
100         if (current == null) {
101             current = new Integer JavaDoc(from);
102         }
103         else {
104             current = new Integer JavaDoc(current.intValue() + 1);;
105         }
106         logger.debug("Sequence now " + current);
107     }
108     
109     
110     /**
111      * Set an object to watch.
112      *
113      * S@param reset The reset to set.
114      */

115     public void setWatch(Object JavaDoc watch) {
116         if (this.watch == null) {
117             if (watch == null) {
118                 return;
119             }
120             else {
121                 current = null;
122             }
123         }
124         else if (!this.watch.equals(watch)) {
125             current = null;
126         }
127         this.watch = watch;
128     }
129     
130     public Object JavaDoc getWatch() {
131         return watch;
132     }
133     
134     /*
135      * (non-Javadoc)
136      * @see java.lang.Object#toString()
137      */

138     public String JavaDoc toString() {
139         if (name == null) {
140             return "A Sequence Number";
141         }
142         return name;
143     }
144 }
145
Popular Tags