1 55 56 package org.jboss.axis.types; 57 58 import org.jboss.axis.utils.Messages; 59 60 61 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 80 public Duration() 81 { 82 } 83 84 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 110 public Duration(String duration) throws IllegalArgumentException 111 { 112 int position = 1; 113 int timePosition = duration.indexOf("T"); 114 115 if (duration.indexOf("P") == -1) 116 throw new IllegalArgumentException (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 137 public void parseTime(String 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 167 public void parseDate(String 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 195 public boolean isNegative() 196 { 197 return isNegative; 198 } 199 200 203 public int getYears() 204 { 205 return years; 206 } 207 208 211 public int getMonths() 212 { 213 return months; 214 } 215 216 219 public int getDays() 220 { 221 return days; 222 } 223 224 227 public int getHours() 228 { 229 return hours; 230 } 231 232 235 public int getMinutes() 236 { 237 return minutes; 238 } 239 240 243 public double getSeconds() 244 { 245 return seconds; 246 } 247 248 251 public void setNegative(boolean negative) 252 { 253 isNegative = negative; 254 } 255 256 259 public void setYears(int years) 260 { 261 this.years = years; 262 } 263 264 267 public void setMonths(int months) 268 { 269 this.months = months; 270 } 271 272 275 public void setDays(int days) 276 { 277 this.days = days; 278 } 279 280 283 public void setHours(int hours) 284 { 285 this.hours = hours; 286 } 287 288 291 public void setMinutes(int minutes) 292 { 293 this.minutes = minutes; 294 } 295 296 299 public void setSeconds(int seconds) 300 { 301 this.seconds = seconds; 302 } 303 304 307 public String toString() 308 { 309 StringBuffer duration = new StringBuffer (); 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 358 public boolean equals(Object 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 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 |