KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > types > Duration


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.types;
57
58 import org.jboss.axis.utils.Messages;
59
60
61 /**
62  * Implementation of the XML Schema type duration
63  *
64  * @author Wes Moulder <wes@themindelectric.com>
65  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#duration">XML Schema 3.2.6</a>
66  */

67 public class Duration
68 {
69    boolean isNegative = false;
70    int years;
71    int months;
72    int days;
73    int hours;
74    int minutes;
75    double seconds;
76
77    /**
78     * Default no-arg constructor
79     */

80    public Duration()
81    {
82    }
83
84    /**
85     * @param negative
86     * @param aYears
87     * @param aMonths
88     * @param aDays
89     * @param aHours
90     * @param aMinutes
91     * @param aSeconds
92     */

93    public Duration(boolean negative, int aYears, int aMonths, int aDays, int aHours, int aMinutes, double aSeconds)
94    {
95       isNegative = negative;
96       years = aYears;
97       months = aMonths;
98       days = aDays;
99       hours = aHours;
100       minutes = aMinutes;
101       seconds = aSeconds;
102    }
103
104    /**
105     * This method takes a string that represents an xsd:duration and parses it.
106     *
107     * @param duration
108     * @throws SchemaException if the string doesn't parse correctly.
109     */

110    public Duration(String JavaDoc duration) throws IllegalArgumentException JavaDoc
111    {
112       int position = 1;
113       int timePosition = duration.indexOf("T");
114
115       if (duration.indexOf("P") == -1)
116          throw new IllegalArgumentException JavaDoc(Messages.getMessage("badDuration"));
117
118       if (duration.startsWith("-"))
119       {
120          isNegative = true;
121          position++;
122       }
123
124       if (timePosition != -1)
125          parseTime(duration.substring(timePosition + 1));
126       else
127          timePosition = duration.length();
128
129       parseDate(duration.substring(position, timePosition));
130    }
131
132    /**
133     * This method parses the time portion of a duration.
134     *
135     * @param time
136     */

137    public void parseTime(String JavaDoc time)
138    {
139       int start = 0;
140       int end = time.indexOf("H");
141
142       if (end != -1)
143       {
144          hours = Integer.parseInt(time.substring(0, end));
145          start = end + 1;
146       }
147
148       end = time.indexOf("M");
149
150       if (end != -1)
151       {
152          minutes = Integer.parseInt(time.substring(start, end));
153          start = end + 1;
154       }
155
156       end = time.indexOf("S");
157
158       if (end != -1)
159          seconds = Double.parseDouble(time.substring(start, end));
160    }
161
162    /**
163     * This method parses the date portion of a duration.
164     *
165     * @param date
166     */

167    public void parseDate(String JavaDoc date)
168    {
169       int start = 0;
170       int end = date.indexOf("Y");
171
172       if (end != -1)
173       {
174          years = Integer.parseInt(date.substring(0, end));
175          start = end + 1;
176       }
177
178       end = date.indexOf("M");
179
180       if (end != -1)
181       {
182          months = Integer.parseInt(date.substring(start, end));
183          start = end + 1;
184       }
185
186       end = date.indexOf("D");
187
188       if (end != -1)
189          days = Integer.parseInt(date.substring(start, end));
190    }
191
192    /**
193     *
194     */

195    public boolean isNegative()
196    {
197       return isNegative;
198    }
199
200    /**
201     *
202     */

203    public int getYears()
204    {
205       return years;
206    }
207
208    /**
209     *
210     */

211    public int getMonths()
212    {
213       return months;
214    }
215
216    /**
217     *
218     */

219    public int getDays()
220    {
221       return days;
222    }
223
224    /**
225     *
226     */

227    public int getHours()
228    {
229       return hours;
230    }
231
232    /**
233     *
234     */

235    public int getMinutes()
236    {
237       return minutes;
238    }
239
240    /**
241     *
242     */

243    public double getSeconds()
244    {
245       return seconds;
246    }
247
248    /**
249     * @param negative
250     */

251    public void setNegative(boolean negative)
252    {
253       isNegative = negative;
254    }
255
256    /**
257     * @param years
258     */

259    public void setYears(int years)
260    {
261       this.years = years;
262    }
263
264    /**
265     * @param months
266     */

267    public void setMonths(int months)
268    {
269       this.months = months;
270    }
271
272    /**
273     * @param days
274     */

275    public void setDays(int days)
276    {
277       this.days = days;
278    }
279
280    /**
281     * @param hours
282     */

283    public void setHours(int hours)
284    {
285       this.hours = hours;
286    }
287
288    /**
289     * @param minutes
290     */

291    public void setMinutes(int minutes)
292    {
293       this.minutes = minutes;
294    }
295
296    /**
297     * @param seconds
298     */

299    public void setSeconds(int seconds)
300    {
301       this.seconds = seconds;
302    }
303
304    /**
305     * This returns the xml representation of an xsd:duration object.
306     */

307    public String JavaDoc toString()
308    {
309       StringBuffer JavaDoc duration = new StringBuffer JavaDoc();
310
311       duration.append("P");
312
313       if (years != 0)
314          duration.append(years + "Y");
315
316       if (months != 0)
317          duration.append(months + "M");
318
319       if (days != 0)
320          duration.append(days + "D");
321
322       if (hours != 0 || minutes != 0 || seconds != 0.0)
323       {
324          duration.append("T");
325
326          if (hours != 0)
327             duration.append(hours + "H");
328
329          if (minutes != 0)
330             duration.append(minutes + "M");
331
332          if (seconds != 0)
333          {
334             if (seconds == (int)seconds)
335                duration.append((int)seconds + "S");
336             else
337                duration.append(seconds + "S");
338          }
339       }
340
341       if (duration.length() == 1)
342          duration.append("T0S");
343
344       if (isNegative)
345          duration.insert(0, "-");
346
347       return duration.toString();
348    }
349
350    /**
351     * This currently does a verbatim check on the object. If you have a
352     * duration that is 60 minutes, and one that is 1 hour, they won't
353     * be equal.
354     *
355     * @param object
356     * @todo make this more flexible
357     */

358    public boolean equals(Object JavaDoc object)
359    {
360       if (!(object instanceof Duration))
361          return false;
362
363       Duration duration = (Duration)object;
364
365       int totalMonthsInTime = this.years * 12 + this.months;
366       int totalMonthsToCompare = duration.years * 12 + duration.months;
367
368       double totalSecondsInTime = ((this.days * 24 + this.hours) * 60 + this.minutes) * 60 + this.seconds;
369       double totalSecondsToCompare = ((duration.days * 24 + duration.hours) * 60 + duration.minutes) * 60 + duration.seconds;
370
371       return
372               this.isNegative == duration.isNegative &&
373               totalMonthsInTime == totalMonthsToCompare &&
374               totalSecondsInTime == totalSecondsToCompare;
375    }
376
377    /**
378     *
379     */

380    public int hashCode()
381    {
382       int hashCode = 0;
383
384       if (isNegative)
385          hashCode++;
386
387       hashCode += years;
388       hashCode += months;
389       hashCode += days;
390       hashCode += hours;
391       hashCode += minutes;
392       hashCode += seconds;
393
394       return hashCode;
395    }
396 }
Popular Tags