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 30 public class OffsetDateTimeField extends DecoratedDateTimeField { 31 private static final long serialVersionUID = 3145790132623583142L; 32 33 private final int iOffset; 34 35 private final int iMin; 36 private final int iMax; 37 38 45 public OffsetDateTimeField(DateTimeField field, int offset) { 46 this(field, (field == null ? null : field.getType()), offset, Integer.MIN_VALUE, Integer.MAX_VALUE); 47 } 48 49 57 public OffsetDateTimeField(DateTimeField field, DateTimeFieldType type, int offset) { 58 this(field, type, offset, Integer.MIN_VALUE, Integer.MAX_VALUE); 59 } 60 61 71 public OffsetDateTimeField(DateTimeField field, DateTimeFieldType type, int offset, 72 int minValue, int maxValue) { 73 super(field, type); 74 75 if (offset == 0) { 76 throw new IllegalArgumentException ("The offset cannot be zero"); 77 } 78 79 iOffset = offset; 80 81 if (minValue < (field.getMinimumValue() + offset)) { 82 iMin = field.getMinimumValue() + offset; 83 } else { 84 iMin = minValue; 85 } 86 if (maxValue > (field.getMaximumValue() + offset)) { 87 iMax = field.getMaximumValue() + offset; 88 } else { 89 iMax = maxValue; 90 } 91 } 92 93 99 public int get(long instant) { 100 return super.get(instant) + iOffset; 101 } 102 103 111 public long add(long instant, int amount) { 112 instant = super.add(instant, amount); 113 FieldUtils.verifyValueBounds(this, get(instant), iMin, iMax); 114 return instant; 115 } 116 117 125 public long add(long instant, long amount) { 126 instant = super.add(instant, amount); 127 FieldUtils.verifyValueBounds(this, get(instant), iMin, iMax); 128 return instant; 129 } 130 131 139 public long addWrapField(long instant, int amount) { 140 return set(instant, FieldUtils.getWrappedValue(get(instant), amount, iMin, iMax)); 141 } 142 143 151 public long set(long instant, int value) { 152 FieldUtils.verifyValueBounds(this, value, iMin, iMax); 153 return super.set(instant, value - iOffset); 154 } 155 156 public boolean isLeap(long instant) { 157 return getWrappedField().isLeap(instant); 158 } 159 160 public int getLeapAmount(long instant) { 161 return getWrappedField().getLeapAmount(instant); 162 } 163 164 public DurationField getLeapDurationField() { 165 return getWrappedField().getLeapDurationField(); 166 } 167 168 173 public int getMinimumValue() { 174 return iMin; 175 } 176 177 182 public int getMaximumValue() { 183 return iMax; 184 } 185 186 public long roundFloor(long instant) { 187 return getWrappedField().roundFloor(instant); 188 } 189 190 public long roundCeiling(long instant) { 191 return getWrappedField().roundCeiling(instant); 192 } 193 194 public long roundHalfFloor(long instant) { 195 return getWrappedField().roundHalfFloor(instant); 196 } 197 198 public long roundHalfCeiling(long instant) { 199 return getWrappedField().roundHalfCeiling(instant); 200 } 201 202 public long roundHalfEven(long instant) { 203 return getWrappedField().roundHalfEven(instant); 204 } 205 206 public long remainder(long instant) { 207 return getWrappedField().remainder(instant); 208 } 209 210 215 public int getOffset() { 216 return iOffset; 217 } 218 } 219 | Popular Tags |