1 16 package org.joda.time.base; 17 18 import java.io.Serializable ; 19 20 import org.joda.time.Chronology; 21 import org.joda.time.DateTimeUtils; 22 import org.joda.time.Interval; 23 import org.joda.time.Period; 24 import org.joda.time.PeriodType; 25 import org.joda.time.ReadableDuration; 26 import org.joda.time.ReadableInstant; 27 import org.joda.time.convert.ConverterManager; 28 import org.joda.time.convert.DurationConverter; 29 import org.joda.time.field.FieldUtils; 30 31 45 public abstract class BaseDuration 46 extends AbstractDuration 47 implements ReadableDuration, Serializable { 48 49 50 private static final long serialVersionUID = 2581698638990L; 51 52 53 private long iMillis; 54 55 60 protected BaseDuration(long duration) { 61 super(); 62 iMillis = duration; 63 } 64 65 72 protected BaseDuration(long startInstant, long endInstant) { 73 super(); 74 iMillis = FieldUtils.safeAdd(endInstant, -startInstant); 75 } 76 77 84 protected BaseDuration(ReadableInstant start, ReadableInstant end) { 85 super(); 86 if (start == end) { 87 iMillis = 0L; 88 } else { 89 long startMillis = DateTimeUtils.getInstantMillis(start); 90 long endMillis = DateTimeUtils.getInstantMillis(end); 91 iMillis = FieldUtils.safeAdd(endMillis, -startMillis); 92 } 93 } 94 95 102 protected BaseDuration(Object duration) { 103 super(); 104 DurationConverter converter = ConverterManager.getInstance().getDurationConverter(duration); 105 iMillis = converter.getDurationMillis(duration); 106 } 107 108 114 public long getMillis() { 115 return iMillis; 116 } 117 118 124 protected void setMillis(long duration) { 125 iMillis = duration; 126 } 127 128 143 public Period toPeriod(PeriodType type) { 144 return new Period(getMillis(), type); 145 } 146 147 163 public Period toPeriod(Chronology chrono) { 164 return new Period(getMillis(), chrono); 165 } 166 167 184 public Period toPeriod(PeriodType type, Chronology chrono) { 185 return new Period(getMillis(), type, chrono); 186 } 187 188 199 public Period toPeriodFrom(ReadableInstant startInstant) { 200 return new Period(startInstant, this); 201 } 202 203 215 public Period toPeriodFrom(ReadableInstant startInstant, PeriodType type) { 216 return new Period(startInstant, this, type); 217 } 218 219 231 public Period toPeriodTo(ReadableInstant endInstant) { 232 return new Period(this, endInstant); 233 } 234 235 248 public Period toPeriodTo(ReadableInstant endInstant, PeriodType type) { 249 return new Period(this, endInstant, type); 250 } 251 252 258 public Interval toIntervalFrom(ReadableInstant startInstant) { 259 return new Interval(startInstant, this); 260 } 261 262 268 public Interval toIntervalTo(ReadableInstant endInstant) { 269 return new Interval(this, endInstant); 270 } 271 272 } 273 | Popular Tags |