Duration.java |
1 /** 2 * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 * SOFTWARE. 21 */ 22 23 /* 24 * Copyright (c) 2000, Columbia University. All rights reserved. 25 * 26 * Redistribution and use in source and binary forms, with or without 27 * modification, are permitted provided that the following conditions are met: 28 * 29 * 1. Redistributions of source code must retain the above copyright 30 * notice, this list of conditions and the following disclaimer. 31 * 32 * 2. Redistributions in binary form must reproduce the above copyright 33 * notice, this list of conditions and the following disclaimer in the 34 * documentation and/or other materials provided with the distribution. 35 * 36 * 3. Neither the name of the University nor the names of its contributors 37 * may be used to endorse or promote products derived from this software 38 * without specific prior written permission. 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS 41 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 42 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR 44 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 45 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 46 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 47 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 48 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 49 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 50 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 51 */ 52 53 package com.liferay.portal.kernel.cal; 54 55 import java.io.Serializable; 56 57 /** 58 * <a href="Duration.java.html"><b><i>View Source</i></b></a> 59 * 60 * @author Jonathan Lennox 61 * 62 */ 63 public class Duration implements Cloneable, Serializable { 64 65 /** 66 * Field weeks 67 */ 68 private int weeks; 69 70 /** 71 * Field days 72 */ 73 private int days; 74 75 /** 76 * Field hours 77 */ 78 private int hours; 79 80 /** 81 * Field minutes 82 */ 83 private int minutes; 84 85 /** 86 * Field seconds 87 */ 88 private int seconds; 89 90 /** 91 * Field SECONDS_PER_MINUTE 92 */ 93 private final static int SECONDS_PER_MINUTE = 60; 94 95 /** 96 * Field MINUTES_PER_HOUR 97 */ 98 private final static int MINUTES_PER_HOUR = 60; 99 100 /** 101 * Field HOURS_PER_DAY 102 */ 103 private final static int HOURS_PER_DAY = 24; 104 105 /** 106 * Field DAYS_PER_WEEK 107 */ 108 private final static int DAYS_PER_WEEK = 7; 109 110 /** 111 * Field MILLIS_PER_SECOND 112 */ 113 private final static int MILLIS_PER_SECOND = 1000; 114 115 /** 116 * Field MILLIS_PER_MINUTE 117 */ 118 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE 119 * MILLIS_PER_SECOND; 120 121 /** 122 * Field MILLIS_PER_HOUR 123 */ 124 private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR 125 * MILLIS_PER_MINUTE; 126 127 /** 128 * Field MILLIS_PER_DAY 129 */ 130 private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR; 131 132 /** 133 * Field MILLIS_PER_WEEK 134 */ 135 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY; 136 137 /** 138 * Constructor Duration 139 * 140 * 141 */ 142 public Duration() { 143 144 /* Zero-initialization of all fields happens by default */ 145 146 } 147 148 /** 149 * Constructor Duration 150 * 151 * 152 * @param d 153 * @param h 154 * @param m 155 * @param s 156 * 157 */ 158 public Duration(int d, int h, int m, int s) { 159 days = d; 160 hours = h; 161 minutes = m; 162 seconds = s; 163 } 164 165 /** 166 * Constructor Duration 167 * 168 * 169 * @param h 170 * @param m 171 * @param s 172 * 173 */ 174 public Duration(int h, int m, int s) { 175 this(0, h, m, s); 176 } 177 178 /** 179 * Constructor Duration 180 * 181 * 182 * @param w 183 * 184 */ 185 public Duration(int w) { 186 weeks = w; 187 } 188 189 /** 190 * Method clear 191 * 192 * 193 */ 194 public void clear() { 195 weeks = 0; 196 days = 0; 197 hours = 0; 198 minutes = 0; 199 seconds = 0; 200 } 201 ; 202 203 /** 204 * Method getWeeks 205 * 206 * 207 * @return int 208 * 209 */ 210 public int getWeeks() { 211 return weeks; 212 } 213 214 /** 215 * Method setWeeks 216 * 217 * 218 * @param w 219 * 220 */ 221 public void setWeeks(int w) { 222 if (w < 0) { 223 throw new IllegalArgumentException("Week value out of range"); 224 } 225 226 checkWeeksOkay(w); 227 228 weeks = w; 229 } 230 231 /** 232 * Method getDays 233 * 234 * 235 * @return int 236 * 237 */ 238 public int getDays() { 239 return days; 240 } 241 242 /** 243 * Method setDays 244 * 245 * 246 * @param d 247 * 248 */ 249 public void setDays(int d) { 250 if (d < 0) { 251 throw new IllegalArgumentException("Day value out of range"); 252 } 253 254 checkNonWeeksOkay(d); 255 256 days = d; 257 258 normalize(); 259 } 260 261 /** 262 * Method getHours 263 * 264 * 265 * @return int 266 * 267 */ 268 public int getHours() { 269 return hours; 270 } 271 272 /** 273 * Method setHours 274 * 275 * 276 * @param h 277 * 278 */ 279 public void setHours(int h) { 280 if (h < 0) { 281 throw new IllegalArgumentException("Hour value out of range"); 282 } 283 284 checkNonWeeksOkay(h); 285 286 hours = h; 287 288 normalize(); 289 } 290 291 /** 292 * Method getMinutes 293 * 294 * 295 * @return int 296 * 297 */ 298 public int getMinutes() { 299 return minutes; 300 } 301 302 /** 303 * Method setMinutes 304 * 305 * 306 * @param m 307 * 308 */ 309 public void setMinutes(int m) { 310 if (m < 0) { 311 throw new IllegalArgumentException("Minute value out of range"); 312 } 313 314 checkNonWeeksOkay(m); 315 316 minutes = m; 317 318 normalize(); 319 } 320 321 /** 322 * Method getSeconds 323 * 324 * 325 * @return int 326 * 327 */ 328 public int getSeconds() { 329 return seconds; 330 } 331 332 /** 333 * Method setSeconds 334 * 335 * 336 * @param s 337 * 338 */ 339 public void setSeconds(int s) { 340 if (s < 0) { 341 throw new IllegalArgumentException("Second value out of range"); 342 } 343 344 checkNonWeeksOkay(s); 345 346 seconds = s; 347 348 normalize(); 349 } 350 351 /** 352 * Method getInterval 353 * 354 * 355 * @return long 356 * 357 */ 358 public long getInterval() { 359 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE 360 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY 361 + weeks * MILLIS_PER_WEEK; 362 } 363 364 /** 365 * Method setInterval 366 * 367 * 368 * @param millis 369 * 370 */ 371 public void setInterval(long millis) { 372 if (millis < 0) { 373 throw new IllegalArgumentException("Negative-length interval"); 374 } 375 376 clear(); 377 378 days = (int)(millis / MILLIS_PER_DAY); 379 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND); 380 381 normalize(); 382 } 383 384 /** 385 * Method normalize 386 * 387 * 388 */ 389 protected void normalize() { 390 minutes += seconds / SECONDS_PER_MINUTE; 391 seconds %= SECONDS_PER_MINUTE; 392 hours += minutes / MINUTES_PER_HOUR; 393 minutes %= MINUTES_PER_HOUR; 394 days += hours / HOURS_PER_DAY; 395 hours %= HOURS_PER_DAY; 396 } 397 398 /** 399 * Method checkWeeksOkay 400 * 401 * 402 * @param f 403 * 404 */ 405 protected void checkWeeksOkay(int f) { 406 if ((f != 0) 407 && ((days != 0) || (hours != 0) || (minutes != 0) 408 || (seconds != 0))) { 409 throw new IllegalStateException( 410 "Weeks and non-weeks are incompatible"); 411 } 412 } 413 414 /** 415 * Method checkNonWeeksOkay 416 * 417 * 418 * @param f 419 * 420 */ 421 protected void checkNonWeeksOkay(int f) { 422 if ((f != 0) && (weeks != 0)) { 423 throw new IllegalStateException( 424 "Weeks and non-weeks are incompatible"); 425 } 426 } 427 428 /** 429 * Method clone 430 * 431 * 432 * @return Object 433 * 434 */ 435 public Object clone() { 436 try { 437 Duration other = (Duration)super.clone(); 438 439 other.weeks = weeks; 440 other.days = days; 441 other.hours = hours; 442 other.minutes = minutes; 443 other.seconds = seconds; 444 445 return other; 446 } 447 catch (CloneNotSupportedException e) { 448 throw new InternalError(); 449 } 450 } 451 452 /** 453 * Method toString 454 * 455 * 456 * @return String 457 * 458 */ 459 public String toString() { 460 StringBuilder sb = new StringBuilder(); 461 462 sb.append(getClass().getName()); 463 sb.append("[weeks="); 464 sb.append(weeks); 465 sb.append(",days="); 466 sb.append(days); 467 sb.append(",hours="); 468 sb.append(hours); 469 sb.append(",minutes="); 470 sb.append(minutes); 471 sb.append(",seconds="); 472 sb.append(seconds); 473 sb.append("]"); 474 475 return sb.toString(); 476 } 477 478 }