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.DurationField; 23 import org.joda.time.DurationFieldType; 24 import org.joda.time.MutablePeriod; 25 import org.joda.time.Period; 26 import org.joda.time.PeriodType; 27 import org.joda.time.ReadableInstant; 28 import org.joda.time.ReadablePartial; 29 import org.joda.time.ReadablePeriod; 30 import org.joda.time.chrono.ISOChronology; 31 import org.joda.time.field.FieldUtils; 32 33 46 public abstract class BaseSingleFieldPeriod 47 implements ReadablePeriod, Comparable , Serializable { 48 49 50 private static final long serialVersionUID = 9386874258972L; 51 52 53 private int iPeriod; 54 55 65 protected static int between(ReadableInstant start, ReadableInstant end, DurationFieldType field) { 66 if (start == null || end == null) { 67 throw new IllegalArgumentException ("ReadableInstant objects must not be null"); 68 } 69 Chronology chrono = DateTimeUtils.getInstantChronology(start); 70 int amount = field.getField(chrono).getDifference(end.getMillis(), start.getMillis()); 71 return amount; 72 } 73 74 87 protected static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) { 88 if (start == null || end == null) { 89 throw new IllegalArgumentException ("ReadablePartial objects must not be null"); 90 } 91 if (start.size() != end.size()) { 92 throw new IllegalArgumentException ("ReadablePartial objects must have the same set of fields"); 93 } 94 for (int i = 0, isize = start.size(); i < isize; i++) { 95 if (start.getFieldType(i) != end.getFieldType(i)) { 96 throw new IllegalArgumentException ("ReadablePartial objects must have the same set of fields"); 97 } 98 } 99 if (DateTimeUtils.isContiguous(start) == false) { 100 throw new IllegalArgumentException ("ReadablePartial objects must be contiguous"); 101 } 102 Chronology chrono = DateTimeUtils.getChronology(start.getChronology()).withUTC(); 103 int[] values = chrono.get(zeroInstance, chrono.set(start, 0L), chrono.set(end, 0L)); 104 return values[0]; 105 } 106 107 127 protected static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) { 128 if (period == null) { 129 return 0; 130 } 131 Chronology iso = ISOChronology.getInstanceUTC(); 132 long duration = 0L; 133 for (int i = 0; i < period.size(); i++) { 134 int value = period.getValue(i); 135 if (value != 0) { 136 DurationField field = period.getFieldType(i).getField(iso); 137 if (field.isPrecise() == false) { 138 throw new IllegalArgumentException ( 139 "Cannot convert period to duration as " + field.getName() + 140 " is not precise in the period " + period); 141 } 142 duration = FieldUtils.safeAdd(duration, FieldUtils.safeMultiply(field.getUnitMillis(), value)); 143 } 144 } 145 return FieldUtils.safeToInt(duration / millisPerUnit); 146 } 147 148 154 protected BaseSingleFieldPeriod(int period) { 155 super(); 156 iPeriod = period; 157 } 158 159 165 protected int getValue() { 166 return iPeriod; 167 } 168 169 175 protected void setValue(int value) { 176 iPeriod = value; 177 } 178 179 185 public abstract DurationFieldType getFieldType(); 186 187 192 public abstract PeriodType getPeriodType(); 193 194 200 public int size() { 201 return 1; 202 } 203 204 214 public DurationFieldType getFieldType(int index) { 215 if (index != 0) { 216 throw new IndexOutOfBoundsException (String.valueOf(index)); 217 } 218 return getFieldType(); 219 } 220 221 230 public int getValue(int index) { 231 if (index != 0) { 232 throw new IndexOutOfBoundsException (String.valueOf(index)); 233 } 234 return getValue(); 235 } 236 237 246 public int get(DurationFieldType type) { 247 if (type == getFieldType()) { 248 return getValue(); 249 } 250 return 0; 251 } 252 253 259 public boolean isSupported(DurationFieldType type) { 260 return (type == getFieldType()); 261 } 262 263 270 public Period toPeriod() { 271 return Period.ZERO.withFields(this); 272 } 273 274 282 public MutablePeriod toMutablePeriod() { 283 MutablePeriod period = new MutablePeriod(); 284 period.add(this); 285 return period; 286 } 287 288 298 public boolean equals(Object period) { 299 if (this == period) { 300 return true; 301 } 302 if (period instanceof ReadablePeriod == false) { 303 return false; 304 } 305 ReadablePeriod other = (ReadablePeriod) period; 306 return (other.getPeriodType() == getPeriodType() && other.getValue(0) == getValue()); 307 } 308 309 314 public int hashCode() { 315 int total = 17; 316 total = 27 * total + getValue(); 317 total = 27 * total + getFieldType().hashCode(); 318 return total; 319 } 320 321 329 public int compareTo(Object other) { 330 if (other.getClass() != getClass()) { 331 throw new ClassCastException (getClass() + " cannot be compared to " + other.getClass()); 332 } 333 int otherValue = ((BaseSingleFieldPeriod) other).getValue(); 334 int thisValue = getValue(); 335 if (thisValue > otherValue) { 336 return 1; 337 } 338 if (thisValue < otherValue) { 339 return -1; 340 } 341 return 0; 342 } 343 344 } 345 | Popular Tags |