1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
23
24
25
26
27
28
29
30 public class FieldUtils {
31
32
33
34
35 private FieldUtils() {
36 super();
37 }
38
39
40
41
42
43
44
45
46
47
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
56
57
58
59
60
61
62
63 public static int safeAdd(int val1, int val2) {
64 int sum = val1 + val2;
65
66 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
74
75
76
77
78
79
80
81 public static long safeAdd(long val1, long val2) {
82 long sum = val1 + val2;
83
84 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
92
93
94
95
96
97
98
99 public static long safeSubtract(long val1, long val2) {
100 long diff = val1 - val2;
101
102 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
110
111
112
113
114
115
116
117
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("Multiplication overflows an int: " + val1 + " * " + val2);
122 }
123 return (int) total;
124 }
125
126
127
128
129
130
131
132
133
134
135 public static long safeMultiply(long val1, int val2) {
136 switch (val2) {
137 case -1:
138 if (val1 == Long.MIN_VALUE) {
139 throw new ArithmeticException("Multiplication overflows a long: " + val1 + " * " + val2);
140 }
141 return -val1;
142 case 0:
143 return 0L;
144 case 1:
145 return val1;
146 }
147 long total = val1 * val2;
148 if (total / val2 != val1) {
149 throw new ArithmeticException("Multiplication overflows a long: " + val1 + " * " + val2);
150 }
151 return total;
152 }
153
154
155
156
157
158
159
160
161
162 public static long safeMultiply(long val1, long val2) {
163 if (val2 == 1) {
164 return val1;
165 }
166 if (val1 == 1) {
167 return val2;
168 }
169 if (val1 == 0 || val2 == 0) {
170 return 0;
171 }
172 long total = val1 * val2;
173 if (total / val2 != val1 || val1 == Long.MIN_VALUE && val2 == -1 || val2 == Long.MIN_VALUE && val1 == -1) {
174 throw new ArithmeticException("Multiplication overflows a long: " + val1 + " * " + val2);
175 }
176 return total;
177 }
178
179
180
181
182
183
184
185
186 public static int safeToInt(long value) {
187 if (Integer.MIN_VALUE <= value && value <= Integer.MAX_VALUE) {
188 return (int) value;
189 }
190 throw new ArithmeticException("Value cannot fit in an int: " + value);
191 }
192
193
194
195
196
197
198
199
200
201 public static int safeMultiplyToInt(long val1, long val2) {
202 long val = FieldUtils.safeMultiply(val1, val2);
203 return FieldUtils.safeToInt(val);
204 }
205
206
207
208
209
210
211
212
213
214
215 public static void verifyValueBounds(DateTimeField field,
216 int value, int lowerBound, int upperBound) {
217 if ((value < lowerBound) || (value > upperBound)) {
218 throw new IllegalFieldValueException
219 (field.getType(), Integer.valueOf(value),
220 Integer.valueOf(lowerBound), Integer.valueOf(upperBound));
221 }
222 }
223
224
225
226
227
228
229
230
231
232
233 public static void verifyValueBounds(DateTimeFieldType fieldType,
234 int value, int lowerBound, int upperBound) {
235 if ((value < lowerBound) || (value > upperBound)) {
236 throw new IllegalFieldValueException
237 (fieldType, Integer.valueOf(value),
238 Integer.valueOf(lowerBound), Integer.valueOf(upperBound));
239 }
240 }
241
242
243
244
245
246
247
248
249
250 public static void verifyValueBounds(String fieldName,
251 int value, int lowerBound, int upperBound) {
252 if ((value < lowerBound) || (value > upperBound)) {
253 throw new IllegalFieldValueException
254 (fieldName, Integer.valueOf(value),
255 Integer.valueOf(lowerBound), Integer.valueOf(upperBound));
256 }
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 public static int getWrappedValue(int currentValue, int wrapValue,
275 int minValue, int maxValue) {
276 return getWrappedValue(currentValue + wrapValue, minValue, maxValue);
277 }
278
279
280
281
282
283
284
285
286
287
288
289
290
291 public static int getWrappedValue(int value, int minValue, int maxValue) {
292 if (minValue >= maxValue) {
293 throw new IllegalArgumentException("MIN > MAX");
294 }
295
296 int wrapRange = maxValue - minValue + 1;
297 value -= minValue;
298
299 if (value >= 0) {
300 return (value % wrapRange) + minValue;
301 }
302
303 int remByRange = (-value) % wrapRange;
304
305 if (remByRange == 0) {
306 return 0 + minValue;
307 }
308 return (wrapRange - remByRange) + minValue;
309 }
310
311
312
313
314
315
316
317
318
319
320 public static boolean equals(Object object1, Object object2) {
321 if (object1 == object2) {
322 return true;
323 }
324 if (object1 == null || object2 == null) {
325 return false;
326 }
327 return object1.equals(object2);
328 }
329
330 }