KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > groovy > runtime > IntegerMath


1 /*
2  * Created on Mar 5, 2004
3  *
4  */

5 package org.codehaus.groovy.runtime;
6
7 /**
8  * Integer NumberMath operations
9  *
10  * @author Steve Goetze
11  */

12 public class IntegerMath extends NumberMath {
13
14     protected static IntegerMath instance = new IntegerMath();
15
16     private IntegerMath() {}
17                     
18     protected Number JavaDoc absImpl(Number JavaDoc number) {
19         return new Integer JavaDoc(Math.abs(number.intValue()));
20     }
21     
22     protected Number JavaDoc addImpl(Number JavaDoc left, Number JavaDoc right) {
23         return new Integer JavaDoc(left.intValue() + right.intValue());
24     }
25
26     protected Number JavaDoc subtractImpl(Number JavaDoc left, Number JavaDoc right) {
27         return new Integer JavaDoc(left.intValue() - right.intValue());
28     }
29
30     protected Number JavaDoc multiplyImpl(Number JavaDoc left, Number JavaDoc right) {
31         return new Integer JavaDoc(left.intValue() * right.intValue());
32     }
33
34     protected Number JavaDoc divideImpl(Number JavaDoc left, Number JavaDoc right) {
35         return BigDecimalMath.instance.divideImpl(left, right);
36     }
37     
38     protected int compareToImpl(Number JavaDoc left, Number JavaDoc right) {
39         int leftVal = left.intValue();
40         int rightVal = right.intValue();
41         return (leftVal<rightVal ? -1 : (leftVal==rightVal ? 0 : 1));
42     }
43
44     protected Number JavaDoc orImpl(Number JavaDoc left, Number JavaDoc right) {
45         return new Integer JavaDoc(left.intValue() | right.intValue());
46     }
47
48     protected Number JavaDoc andImpl(Number JavaDoc left, Number JavaDoc right) {
49         return new Integer JavaDoc(left.intValue() & right.intValue());
50     }
51
52     protected Number JavaDoc intdivImpl(Number JavaDoc left, Number JavaDoc right) {
53         return new Integer JavaDoc(left.intValue() / right.intValue());
54     }
55     
56     protected Number JavaDoc modImpl(Number JavaDoc left, Number JavaDoc right) {
57         return new Integer JavaDoc(left.intValue() % right.intValue());
58     }
59
60     protected Number JavaDoc negateImpl(Number JavaDoc left) {
61         return new Integer JavaDoc(-left.intValue());
62     }
63
64     protected Number JavaDoc leftShiftImpl(Number JavaDoc left, Number JavaDoc right) {
65         return new Integer JavaDoc(left.intValue() << right.intValue());
66     }
67
68     protected Number JavaDoc rightShiftImpl(Number JavaDoc left, Number JavaDoc right) {
69         return new Integer JavaDoc(left.intValue() >> right.intValue());
70     }
71
72     protected Number JavaDoc rightShiftUnsignedImpl(Number JavaDoc left, Number JavaDoc right) {
73         return new Integer JavaDoc(left.intValue() >>> right.intValue());
74     }
75 }
76
Popular Tags