1 /* Duration.java -- 2 Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 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. 10 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. 15 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., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 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. 25 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. */ 37 38 package javax.xml.datatype; 39 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; 46 47 /** 48 * An immutable time space as specified in XML Schema 1.0. 49 * 50 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a) 51 * @since 1.5 52 */ 53 public abstract class Duration 54 { 55 56 /** 57 * Returns the name of the XML Schema data type this value maps to. 58 */ getXMLSchemaType()59 public QName getXMLSchemaType() 60 { 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) 69 { 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(); 78 } 79 } 80 81 /** 82 * Returns the sign of this value. 83 */ getSign()84 public abstract int getSign(); 85 86 /** 87 * Returns the years in this duration as an int, or 0 if not present. 88 */ getYears()89 public int getYears() 90 { 91 Number val = getField(DatatypeConstants.YEARS); 92 return (val == null) ? 0 : val.intValue(); 93 } 94 95 /** 96 * Returns the months in this duration as an int, or 0 if not present. 97 */ getMonths()98 public int getMonths() 99 { 100 Number val = getField(DatatypeConstants.MONTHS); 101 return (val == null) ? 0 : val.intValue(); 102 } 103 104 /** 105 * Returns the days in this duration as an int, or 0 if not present. 106 */ getDays()107 public int getDays() 108 { 109 Number val = getField(DatatypeConstants.DAYS); 110 return (val == null) ? 0 : val.intValue(); 111 } 112 113 /** 114 * Returns the hours in this duration as an int, or 0 if not present. 115 */ getHours()116 public int getHours() 117 { 118 Number val = getField(DatatypeConstants.HOURS); 119 return (val == null) ? 0 : val.intValue(); 120 } 121 122 /** 123 * Returns the minutes in this duration as an int, or 0 if not present. 124 */ getMinutes()125 public int getMinutes() 126 { 127 Number val = getField(DatatypeConstants.MINUTES); 128 return (val == null) ? 0 : val.intValue(); 129 } 130 131 /** 132 * Returns the seconds in this duration as an int, or 0 if not present. 133 */ getSeconds()134 public int getSeconds() 135 { 136 Number val = getField(DatatypeConstants.SECONDS); 137 return (val == null) ? 0 : val.intValue(); 138 } 139 140 /** 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. 145 */ getTimeInMillis(Calendar startInstant)146 public long getTimeInMillis(Calendar startInstant) 147 { 148 Calendar cal = (Calendar) startInstant.clone(); 149 long t1 = cal.getTimeInMillis(); 150 addTo(cal); 151 long t2 = cal.getTimeInMillis(); 152 return t2 - t1; 153 } 154 155 /** 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. 160 */ getTimeInMillis(Date startInstant)161 public long getTimeInMillis(Date startInstant) 162 { 163 Date date = (Date) startInstant.clone(); 164 long t1 = date.getTime(); 165 addTo(date); 166 long t2 = date.getTime(); 167 return t2 - t1; 168 } 169 170 /** 171 * Returns the value of the specified field, or <code>null</code> if the 172 * field is undefined. 173 */ getField(DatatypeConstants.Field field)174 public abstract Number getField(DatatypeConstants.Field field); 175 176 /** 177 * Indicates whether the specified field is set. 178 */ isSet(DatatypeConstants.Field field)179 public abstract boolean isSet(DatatypeConstants.Field field); 180 181 /** 182 * Returns the result of adding the specified duration to this duration. 183 */ add(Duration rhs)184 public abstract Duration add(Duration rhs); 185 186 /** 187 * Adds this duration to the specified calendar. 188 */ addTo(Calendar calendar)189 public abstract void addTo(Calendar calendar); 190 /*{ 191 switch (getSign()) 192 { 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()); 208 } 209 }*/ 210 211 /** 212 * Adds this duration to the specified date. 213 */ addTo(Date date)214 public void addTo(Date date) 215 { 216 Calendar calendar = new GregorianCalendar(); 217 calendar.setTimeInMillis(date.getTime()); 218 addTo(calendar); 219 date.setTime(calendar.getTimeInMillis()); 220 } 221 222 /** 223 * Returns the result of subtracting the given duration from this 224 * duration. 225 */ subtract(Duration rhs)226 public Duration subtract(Duration rhs) 227 { 228 // TODO 229 throw new UnsupportedOperationException(); 230 } 231 232 /** 233 * Returns the result of multiplying this duration by the given factor. 234 */ multiply(int factor)235 public Duration multiply(int factor) 236 { 237 return multiply(BigDecimal.valueOf((long) factor)); 238 } 239 240 /** 241 * Returns the result of multiplying this duration by the given factor. 242 */ multiply(BigDecimal factor)243 public abstract Duration multiply(BigDecimal factor); 244 245 /** 246 * Returns the unary negative of this duration. 247 */ negate()248 public abstract Duration negate(); 249 250 /** 251 * Converts the years and months fields into the days field using a 252 * specific time instant as the reference point. 253 */ normalizeWith(Calendar startTimeInstant)254 public abstract Duration normalizeWith(Calendar startTimeInstant); 255 256 /** 257 * Partial order relation comparison with this duration, in accordance 258 * with XML Schema 1.0 Part 2, Section 3.2.7.6.2. 259 */ compare(Duration duration)260 public abstract int compare(Duration duration); 261 isLongerThan(Duration duration)262 public boolean isLongerThan(Duration duration) 263 { 264 // TODO 265 throw new UnsupportedOperationException(); 266 } 267 isShorterThan(Duration duration)268 public boolean isShorterThan(Duration duration) 269 { 270 // TODO 271 throw new UnsupportedOperationException(); 272 } 273 equals(java.lang.Object duration)274 public boolean equals(java.lang.Object duration) 275 { 276 // TODO 277 throw new UnsupportedOperationException(); 278 } 279 hashCode()280 public abstract int hashCode(); 281 282 /** 283 * Returns the lexical representation of this duration. 284 */ toString()285 public String toString() 286 { 287 // TODO 288 throw new UnsupportedOperationException(); 289 } 290 291 } 292