1 16 package org.joda.time.field; 17 18 import org.joda.time.DateTimeField; 19 import org.joda.time.DateTimeFieldType; 20 import org.joda.time.DurationField; 21 22 34 public class RemainderDateTimeField extends DecoratedDateTimeField { 35 36 private static final long serialVersionUID = 5708241235177666790L; 37 38 final int iDivisor; 40 final DurationField iRangeField; 41 42 50 public RemainderDateTimeField(DateTimeField field, 51 DateTimeFieldType type, int divisor) { 52 super(field, type); 53 54 if (divisor < 2) { 55 throw new IllegalArgumentException ("The divisor must be at least 2"); 56 } 57 58 DurationField rangeField = field.getDurationField(); 59 if (rangeField == null) { 60 iRangeField = null; 61 } else { 62 iRangeField = new ScaledDurationField( 63 rangeField, type.getRangeDurationType(), divisor); 64 } 65 66 iDivisor = divisor; 67 } 68 69 75 public RemainderDateTimeField(DividedDateTimeField dividedField) { 76 this(dividedField, dividedField.getType()); 77 } 78 79 86 public RemainderDateTimeField(DividedDateTimeField dividedField, DateTimeFieldType type) { 87 super(dividedField.getWrappedField(), type); 88 iDivisor = dividedField.iDivisor; 89 iRangeField = dividedField.iDurationField; 90 } 91 92 99 public int get(long instant) { 100 int value = getWrappedField().get(instant); 101 if (value >= 0) { 102 return value % iDivisor; 103 } else { 104 return (iDivisor - 1) + ((value + 1) % iDivisor); 105 } 106 } 107 108 117 public long addWrapField(long instant, int amount) { 118 return set(instant, FieldUtils.getWrappedValue(get(instant), amount, 0, iDivisor - 1)); 119 } 120 121 129 public long set(long instant, int value) { 130 FieldUtils.verifyValueBounds(this, value, 0, iDivisor - 1); 131 int divided = getDivided(getWrappedField().get(instant)); 132 return getWrappedField().set(instant, divided * iDivisor + value); 133 } 134 135 138 public DurationField getRangeDurationField() { 139 return iRangeField; 140 } 141 142 147 public int getMinimumValue() { 148 return 0; 149 } 150 151 157 public int getMaximumValue() { 158 return iDivisor - 1; 159 } 160 161 public long roundFloor(long instant) { 162 return getWrappedField().roundFloor(instant); 163 } 164 165 public long roundCeiling(long instant) { 166 return getWrappedField().roundCeiling(instant); 167 } 168 169 public long roundHalfFloor(long instant) { 170 return getWrappedField().roundHalfFloor(instant); 171 } 172 173 public long roundHalfCeiling(long instant) { 174 return getWrappedField().roundHalfCeiling(instant); 175 } 176 177 public long roundHalfEven(long instant) { 178 return getWrappedField().roundHalfEven(instant); 179 } 180 181 public long remainder(long instant) { 182 return getWrappedField().remainder(instant); 183 } 184 185 190 public int getDivisor() { 191 return iDivisor; 192 } 193 194 private int getDivided(int value) { 195 if (value >= 0) { 196 return value / iDivisor; 197 } else { 198 return ((value + 1) / iDivisor) - 1; 199 } 200 } 201 202 } 203 | Popular Tags |