1 16 package org.joda.time.base; 17 18 import org.joda.time.DateTime; 19 import org.joda.time.DateTimeUtils; 20 import org.joda.time.Duration; 21 import org.joda.time.Interval; 22 import org.joda.time.MutableInterval; 23 import org.joda.time.Period; 24 import org.joda.time.PeriodType; 25 import org.joda.time.ReadableInstant; 26 import org.joda.time.ReadableInterval; 27 import org.joda.time.field.FieldUtils; 28 import org.joda.time.format.DateTimeFormatter; 29 import org.joda.time.format.ISODateTimeFormat; 30 31 44 public abstract class AbstractInterval implements ReadableInterval { 45 46 49 protected AbstractInterval() { 50 super(); 51 } 52 53 61 protected void checkInterval(long start, long end) { 62 if (end < start) { 63 throw new IllegalArgumentException ("The end instant must be greater or equal to the start"); 64 } 65 } 66 67 73 public DateTime getStart() { 74 return new DateTime(getStartMillis(), getChronology()); 75 } 76 77 82 public DateTime getEnd() { 83 return new DateTime(getEndMillis(), getChronology()); 84 } 85 86 97 public boolean contains(long millisInstant) { 98 long thisStart = getStartMillis(); 99 long thisEnd = getEndMillis(); 100 return (millisInstant >= thisStart && millisInstant < thisEnd); 101 } 102 103 111 public boolean containsNow() { 112 return contains(DateTimeUtils.currentTimeMillis()); 113 } 114 115 137 public boolean contains(ReadableInstant instant) { 138 if (instant == null) { 139 return containsNow(); 140 } 141 return contains(instant.getMillis()); 142 } 143 144 178 public boolean contains(ReadableInterval interval) { 179 if (interval == null) { 180 return containsNow(); 181 } 182 long otherStart = interval.getStartMillis(); 183 long otherEnd = interval.getEndMillis(); 184 long thisStart = getStartMillis(); 185 long thisEnd = getEndMillis(); 186 return (thisStart <= otherStart && otherStart < thisEnd && otherEnd <= thisEnd); 187 } 188 189 230 public boolean overlaps(ReadableInterval interval) { 231 long thisStart = getStartMillis(); 232 long thisEnd = getEndMillis(); 233 if (interval == null) { 234 long now = DateTimeUtils.currentTimeMillis(); 235 return (thisStart < now && now < thisEnd); 236 } else { 237 long otherStart = interval.getStartMillis(); 238 long otherEnd = interval.getEndMillis(); 239 return (thisStart < otherEnd && otherStart < thisEnd); 240 } 241 } 242 243 253 public boolean isBefore(long millisInstant) { 254 return (getEndMillis() <= millisInstant); 255 } 256 257 264 public boolean isBeforeNow() { 265 return isBefore(DateTimeUtils.currentTimeMillis()); 266 } 267 268 276 public boolean isBefore(ReadableInstant instant) { 277 if (instant == null) { 278 return isBeforeNow(); 279 } 280 return isBefore(instant.getMillis()); 281 } 282 283 291 public boolean isBefore(ReadableInterval interval) { 292 if (interval == null) { 293 return isBeforeNow(); 294 } 295 return isBefore(interval.getStartMillis()); 296 } 297 298 308 public boolean isAfter(long millisInstant) { 309 return (getStartMillis() > millisInstant); 310 } 311 312 319 public boolean isAfterNow() { 320 return isAfter(DateTimeUtils.currentTimeMillis()); 321 } 322 323 331 public boolean isAfter(ReadableInstant instant) { 332 if (instant == null) { 333 return isAfterNow(); 334 } 335 return isAfter(instant.getMillis()); 336 } 337 338 347 public boolean isAfter(ReadableInterval interval) { 348 long endMillis; 349 if (interval == null) { 350 endMillis = DateTimeUtils.currentTimeMillis(); 351 } else { 352 endMillis = interval.getEndMillis(); 353 } 354 return (getStartMillis() >= endMillis); 355 } 356 357 363 public Interval toInterval() { 364 return new Interval(getStartMillis(), getEndMillis(), getChronology()); 365 } 366 367 374 public MutableInterval toMutableInterval() { 375 return new MutableInterval(getStartMillis(), getEndMillis(), getChronology()); 376 } 377 378 387 public long toDurationMillis() { 388 return FieldUtils.safeAdd(getEndMillis(), -getStartMillis()); 389 } 390 391 399 public Duration toDuration() { 400 long durMillis = toDurationMillis(); 401 if (durMillis == 0) { 402 return Duration.ZERO; 403 } else { 404 return new Duration(durMillis); 405 } 406 } 407 408 418 public Period toPeriod() { 419 return new Period(getStartMillis(), getEndMillis(), getChronology()); 420 } 421 422 432 public Period toPeriod(PeriodType type) { 433 return new Period(getStartMillis(), getEndMillis(), type, getChronology()); 434 } 435 436 448 public boolean equals(Object readableInterval) { 449 if (this == readableInterval) { 450 return true; 451 } 452 if (readableInterval instanceof ReadableInterval == false) { 453 return false; 454 } 455 ReadableInterval other = (ReadableInterval) readableInterval; 456 return 457 getStartMillis() == other.getStartMillis() && 458 getEndMillis() == other.getEndMillis() && 459 FieldUtils.equals(getChronology(), other.getChronology()); 460 } 461 462 467 public int hashCode() { 468 long start = getStartMillis(); 469 long end = getEndMillis(); 470 int result = 97; 471 result = 31 * result + ((int) (start ^ (start >>> 32))); 472 result = 31 * result + ((int) (end ^ (end >>> 32))); 473 result = 31 * result + getChronology().hashCode(); 474 return result; 475 } 476 477 482 public String toString() { 483 DateTimeFormatter printer = ISODateTimeFormat.dateHourMinuteSecondFraction(); 484 printer = printer.withChronology(getChronology()); 485 StringBuffer buf = new StringBuffer (48); 486 printer.printTo(buf, getStartMillis()); 487 buf.append('/'); 488 printer.printTo(buf, getEndMillis()); 489 return buf.toString(); 490 } 491 492 } 493 | Popular Tags |