1 16 package org.joda.time.field; 17 18 import org.joda.time.DateTimeFieldType; 19 import org.joda.time.DurationField; 20 21 35 public class PreciseDateTimeField extends PreciseDurationDateTimeField { 36 37 private static final long serialVersionUID = -5586801265774496376L; 38 39 40 private final int iRange; 41 42 private final DurationField iRangeField; 43 44 55 public PreciseDateTimeField(DateTimeFieldType type, 56 DurationField unit, DurationField range) { 57 super(type, unit); 58 59 if (!range.isPrecise()) { 60 throw new IllegalArgumentException ("Range duration field must be precise"); 61 } 62 63 long rangeMillis = range.getUnitMillis(); 64 iRange = (int)(rangeMillis / getUnitMillis()); 65 if (iRange < 2) { 66 throw new IllegalArgumentException ("The effective range must be at least 2"); 67 } 68 69 iRangeField = range; 70 } 71 72 78 public int get(long instant) { 79 if (instant >= 0) { 80 return (int) ((instant / getUnitMillis()) % iRange); 81 } else { 82 return iRange - 1 + (int) (((instant + 1) / getUnitMillis()) % iRange); 83 } 84 } 85 86 94 public long addWrapField(long instant, int amount) { 95 int thisValue = get(instant); 96 int wrappedValue = FieldUtils.getWrappedValue 97 (thisValue, amount, getMinimumValue(), getMaximumValue()); 98 return instant + (wrappedValue - thisValue) * getUnitMillis(); 100 } 101 102 110 public long set(long instant, int value) { 111 FieldUtils.verifyValueBounds(this, value, getMinimumValue(), getMaximumValue()); 112 return instant + (value - get(instant)) * iUnitMillis; 113 } 114 115 121 public DurationField getRangeDurationField() { 122 return iRangeField; 123 } 124 125 130 public int getMaximumValue() { 131 return iRange - 1; 132 } 133 134 142 public int getRange() { 143 return iRange; 144 } 145 146 } 147 | Popular Tags |