1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.util;
17
18 import java.math.BigDecimal;
19 import java.math.BigInteger;
20 import java.math.MathContext;
21 import java.math.RoundingMode;
22
23 public class KualiInteger extends Number implements Comparable {
24 public static final int ROUND_BEHAVIOR = KualiDecimal.ROUND_BEHAVIOR;
25 public static final int SCALE = 0;
26
27 public static KualiInteger ZERO = new KualiInteger(0);
28
29 private final BigInteger value;
30
31
32
33
34
35
36
37
38
39 public KualiInteger(String value) {
40 if (value == null) {
41 throw new IllegalArgumentException("invalid (null) String in KualiInteger constructor");
42 }
43
44 this.value = new BigInteger(value);
45 }
46
47
48
49
50 public KualiInteger(long value) {
51 this.value = BigInteger.valueOf(value);
52 }
53
54
55
56
57
58
59
60 public KualiInteger(BigInteger value) {
61 if (value == null) {
62 throw new IllegalArgumentException("invalid (null) BigDecimal in KualiDecimal constructor");
63 }
64
65 this.value = value;
66 }
67
68
69
70
71
72
73
74 public KualiInteger(BigDecimal value) {
75 if (value == null) {
76 throw new IllegalArgumentException("invalid (null) BigDecimal in KualiDecimal constructor");
77 }
78
79 this.value = value.setScale(SCALE, ROUND_BEHAVIOR).toBigInteger();
80 }
81
82
83
84
85
86
87
88
89 public KualiInteger(KualiDecimal value, RoundingMode roundingMode) {
90 if (value == null) {
91 throw new IllegalArgumentException("invalid (null) KualiDecimal in KualiInteger constructor");
92 }
93 if (roundingMode == null) {
94 throw new IllegalArgumentException("invalid (null) RoundingMode in KualiInteger constructor");
95 }
96
97 this.value = value.bigDecimalValue().round(new MathContext(0, roundingMode)).toBigInteger();
98 }
99
100
101
102
103
104
105
106
107
108 public KualiInteger add(KualiInteger addend) {
109 if (addend == null) {
110 throw new IllegalArgumentException("invalid (null) addend");
111 }
112
113 BigInteger sum = this.value.add(addend.value);
114 return new KualiInteger(sum);
115 }
116
117
118
119
120
121
122
123
124
125 public KualiInteger subtract(KualiInteger subtrahend) {
126 if (subtrahend == null) {
127 throw new IllegalArgumentException("invalid (null) subtrahend");
128 }
129
130 BigInteger difference = this.value.subtract(subtrahend.value);
131 return new KualiInteger(difference);
132 }
133
134
135
136
137
138
139
140
141
142 public KualiInteger multiply(KualiInteger multiplier) {
143 if (multiplier == null) {
144 throw new IllegalArgumentException("invalid (null) multiplier");
145 }
146
147 BigInteger product = this.value.multiply(multiplier.value);
148 return new KualiInteger(product);
149 }
150
151 public KualiInteger multiply(BigDecimal multiplier) {
152 if (multiplier == null) {
153 throw new IllegalArgumentException("invalid (null) multiplier");
154 }
155
156 BigDecimal product = multiplier.multiply(new BigDecimal(this.value));
157 return new KualiInteger(product);
158 }
159
160 public KualiInteger multiply(KualiDecimal multiplier) {
161 return multiply(multiplier.bigDecimalValue());
162 }
163
164
165 public BigDecimal divide(BigDecimal dividend) {
166 if (dividend == null) {
167 throw new IllegalArgumentException("invalid (null) dividend");
168 }
169
170 return this.bigDecimalValue().divide(dividend, 8, ROUND_BEHAVIOR);
171 }
172
173 public BigDecimal divide(KualiInteger dividend) {
174 if (dividend == null) {
175 throw new IllegalArgumentException("invalid (null) dividend");
176 }
177
178 return divide(dividend.bigDecimalValue());
179 }
180
181
182
183
184
185 public double doubleValue() {
186 return this.value.doubleValue();
187 }
188
189
190
191
192 public float floatValue() {
193 return this.value.floatValue();
194 }
195
196
197
198
199 public int intValue() {
200 return this.value.intValue();
201 }
202
203
204
205
206 public long longValue() {
207 return this.value.longValue();
208 }
209
210
211
212
213 public BigInteger bigIntegerValue() {
214 return this.value;
215 }
216
217
218
219
220 public BigDecimal bigDecimalValue() {
221 return new BigDecimal(this.value);
222 }
223
224
225
226
227 public KualiDecimal kualiDecimalValue() {
228 return new KualiDecimal(this.bigDecimalValue());
229 }
230
231
232
233
234
235
236 public boolean isLessThan(KualiInteger operand) {
237 if (operand == null) {
238 throw new IllegalArgumentException("invalid (null) operand");
239 }
240
241 return (this.compareTo(operand) == -1);
242 }
243
244
245
246
247
248 public boolean isGreaterThan(KualiInteger operand) {
249 if (operand == null) {
250 throw new IllegalArgumentException("invalid (null) operand");
251 }
252
253 return (this.compareTo(operand) == 1);
254 }
255
256
257
258
259
260 public boolean isLessEqual(KualiInteger operand) {
261 if (operand == null) {
262 throw new IllegalArgumentException("invalid (null) operand");
263 }
264
265 return !isGreaterThan(operand);
266 }
267
268
269
270
271
272 public boolean isGreaterEqual(KualiInteger operand) {
273 if (operand == null) {
274 throw new IllegalArgumentException("invalid (null) operand");
275 }
276
277 return !isLessThan(operand);
278 }
279
280
281
282
283 public boolean isNegative() {
284 return (this.compareTo(ZERO) == -1);
285 }
286
287
288
289
290 public boolean isPositive() {
291 return (this.compareTo(ZERO) == 1);
292 }
293
294
295
296
297
298 public boolean isZero() {
299 return (this.compareTo(ZERO) == 0);
300 }
301
302
303
304
305
306 public boolean isNonZero() {
307 return !this.isZero();
308 }
309
310
311
312
313 public KualiInteger negated() {
314 return multiply(new KualiInteger("-1"));
315 }
316
317
318
319
320
321
322
323
324
325
326
327 public int compareTo(Object o) {
328 return compareTo((KualiInteger) o);
329 }
330
331
332
333
334
335
336 public int compareTo(KualiInteger k) {
337 return this.value.compareTo(k.value);
338 }
339
340
341
342
343
344
345 public boolean equals(Object obj) {
346 boolean equals = false;
347
348 if (obj instanceof KualiInteger) {
349 KualiInteger k = (KualiInteger) obj;
350
351
352
353 equals = (this.compareTo(k) == 0);
354 }
355
356 return equals;
357 }
358
359
360
361
362
363 public int hashCode() {
364 return this.value.hashCode();
365 }
366
367
368
369
370 public String toString() {
371 return this.value.toString();
372 }
373
374 }