KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > catcode > odf > Duration


1 /*
2     Duration creates an XML-Schema-style Duration object.
3     Copyright (C) 2005 J. David Eisenberg
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18     
19     Author: J. David Eisenberg
20     Contact: catcode@catcode.com
21
22 */

23 package com.catcode.odf;
24
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27 /**
28  * Creates an XML-Schema-style Duration object.
29  * @author J. David Eisenberg
30  * @version 0.1, 2005-10-24
31  */

32 public class Duration
33 {
34     protected int years;
35     protected int months;
36     protected int days;
37     protected int hours;
38     protected int minutes;
39     protected double seconds;
40     protected boolean negative;
41     
42     private static final Pattern JavaDoc durationPattern =
43         Pattern.compile("-?P(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?"
44             + "(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:([\\d.]+)S)?)?");
45
46     /**
47      * Create a new <code>Duration</code> with all fields
48      * set to zero.
49      */

50     public Duration()
51     {
52         this( 0, 0, 0, 0, 0, 0.0 );
53     }
54
55     /**
56      * Create a new <code>Duration</code> with
57      * <code>years</code>, <code>months</code> and <code>days</code>
58      * set as specified.
59      *
60      * @param years the number of years.
61      * @param months the number of months.
62      * @param days the number of days
63      */

64     public Duration( int years, int months, int days )
65     {
66         this( years, months, days, 0, 0, 0.0 );
67     }
68     
69     /**
70      * Create a new <code>Duration</code> with all
71      * numeric fields set as specified.
72      *
73      * @param years the number of years.
74      * @param months the number of months.
75      * @param days the number of days
76      * @param hours the number of hours.
77      * @param minutes the number of minutes.
78      * @param seconds the number of seconds.
79      */

80     public Duration( int years, int months, int days,
81         int hours, int minutes, double seconds )
82     {
83         this.years = years;
84         this.months = months;
85         this.days = days;
86         this.hours = hours;
87         this.minutes = minutes;
88         this.seconds = seconds;
89         this.negative = false;
90     }
91     
92     /**
93      * Returns the number of years of this <code>Duration</code>.
94      * @return number of years of this <code>Duration</code>.
95      */

96     public int getYears()
97     {
98         return this.years;
99     }
100
101     /**
102      * Sets the number of years of this <code>Duration</code> to
103      * the specified value.
104      *
105      * @param years the number of years.
106      */

107     public void setYears(int years)
108     {
109         this.years = years;
110     }
111
112     /**
113      * Returns the number of years of this <code>Duration</code>.
114      * @return number of years of this <code>Duration</code>.
115      */

116     public int getMonths()
117     {
118         return this.months;
119     }
120
121     /**
122      * Sets the number of months of this <code>Duration</code> to
123      * the specified value.
124      *
125      * @param months the number of months.
126      */

127     public void setMonths(int months)
128     {
129         this.months = months;
130     }
131
132     /**
133      * Returns the number of years of this <code>Duration</code>.
134      * @return number of years of this <code>Duration</code>.
135      */

136     public int getDays()
137     {
138         return this.days;
139     }
140
141     /**
142      * Sets the number of days of this <code>Duration</code> to
143      * the specified value.
144      *
145      * @param days the number of days.
146      */

147     public void setDays(int days)
148     {
149         this.days = days;
150     }
151
152     /**
153      * Returns the number of years of this <code>Duration</code>.
154      * @return number of years of this <code>Duration</code>.
155      */

156     public int getHours()
157     {
158         return this.hours;
159     }
160
161     /**
162      * Sets the number of hours of this <code>Duration</code> to
163      * the specified value.
164      *
165      * @param hours the number of hours.
166      */

167     public void setHours(int hours)
168     {
169         this.hours = hours;
170     }
171
172     /**
173      * Returns the number of years of this <code>Duration</code>.
174      * @return number of years of this <code>Duration</code>.
175      */

176     public int getMinutes()
177     {
178         return this.minutes;
179     }
180
181     /**
182      * Sets the number of minutes of this <code>Duration</code> to
183      * the specified value.
184      *
185      * @param minutes the number of minutes.
186      */

187     public void setMinutes(int minutes)
188     {
189         this.minutes = minutes;
190     }
191
192     /**
193      * Returns the number of years of this <code>Duration</code>.
194      * @return number of years of this <code>Duration</code>.
195      */

196     public double getSeconds()
197     {
198         return this.seconds;
199     }
200
201     /**
202      * Sets the number of seconds of this <code>Duration</code> to
203      * the specified value.
204      *
205      * @param seconds the number of seconds.
206      */

207     public void setSeconds(double seconds)
208     {
209         this.seconds = seconds;
210     }
211
212     /**
213      * Returns the negative status <code>Duration</code>.
214      * @return status of the <code>negative</code> field of
215      * this <code>Duration</code>.
216      */

217     public boolean isNegative()
218     {
219         return this.negative;
220     }
221     
222     /**
223      * Sets the negative status of this <code>Duration</code> to
224      * the specified value.
225      *
226      * @param negative the negative status.
227      */

228     public void setNegative( boolean negative )
229     {
230         this.negative = negative;
231     }
232
233     /**
234      * Parses the string argument as a <code>Duration</code>.
235      * The format of the string should be
236      * <code>PyyYmmMddDThhHmmMssS</code> with an optional leading
237      * minus sign. The seconds may contain a decimal point.
238      *
239      * @param str the string to parse
240      * @return the <code>Duration</code> specified by the string.
241      */

242     public static Duration parseDuration( String JavaDoc str )
243     {
244         String JavaDoc part;
245         Duration d = new Duration();
246         Matcher JavaDoc m = durationPattern.matcher( str );
247         if (m.find())
248         {
249             d.negative = (str.startsWith("-"));
250             try
251             {
252                 part = m.group(1);
253                 d.years = (part != null ) ? Integer.parseInt( part ) : 0;
254                 part = m.group(2);
255                 d.months = (part != null ) ? Integer.parseInt( part ) : 0;
256                 part = m.group(3);
257                 d.days = (part != null ) ? Integer.parseInt( part ) : 0;
258                 part = m.group(4);
259                 d.hours = (part != null ) ? Integer.parseInt( part ) : 0;
260                 part = m.group(5);
261                 d.minutes = (part != null ) ? Integer.parseInt( part ) : 0;
262                 part = m.group(6);
263                 d.seconds = (part != null ) ? Double.parseDouble( part ) : 0.0;
264             }
265             catch (Exception JavaDoc e)
266             {
267                 d = null;
268             }
269         }
270         else
271         {
272             d = null;
273         }
274         return d;
275     }
276     
277     /**
278      * Returns a string representation of this
279      * <code>Duration</code>.
280      *
281      * @return a string representation of
282      * this <code>Duration</code>.
283      */

284     public String JavaDoc toString( )
285     {
286         String JavaDoc result = (negative) ? "-P" : "P";
287         
288         if (years > 0 || months > 0 || days > 0)
289         {
290             if (years > 0)
291             {
292                 result += years + "Y";
293             }
294             if (months > 0)
295             {
296                 result += months + "M";
297             }
298             if (days > 0)
299             {
300                 result += days + "D";
301             }
302         }
303         else
304         {
305             result += "0D";
306         }
307         if (hours > 0 || minutes > 0 || seconds > 0)
308         {
309             result += "T";
310             if (hours > 0)
311             {
312                 result += hours + "H";
313             }
314             if (minutes > 0)
315             {
316                 result += minutes + "M";
317             }
318             if (seconds > 0)
319             {
320                 result += seconds + "S";
321             }
322         }
323         return result;
324     }
325 }
326
327
Popular Tags