1 16 17 package org.apache.axis.types; 18 19 import org.apache.axis.utils.Messages; 20 21 import java.util.Calendar ; 22 23 31 public class Duration implements java.io.Serializable { 32 boolean isNegative = false; 33 int years; 34 int months; 35 int days; 36 int hours; 37 int minutes; 38 double seconds; 39 40 43 public Duration() { 44 } 45 46 55 public Duration(boolean negative, int aYears, int aMonths, int aDays, 56 int aHours, int aMinutes, double aSeconds) { 57 isNegative = negative; 58 years = aYears; 59 months = aMonths; 60 days = aDays; 61 hours = aHours; 62 minutes = aMinutes; 63 setSeconds(aSeconds); 64 } 65 66 73 public Duration(String duration) throws IllegalArgumentException { 74 int position = 1; 75 int timePosition = duration.indexOf("T"); 76 77 if (duration.indexOf("P") == -1 || duration.equals("P")) { 79 throw new IllegalArgumentException ( 80 Messages.getMessage("badDuration")); 81 } 82 83 if (duration.lastIndexOf("T") == duration.length() - 1) { 85 throw new IllegalArgumentException ( 86 Messages.getMessage("badDuration")); 87 } 88 89 if (duration.startsWith("-")) { 91 isNegative = true; 92 position++; 93 } 94 95 if (timePosition != -1) { 97 parseTime(duration.substring(timePosition + 1)); 98 } else { 99 timePosition = duration.length(); 100 } 101 102 if (position != timePosition) { 104 parseDate(duration.substring(position, timePosition)); 105 } 106 } 107 108 115 public Duration(boolean negative, Calendar calendar) throws 116 IllegalArgumentException { 117 this.isNegative = negative; 118 this.years = calendar.get(Calendar.YEAR); 119 this.months = calendar.get(Calendar.MONTH); 120 this.days = calendar.get(Calendar.DATE); 121 this.hours = calendar.get(Calendar.HOUR); 122 this.minutes = calendar.get(Calendar.MINUTE); 123 this.seconds = calendar.get(Calendar.SECOND); 124 this.seconds += ((double) calendar.get(Calendar.MILLISECOND)) / 100; 125 if (years == 0 && months == 0 && days == 0 && hours == 0 && 126 minutes == 0 && seconds == 0) { 127 throw new IllegalArgumentException (Messages.getMessage( 128 "badCalendarForDuration")); 129 } 130 } 131 132 140 public void parseTime(String time) throws IllegalArgumentException { 141 if (time.length() == 0 || time.indexOf("-") != -1) { 142 throw new IllegalArgumentException ( 143 Messages.getMessage("badTimeDuration")); 144 } 145 146 if (!time.endsWith("H") && !time.endsWith("M") && !time.endsWith("S")) { 148 throw new IllegalArgumentException ( 149 Messages.getMessage("badTimeDuration")); 150 } 151 152 try { 153 int start = 0; 155 156 int end = time.indexOf("H"); 158 if (start == end) { 161 throw new IllegalArgumentException ( 162 Messages.getMessage("badTimeDuration")); 163 } 164 if (end != -1) { 165 hours = Integer.parseInt(time.substring(0, end)); 166 start = end + 1; 167 } 168 169 end = time.indexOf("M"); 171 if (start == end) { 174 throw new IllegalArgumentException ( 175 Messages.getMessage("badTimeDuration")); 176 } 177 178 if (end != -1) { 179 minutes = Integer.parseInt(time.substring(start, end)); 180 start = end + 1; 181 } 182 183 end = time.indexOf("S"); 185 if (start == end) { 188 throw new IllegalArgumentException ( 189 Messages.getMessage("badTimeDuration")); 190 } 191 192 if (end != -1) { 193 setSeconds(Double.parseDouble(time.substring(start, end))); 194 } 195 } catch (NumberFormatException e) { 196 throw new IllegalArgumentException ( 197 Messages.getMessage("badTimeDuration")); 198 } 199 } 200 201 209 public void parseDate(String date) throws IllegalArgumentException { 210 if (date.length() == 0 || date.indexOf("-") != -1) { 211 throw new IllegalArgumentException ( 212 Messages.getMessage("badDateDuration")); 213 } 214 215 if (!date.endsWith("Y") && !date.endsWith("M") && !date.endsWith("D")) { 217 throw new IllegalArgumentException ( 218 Messages.getMessage("badDateDuration")); 219 } 220 221 try { 223 int start = 0; 225 int end = date.indexOf("Y"); 226 227 if (start == end) { 230 throw new IllegalArgumentException ( 231 Messages.getMessage("badDateDuration")); 232 } 233 if (end != -1) { 234 years = Integer.parseInt(date.substring(0, end)); 235 start = end + 1; 236 } 237 238 end = date.indexOf("M"); 240 if (start == end) { 243 throw new IllegalArgumentException ( 244 Messages.getMessage("badDateDuration")); 245 } 246 if (end != -1) { 247 months = Integer.parseInt(date.substring(start, end)); 248 start = end + 1; 249 } 250 251 end = date.indexOf("D"); 252 if (start == end) { 255 throw new IllegalArgumentException ( 256 Messages.getMessage("badDateDuration")); 257 } 258 if (end != -1) { 259 days = Integer.parseInt(date.substring(start, end)); 260 } 261 } catch (NumberFormatException e) { 262 throw new IllegalArgumentException ( 263 Messages.getMessage("badDateDuration")); 264 } 265 } 266 267 270 public boolean isNegative() { 271 return isNegative; 272 } 273 274 277 public int getYears() { 278 return years; 279 } 280 281 284 public int getMonths() { 285 return months; 286 } 287 288 291 public int getDays() { 292 return days; 293 } 294 295 298 public int getHours() { 299 return hours; 300 } 301 302 305 public int getMinutes() { 306 return minutes; 307 } 308 309 312 public double getSeconds() { 313 return seconds; 314 } 315 316 319 public void setNegative(boolean negative) { 320 isNegative = negative; 321 } 322 323 326 public void setYears(int years) { 327 this.years = years; 328 } 329 330 333 public void setMonths(int months) { 334 this.months = months; 335 } 336 337 340 public void setDays(int days) { 341 this.days = days; 342 } 343 344 347 public void setHours(int hours) { 348 this.hours = hours; 349 } 350 351 354 public void setMinutes(int minutes) { 355 this.minutes = minutes; 356 } 357 358 363 public void setSeconds(int seconds) { 364 this.seconds = seconds; 365 } 366 367 373 public void setSeconds(double seconds) { 374 this.seconds = ((double) (Math.round(seconds * 100))) / 100; 375 } 376 377 380 public String toString() { 381 StringBuffer duration = new StringBuffer (); 382 383 duration.append("P"); 384 385 if (years != 0) { 386 duration.append(years + "Y"); 387 } 388 if (months != 0) { 389 duration.append(months + "M"); 390 } 391 if (days != 0) { 392 duration.append(days + "D"); 393 } 394 if (hours != 0 || minutes != 0 || seconds != 0.0) { 395 duration.append("T"); 396 397 if (hours != 0) { 398 duration.append(hours + "H"); 399 400 } 401 if (minutes != 0) { 402 duration.append(minutes + "M"); 403 404 } 405 if (seconds != 0) { 406 if (seconds == (int) seconds) { 407 duration.append((int) seconds + "S"); 408 } else { 409 duration.append(seconds + "S"); 410 } 411 } 412 } 413 414 if (duration.length() == 1) { 415 duration.append("T0S"); 416 } 417 418 if (isNegative) { 419 duration.insert(0, "-"); 420 } 421 422 return duration.toString(); 423 } 424 425 433 public boolean equals(Object object) { 434 if (!(object instanceof Duration)) { 435 return false; 436 } 437 438 Calendar thisCalendar = this.getAsCalendar(); 439 Duration duration = (Duration) object; 440 441 return this.isNegative == duration.isNegative && 442 this.getAsCalendar().equals(duration.getAsCalendar()); 443 } 444 445 public int hashCode() { 446 int hashCode = 0; 447 448 if (isNegative) { 449 hashCode++; 450 } 451 hashCode += years; 452 hashCode += months; 453 hashCode += days; 454 hashCode += hours; 455 hashCode += minutes; 456 hashCode += seconds; 457 hashCode += (seconds * 100) % 100; 459 460 return hashCode; 461 } 462 463 472 public Calendar getAsCalendar() { 473 return getAsCalendar(Calendar.getInstance()); 474 } 475 476 486 public Calendar getAsCalendar(Calendar startTime) { 487 Calendar ret = (Calendar ) startTime.clone(); 488 ret.set(Calendar.YEAR, years); 489 ret.set(Calendar.MONTH, months); 490 ret.set(Calendar.DATE, days); 491 ret.set(Calendar.HOUR, hours); 492 ret.set(Calendar.MINUTE, minutes); 493 ret.set(Calendar.SECOND, (int) seconds); 494 ret.set(Calendar.MILLISECOND, 495 (int) (seconds * 100 - Math.round(seconds) * 100)); 496 return ret; 497 } 498 } 499 | Popular Tags |