KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > xml > util > Duration


1 package org.jbpm.bpel.xml.util;
2
3 import java.util.Calendar JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.GregorianCalendar JavaDoc;
6 import java.util.regex.Matcher JavaDoc;
7 import java.util.regex.Pattern JavaDoc;
8
9 /**
10  * @author Alejandro Guízar
11  * @version $Revision: 1.1 $ $Date: 2005/06/23 02:22:46 $
12  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#duration">
13  * XML Schema Part 2: Datatypes &sect;3.2.6</a>
14  */

15 public class Duration {
16   
17   private short year;
18   private short month;
19   private short day;
20   private short hour;
21   private short minute;
22   private short second;
23   private short millis;
24   
25   private boolean negative;
26   
27   private static final Pattern JavaDoc durationPattern = Pattern.compile(
28       "(-)?" + // negative sign
29
"P" + // designator
30
"(?:(\\p{Digit}+)Y)?" + // years
31
"(?:(\\p{Digit}+)M)?" + // months
32
"(?:(\\p{Digit}+)D)?" + // days
33
"(?:T" + // begin-time
34
"(?:(\\p{Digit}+)H)?" + // hours
35
"(?:(\\p{Digit}+)M)?" + // minutes
36
"(?:" + // begin-seconds
37
"(\\p{Digit}+)" + // whole seconds
38
"(?:\\." + // begin-fractional seconds
39
"(\\p{Digit}{1,3})" + // milliseconds
40
"\\p{Digit}*" + // duration below milliseconds
41
")?" + // end-fractional seconds
42
"S)?" + // end-seconds
43
")?"); // end-time
44

45   public Duration() {
46   }
47   
48   public Duration(int year, int month, int day,
49       int hour, int minute, int second, int millis) {
50     setYear((short) year);
51     setMonth((short) month);
52     setDay((short) day);
53     setHour((short) hour);
54     setMinute((short) minute);
55     setSecond((short) second);
56     setMillis((short) millis);
57   }
58   
59   public boolean isNegative() {
60     return negative;
61   }
62   
63   public void setNegative(boolean negative) {
64     this.negative = negative;
65   }
66   
67   public short getYear() {
68     return year;
69   }
70   
71   public void setYear(short year) {
72     this.year = year;
73   }
74   
75   public short getMonth() {
76     return month;
77   }
78   
79   public void setMonth(short month) {
80     this.month = month;
81   }
82   
83   public short getDay() {
84     return day;
85   }
86   
87   public void setDay(short day) {
88     this.day = day;
89   }
90   
91   public short getHour() {
92     return hour;
93   }
94   
95   public void setHour(short hour) {
96     this.hour = hour;
97   }
98
99   public short getMinute() {
100     return minute;
101   }
102   
103   public void setMinute(short minute) {
104     this.minute = minute;
105   }
106   
107   public short getSecond() {
108     return second;
109   }
110   
111   public void setSecond(short second) {
112     this.second = second;
113   }
114   
115   public short getMillis() {
116     return millis;
117   }
118   
119   public void setMillis(short millis) {
120     this.millis = millis;
121   }
122   
123   public Date JavaDoc add(Date JavaDoc dateTime) {
124     Calendar JavaDoc calendar = Calendar.getInstance();
125     calendar.setTime(dateTime);
126     
127     calendar.add(Calendar.YEAR, year);
128     calendar.add(Calendar.MONTH, month);
129     calendar.add(Calendar.DAY_OF_MONTH, day);
130     calendar.add(Calendar.HOUR_OF_DAY, hour);
131     calendar.add(Calendar.MINUTE, minute);
132     calendar.add(Calendar.SECOND, second);
133     calendar.add(Calendar.MILLISECOND, millis);
134     
135     return calendar.getTime();
136   }
137   
138   public boolean equals(Object JavaDoc obj) {
139     boolean equals = false;
140     
141     if (obj instanceof Duration) {
142       Duration d = (Duration) obj;
143       equals =
144         year == d.year && month == d.month && day == d.day &&
145         hour == d.hour && minute == d.minute && second == d.second &&
146         millis == d.millis && negative == d.negative;
147     }
148     return equals;
149   }
150   
151   public int hashCode() {
152     return year ^ month ^ day ^ hour ^ minute ^ second ^ millis ^ (negative ? 1231 : 1237);
153   }
154   
155   public String JavaDoc toString() {
156     StringBuffer JavaDoc literal = new StringBuffer JavaDoc();
157     
158     if (negative)
159       literal.append('-');
160     literal.append('P');
161     
162     if (year != 0)
163       literal.append(year).append('Y');
164     if (month != 0)
165       literal.append(month).append('M');
166     if (day != 0)
167       literal.append(day).append('D');
168     
169     if (hour != 0 || minute != 0 || second != 0 || millis != 0) {
170       literal.append('T');
171       
172       if (hour != 0)
173         literal.append(hour).append('H');
174       if (minute != 0)
175         literal.append(minute).append('M');
176       
177       if (second != 0) {
178         literal.append(second);
179         
180         if (millis != 0)
181           literal.append('.').append(formatDecimal(millis, 3));
182         
183         literal.append('S');
184       }
185       else if (millis != 0)
186         literal.append('0').append(formatDecimal(millis, 3)).append('S');
187     }
188     return literal.toString();
189   }
190   
191   public static Duration parseDuration(String JavaDoc literal) {
192     Matcher JavaDoc matcher = durationPattern.matcher(literal);
193     Duration duration = null;
194     if (matcher.matches()) {
195       duration = new Duration();
196       // group 1: negative sign
197
if (matcher.group(1) != null)
198         duration.setNegative(true);
199       // group 2: years
200
String JavaDoc group = matcher.group(2);
201       if (group != null)
202         duration.setYear(Short.parseShort(group));
203       // group 3: months
204
group = matcher.group(3);
205       if (group != null)
206         duration.setMonth(Short.parseShort(group));
207       // group 4: days
208
group = matcher.group(4);
209       if (group != null)
210         duration.setDay(Short.parseShort(group));
211       // group 5: hours
212
group = matcher.group(5);
213       if (group != null)
214         duration.setHour(Short.parseShort(group));
215       // group 6: minutes
216
group = matcher.group(6);
217       if (group != null)
218         duration.setMinute(Short.parseShort(group));
219       // group 7: seconds
220
group = matcher.group(7);
221       if (group != null)
222         duration.setSecond(Short.parseShort(group));
223       // group 8: milliseconds
224
group = matcher.group(8);
225       if (group != null)
226         duration.setMillis(parseDecimal(group, 3));
227     }
228     return duration;
229   }
230   
231   private static short parseDecimal(String JavaDoc literal, int digitCount) {
232     short number = Short.parseShort(literal);
233     
234     for (int i = 0, n = digitCount - literal.length(); i < n; i++)
235       number *= 10;
236     
237     return number;
238   }
239   
240   private static String JavaDoc formatDecimal(short number, int digitCount) {
241     String JavaDoc literal = Short.toString(number);
242     
243     if (literal.length() < digitCount) {
244       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
245       
246       for (int i = 0, n = digitCount - literal.length(); i < n; i++) {
247         buffer.append('0');
248       }
249       literal = buffer.append(literal).toString();
250     }
251     
252     int i = digitCount;
253     while (--i > 0 && literal.charAt(i) == '0')
254       ; // just decrease the index
255

256     return literal.substring(0, i + 1);
257   }
258
259   public Date JavaDoc getTime() {
260     Calendar JavaDoc calendar = new GregorianCalendar JavaDoc(year, month, day, hour, minute, second);
261     calendar.add(Calendar.MILLISECOND, millis);
262     return calendar.getTime();
263   }
264 }
265
Popular Tags