1 16 package org.joda.time; 17 18 import java.io.Serializable ; 19 20 import org.joda.time.base.BaseInterval; 21 22 47 public final class Interval 48 extends BaseInterval 49 implements ReadableInterval, Serializable { 50 51 52 private static final long serialVersionUID = 4922451897541386752L; 53 54 62 public Interval(long startInstant, long endInstant) { 63 super(startInstant, endInstant, null); 64 } 65 66 74 public Interval(long startInstant, long endInstant, Chronology chronology) { 75 super(startInstant, endInstant, chronology); 76 } 77 78 87 public Interval(ReadableInstant start, ReadableInstant end) { 88 super(start, end); 89 } 90 91 99 public Interval(ReadableInstant start, ReadableDuration duration) { 100 super(start, duration); 101 } 102 103 111 public Interval(ReadableDuration duration, ReadableInstant end) { 112 super(duration, end); 113 } 114 115 126 public Interval(ReadableInstant start, ReadablePeriod period) { 127 super(start, period); 128 } 129 130 141 public Interval(ReadablePeriod period, ReadableInstant end) { 142 super(period, end); 143 } 144 145 151 public Interval(Object interval) { 152 super(interval, null); 153 } 154 155 163 public Interval(Object interval, Chronology chronology) { 164 super(interval, chronology); 165 } 166 167 174 public Interval toInterval() { 175 return this; 176 } 177 178 202 public Interval overlap(ReadableInterval interval) { 203 interval = DateTimeUtils.getReadableInterval(interval); 204 if (overlaps(interval) == false) { 205 return null; 206 } 207 long start = Math.max(getStartMillis(), interval.getStartMillis()); 208 long end = Math.min(getEndMillis(), interval.getEndMillis()); 209 return new Interval(start, end, getChronology()); 210 } 211 212 237 public Interval gap(ReadableInterval interval) { 238 interval = DateTimeUtils.getReadableInterval(interval); 239 long otherStart = interval.getStartMillis(); 240 long otherEnd = interval.getEndMillis(); 241 long thisStart = getStartMillis(); 242 long thisEnd = getEndMillis(); 243 if (thisStart > otherEnd) { 244 return new Interval(otherEnd, thisStart, getChronology()); 245 } else if (otherStart > thisEnd) { 246 return new Interval(thisEnd, otherStart, getChronology()); 247 } else { 248 return null; 249 } 250 } 251 252 289 public boolean abuts(ReadableInterval interval) { 290 if (interval == null) { 291 long now = DateTimeUtils.currentTimeMillis(); 292 return (getStartMillis() == now || getEndMillis() == now); 293 } else { 294 return (interval.getEndMillis() == getStartMillis() || 295 getEndMillis() == interval.getStartMillis()); 296 } 297 } 298 299 306 public Interval withChronology(Chronology chronology) { 307 if (getChronology() == chronology) { 308 return this; 309 } 310 return new Interval(getStartMillis(), getEndMillis(), chronology); 311 } 312 313 320 public Interval withStartMillis(long startInstant) { 321 if (startInstant == getStartMillis()) { 322 return this; 323 } 324 return new Interval(startInstant, getEndMillis(), getChronology()); 325 } 326 327 334 public Interval withStart(ReadableInstant start) { 335 long startMillis = DateTimeUtils.getInstantMillis(start); 336 return withStartMillis(startMillis); 337 } 338 339 346 public Interval withEndMillis(long endInstant) { 347 if (endInstant == getEndMillis()) { 348 return this; 349 } 350 return new Interval(getStartMillis(), endInstant, getChronology()); 351 } 352 353 360 public Interval withEnd(ReadableInstant end) { 361 long endMillis = DateTimeUtils.getInstantMillis(end); 362 return withEndMillis(endMillis); 363 } 364 365 373 public Interval withDurationAfterStart(ReadableDuration duration) { 374 long durationMillis = DateTimeUtils.getDurationMillis(duration); 375 if (durationMillis == toDurationMillis()) { 376 return this; 377 } 378 Chronology chrono = getChronology(); 379 long startMillis = getStartMillis(); 380 long endMillis = chrono.add(startMillis, durationMillis, 1); 381 return new Interval(startMillis, endMillis, chrono); 382 } 383 384 391 public Interval withDurationBeforeEnd(ReadableDuration duration) { 392 long durationMillis = DateTimeUtils.getDurationMillis(duration); 393 if (durationMillis == toDurationMillis()) { 394 return this; 395 } 396 Chronology chrono = getChronology(); 397 long endMillis = getEndMillis(); 398 long startMillis = chrono.add(endMillis, durationMillis, -1); 399 return new Interval(startMillis, endMillis, chrono); 400 } 401 402 410 public Interval withPeriodAfterStart(ReadablePeriod period) { 411 if (period == null) { 412 return withDurationAfterStart(null); 413 } 414 Chronology chrono = getChronology(); 415 long startMillis = getStartMillis(); 416 long endMillis = chrono.add(period, startMillis, 1); 417 return new Interval(startMillis, endMillis, chrono); 418 } 419 420 427 public Interval withPeriodBeforeEnd(ReadablePeriod period) { 428 if (period == null) { 429 return withDurationBeforeEnd(null); 430 } 431 Chronology chrono = getChronology(); 432 long endMillis = getEndMillis(); 433 long startMillis = chrono.add(period, endMillis, -1); 434 return new Interval(startMillis, endMillis, chrono); 435 } 436 437 } 438 | Popular Tags |