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.IllegalFieldValueException; 21 22 30 public class FieldUtils { 31 32 35 private FieldUtils() { 36 super(); 37 } 38 39 48 public static int safeNegate(int value) { 49 if (value == Integer.MIN_VALUE) { 50 throw new ArithmeticException ("Integer.MIN_VALUE cannot be negated"); 51 } 52 return -value; 53 } 54 55 63 public static int safeAdd(int val1, int val2) { 64 int sum = val1 + val2; 65 if ((val1 ^ sum) < 0 && (val1 ^ val2) >= 0) { 67 throw new ArithmeticException 68 ("The calculation caused an overflow: " + val1 + " + " + val2); 69 } 70 return sum; 71 } 72 73 81 public static long safeAdd(long val1, long val2) { 82 long sum = val1 + val2; 83 if ((val1 ^ sum) < 0 && (val1 ^ val2) >= 0) { 85 throw new ArithmeticException 86 ("The calculation caused an overflow: " + val1 + " + " + val2); 87 } 88 return sum; 89 } 90 91 99 public static long safeSubtract(long val1, long val2) { 100 long diff = val1 - val2; 101 if ((val1 ^ diff) < 0 && (val1 ^ val2) < 0) { 103 throw new ArithmeticException 104 ("The calculation caused an overflow: " + val1 + " - " + val2); 105 } 106 return diff; 107 } 108 109 118 public static int safeMultiply(int val1, int val2) { 119 long total = (long) val1 * (long) val2; 120 if (total < Integer.MIN_VALUE || total > Integer.MAX_VALUE) { 121 throw new ArithmeticException 122 ("The calculation caused an overflow: " + val1 + " * " + val2); 123 } 124 return (int) total; 125 } 126 127 136 public static long safeMultiply(long val1, int scalar) { 137 switch (scalar) { 138 case -1: 139 return -val1; 140 case 0: 141 return 0L; 142 case 1: 143 return val1; 144 } 145 long total = val1 * scalar; 146 if (total / scalar != val1) { 147 throw new ArithmeticException 148 ("The calculation caused an overflow: " + val1 + " * " + scalar); 149 } 150 return total; 151 } 152 153 161 public static long safeMultiply(long val1, long val2) { 162 if (val2 == 1) { 163 return val1; 164 } 165 if (val2 == 0) { 166 return 0; 167 } 168 long total = val1 * val2; 169 if (total / val2 != val1) { 170 throw new ArithmeticException 171 ("The calculation caused an overflow: " + val1 + " * " + val2); 172 } 173 return total; 174 } 175 176 183 public static int safeToInt(long value) { 184 if (Integer.MIN_VALUE <= value && value <= Integer.MAX_VALUE) { 185 return (int) value; 186 } 187 throw new ArithmeticException ("Value cannot fit in an int: " + value); 188 } 189 190 198 public static int safeMultiplyToInt(long val1, long val2) { 199 long val = FieldUtils.safeMultiply(val1, val2); 200 return FieldUtils.safeToInt(val); 201 } 202 203 212 public static void verifyValueBounds(DateTimeField field, 213 int value, int lowerBound, int upperBound) { 214 if ((value < lowerBound) || (value > upperBound)) { 215 throw new IllegalFieldValueException 216 (field.getType(), new Integer (value), 217 new Integer (lowerBound), new Integer (upperBound)); 218 } 219 } 220 221 230 public static void verifyValueBounds(DateTimeFieldType fieldType, 231 int value, int lowerBound, int upperBound) { 232 if ((value < lowerBound) || (value > upperBound)) { 233 throw new IllegalFieldValueException 234 (fieldType, new Integer (value), 235 new Integer (lowerBound), new Integer (upperBound)); 236 } 237 } 238 239 247 public static void verifyValueBounds(String fieldName, 248 int value, int lowerBound, int upperBound) { 249 if ((value < lowerBound) || (value > upperBound)) { 250 throw new IllegalFieldValueException 251 (fieldName, new Integer (value), 252 new Integer (lowerBound), new Integer (upperBound)); 253 } 254 } 255 256 271 public static int getWrappedValue(int currentValue, int wrapValue, 272 int minValue, int maxValue) { 273 return getWrappedValue(currentValue + wrapValue, minValue, maxValue); 274 } 275 276 288 public static int getWrappedValue(int value, int minValue, int maxValue) { 289 if (minValue >= maxValue) { 290 throw new IllegalArgumentException ("MIN > MAX"); 291 } 292 293 int wrapRange = maxValue - minValue + 1; 294 value -= minValue; 295 296 if (value >= 0) { 297 return (value % wrapRange) + minValue; 298 } 299 300 int remByRange = (-value) % wrapRange; 301 302 if (remByRange == 0) { 303 return 0 + minValue; 304 } 305 return (wrapRange - remByRange) + minValue; 306 } 307 308 317 public static boolean equals(Object object1, Object object2) { 318 if (object1 == object2) { 319 return true; 320 } 321 if (object1 == null || object2 == null) { 322 return false; 323 } 324 return object1.equals(object2); 325 } 326 327 } 328 | Popular Tags |