Merge from the pain train
[official-gcc.git] / libjava / javax / xml / datatype / Duration.java
blobb7d188cd7b8bd841cfdd53c4e70eb07372cbf7df
1 /* Duration.java --
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.xml.datatype;
40 import java.math.BigDecimal;
41 import java.util.Calendar;
42 import java.util.Date;
43 import java.util.GregorianCalendar;
44 import javax.xml.datatype.DatatypeConstants;
45 import javax.xml.namespace.QName;
47 /**
48 * An immutable time space as specified in XML Schema 1.0.
50 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
51 * @since 1.3
53 public abstract class Duration
56 /**
57 * Returns the name of the XML Schema data type this value maps to.
59 public QName getXMLSchemaType()
61 int state = 0;
62 state |= isSet(DatatypeConstants.YEARS) ? 32 : 0;
63 state |= isSet(DatatypeConstants.MONTHS) ? 16 : 0;
64 state |= isSet(DatatypeConstants.DAYS) ? 8 : 0;
65 state |= isSet(DatatypeConstants.HOURS) ? 4 : 0;
66 state |= isSet(DatatypeConstants.MINUTES) ? 2 : 0;
67 state |= isSet(DatatypeConstants.SECONDS) ? 1 : 0;
68 switch (state)
70 case 63:
71 return DatatypeConstants.DURATION;
72 case 15:
73 return DatatypeConstants.DURATION_DAYTIME;
74 case 48:
75 return DatatypeConstants.DURATION_YEARMONTH;
76 default:
77 throw new IllegalStateException();
81 /**
82 * Returns the sign of this value.
84 public abstract int getSign();
86 /**
87 * Returns the years in this duration as an int, or 0 if not present.
89 public int getYears()
91 Number val = getField(DatatypeConstants.YEARS);
92 return (val == null) ? 0 : val.intValue();
95 /**
96 * Returns the months in this duration as an int, or 0 if not present.
98 public int getMonths()
100 Number val = getField(DatatypeConstants.MONTHS);
101 return (val == null) ? 0 : val.intValue();
105 * Returns the days in this duration as an int, or 0 if not present.
107 public int getDays()
109 Number val = getField(DatatypeConstants.DAYS);
110 return (val == null) ? 0 : val.intValue();
114 * Returns the hours in this duration as an int, or 0 if not present.
116 public int getHours()
118 Number val = getField(DatatypeConstants.HOURS);
119 return (val == null) ? 0 : val.intValue();
123 * Returns the minutes in this duration as an int, or 0 if not present.
125 public int getMinutes()
127 Number val = getField(DatatypeConstants.MINUTES);
128 return (val == null) ? 0 : val.intValue();
132 * Returns the seconds in this duration as an int, or 0 if not present.
134 public int getSeconds()
136 Number val = getField(DatatypeConstants.SECONDS);
137 return (val == null) ? 0 : val.intValue();
141 * Returns the duration length in milliseconds.
142 * Because the length of a month or year may vary depending on the year,
143 * the <code>startInstant</code> parameter is used to specify the duration
144 * offset.
146 public long getTimeInMillis(Calendar startInstant)
148 Calendar cal = (Calendar) startInstant.clone();
149 long t1 = cal.getTimeInMillis();
150 addTo(cal);
151 long t2 = cal.getTimeInMillis();
152 return t2 - t1;
156 * Returns the duration length in milliseconds.
157 * Because the length of a month or year may vary depending on the year,
158 * the <code>startInstant</code> parameter is used to specify the duration
159 * offset.
161 public long getTimeInMillis(Date startInstant)
163 Date date = (Date) startInstant.clone();
164 long t1 = date.getTime();
165 addTo(date);
166 long t2 = date.getTime();
167 return t2 - t1;
171 * Returns the value of the specified field, or <code>null</code> if the
172 * field is undefined.
174 public abstract Number getField(DatatypeConstants.Field field);
177 * Indicates whether the specified field is set.
179 public abstract boolean isSet(DatatypeConstants.Field field);
182 * Returns the result of adding the specified duration to this duration.
184 public abstract Duration add(Duration rhs);
187 * Adds this duration to the specified calendar.
189 public abstract void addTo(Calendar calendar);
191 switch (getSign())
193 case -1:
194 calendar.add(Calendar.YEAR, -getYears());
195 calendar.add(Calendar.MONTH, -getMonths());
196 calendar.add(Calendar.DATE, -getDays());
197 calendar.add(Calendar.HOUR, -getHours());
198 calendar.add(Calendar.MINUTE, -getMinutes());
199 calendar.add(Calendar.SECOND, -getSeconds());
200 break;
201 case 1:
202 calendar.add(Calendar.YEAR, getYears());
203 calendar.add(Calendar.MONTH, getMonths());
204 calendar.add(Calendar.DATE, getDays());
205 calendar.add(Calendar.HOUR, getHours());
206 calendar.add(Calendar.MINUTE, getMinutes());
207 calendar.add(Calendar.SECOND, getSeconds());
212 * Adds this duration to the specified date.
214 public void addTo(Date date)
216 Calendar calendar = new GregorianCalendar();
217 calendar.setTimeInMillis(date.getTime());
218 addTo(calendar);
219 date.setTime(calendar.getTimeInMillis());
223 * Returns the result of subtracting the given duration from this
224 * duration.
226 public Duration subtract(Duration rhs)
228 // TODO
229 throw new UnsupportedOperationException();
233 * Returns the result of multiplying this duration by the given factor.
235 public Duration multiply(int factor)
237 return multiply(BigDecimal.valueOf((long) factor));
241 * Returns the result of multiplying this duration by the given factor.
243 public Duration multiply(BigDecimal factor)
245 // TODO
246 throw new UnsupportedOperationException();
250 * Returns the unary negative of this duration.
252 public abstract Duration negate();
255 * Converts the years and months fields into the days field using a
256 * specific time instant as the reference point.
258 public abstract Duration normalizeWith(Calendar startTimeInstant);
261 * Partial order relation comparison with this duration, in accordance
262 * with XML Schema 1.0 Part 2, Section 3.2.7.6.2.
264 public abstract int compare(Duration duration);
266 public boolean isLongerThan(Duration duration)
268 // TODO
269 throw new UnsupportedOperationException();
272 public boolean isShorterThan(Duration duration)
274 // TODO
275 throw new UnsupportedOperationException();
278 public boolean equals(java.lang.Object duration)
280 // TODO
281 throw new UnsupportedOperationException();
284 public abstract int hashCode();
287 * Returns the lexical representation of this duration.
289 public String toString()
291 // TODO
292 throw new UnsupportedOperationException();