1 16 package org.joda.time; 17 18 import java.io.Serializable ; 19 20 import org.joda.time.base.BaseInterval; 21 import org.joda.time.field.FieldUtils; 22 23 50 public class MutableInterval 51 extends BaseInterval 52 implements ReadWritableInterval, Cloneable , Serializable { 53 54 55 private static final long serialVersionUID = -5982824024992428470L; 56 57 61 public MutableInterval() { 62 super(0L, 0L, null); 63 } 64 65 72 public MutableInterval(long startInstant, long endInstant) { 73 super(startInstant, endInstant, null); 74 } 75 76 84 public MutableInterval(long startInstant, long endInstant, Chronology chronology) { 85 super(startInstant, endInstant, chronology); 86 } 87 88 97 public MutableInterval(ReadableInstant start, ReadableInstant end) { 98 super(start, end); 99 } 100 101 109 public MutableInterval(ReadableInstant start, ReadableDuration duration) { 110 super(start, duration); 111 } 112 113 121 public MutableInterval(ReadableDuration duration, ReadableInstant end) { 122 super(duration, end); 123 } 124 125 136 public MutableInterval(ReadableInstant start, ReadablePeriod period) { 137 super(start, period); 138 } 139 140 151 public MutableInterval(ReadablePeriod period, ReadableInstant end) { 152 super(period, end); 153 } 154 155 161 public MutableInterval(Object interval) { 162 super(interval, null); 163 } 164 165 173 public MutableInterval(Object interval, Chronology chronology) { 174 super(interval, chronology); 175 } 176 177 185 public void setInterval(long startInstant, long endInstant) { 186 super.setInterval(startInstant, endInstant, getChronology()); 187 } 188 189 195 public void setInterval(ReadableInterval interval) { 196 if (interval == null) { 197 throw new IllegalArgumentException ("Interval must not be null"); 198 } 199 long startMillis = interval.getStartMillis(); 200 long endMillis = interval.getEndMillis(); 201 Chronology chrono = interval.getChronology(); 202 super.setInterval(startMillis, endMillis, chrono); 203 } 204 205 213 public void setInterval(ReadableInstant start, ReadableInstant end) { 214 if (start == null && end == null) { 215 long now = DateTimeUtils.currentTimeMillis(); 216 setInterval(now, now); 217 } else { 218 long startMillis = DateTimeUtils.getInstantMillis(start); 219 long endMillis = DateTimeUtils.getInstantMillis(end); 220 Chronology chrono = DateTimeUtils.getInstantChronology(start); 221 super.setInterval(startMillis, endMillis, chrono); 222 } 223 } 224 225 231 public void setChronology(Chronology chrono) { 232 super.setInterval(getStartMillis(), getEndMillis(), chrono); 233 } 234 235 242 public void setStartMillis(long startInstant) { 243 super.setInterval(startInstant, getEndMillis(), getChronology()); 244 } 245 246 252 public void setStart(ReadableInstant start) { 253 long startMillis = DateTimeUtils.getInstantMillis(start); 254 super.setInterval(startMillis, getEndMillis(), getChronology()); 255 } 256 257 264 public void setEndMillis(long endInstant) { 265 super.setInterval(getStartMillis(), endInstant, getChronology()); 266 } 267 268 274 public void setEnd(ReadableInstant end) { 275 long endMillis = DateTimeUtils.getInstantMillis(end); 276 super.setInterval(getStartMillis(), endMillis, getChronology()); 277 } 278 279 287 public void setDurationAfterStart(long duration) { 288 setEndMillis(FieldUtils.safeAdd(getStartMillis(), duration)); 289 } 290 291 298 public void setDurationBeforeEnd(long duration) { 299 setStartMillis(FieldUtils.safeAdd(getEndMillis(), -duration)); 300 } 301 302 310 public void setDurationAfterStart(ReadableDuration duration) { 311 long durationMillis = DateTimeUtils.getDurationMillis(duration); 312 setEndMillis(FieldUtils.safeAdd(getStartMillis(), durationMillis)); 313 } 314 315 322 public void setDurationBeforeEnd(ReadableDuration duration) { 323 long durationMillis = DateTimeUtils.getDurationMillis(duration); 324 setStartMillis(FieldUtils.safeAdd(getEndMillis(), -durationMillis)); 325 } 326 327 336 public void setPeriodAfterStart(ReadablePeriod period) { 337 if (period == null) { 338 setEndMillis(getStartMillis()); 339 } else { 340 setEndMillis(getChronology().add(period, getStartMillis(), 1)); 341 } 342 } 343 344 352 public void setPeriodBeforeEnd(ReadablePeriod period) { 353 if (period == null) { 354 setStartMillis(getEndMillis()); 355 } else { 356 setStartMillis(getChronology().add(period, getEndMillis(), -1)); 357 } 358 } 359 360 366 public MutableInterval copy() { 367 return (MutableInterval) clone(); 368 } 369 370 375 public Object clone() { 376 try { 377 return super.clone(); 378 } catch (CloneNotSupportedException ex) { 379 throw new InternalError ("Clone error"); 380 } 381 } 382 383 } 384 | Popular Tags |