changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / ClassLibrary / Integer.java
1 public class Integer {
2   private int value;
3
4   public Integer(int value) {
5     this.value=value;
6   }
7
8   public Integer(String str) {
9     value=Integer.parseInt(str, 10);
10   }
11
12   public int intValue() {
13     return value;
14   }
15
16   public double doubleValue() {
17     return (double)value;
18   }
19
20   public float floatValue() {
21     return (float)value;
22   }
23
24   public byte[] intToByteArray() {
25     byte[] b = new byte[4];
26     for (int i = 0; i < 4; i++) {
27       int offset = (b.length - 1 - i) * 8;
28       b[i] = (byte) ((value >> offset) & 0xFF);
29     }
30     return b;
31   }
32
33   public int byteArrayToInt(byte [] b) {
34     int value = 0;
35     for (int i = 0; i < 4; i++) {
36       int shift = (4 - 1 - i) * 8;
37       value += (b[i] & 0x000000FF) << shift;
38     }
39     return value;
40   }
41
42   /*
43      public int byteArrayToInt(byte [] b) {
44      int val;
45      val = b[0] << 24 + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF);
46      return val;
47      }
48    */
49
50   public static int parseInt(String str) {
51     return Integer.parseInt(str, 10);
52   }
53
54   public static int parseInt(String str, int radix) {
55     int value=0;
56     boolean isNeg=false;
57     int start=0;
58     byte[] chars=str.getBytes();
59
60     while(chars[start]==' '||chars[start]=='\t')
61       start++;
62
63     if (chars[start]=='-') {
64       isNeg=true;
65       start++;
66     }
67     boolean cont=true;
68     for(int i=start; cont&&i<str.length(); i++) {
69       byte b=chars[i];
70       int val;
71       if (b>='0'&&b<='9')
72         val=b-'0';
73       else if (b>='a'&&b<='z')
74         val=10+b-'a';
75       else if (b>='A'&&b<='Z')
76         val=10+b-'A';
77       else {
78         cont=false;
79       }
80       if (cont) {
81         if (val>=radix)
82           System.error();
83         value=value*radix+val;
84       }
85     }
86     if (isNeg)
87       value=-value;
88     return value;
89   }
90
91   public String toString() {
92     return String.valueOf(value);
93   }
94
95   public static String toString(int i) {
96     Integer I = new Integer(i);
97     return I.toString();
98   }
99
100   public int hashCode() {
101     return value;
102   }
103
104   public boolean equals(Object o) {
105     if (o.getType()!=getType())
106       return false;
107     Integer s=(Integer)o;
108     if (s.intValue()!=this.value)
109       return false;
110     return true;
111   }
112
113   public int compareTo(Integer i) {
114     if (value == i.value)
115       return 0;
116     // Returns just -1 or 1 on inequality; doing math might overflow.
117     return value > i.value?1:-1;
118   }
119   
120   public static int bitCount(int x) {
121     // Successively collapse alternating bit groups into a sum.
122     x = ((x >> 1) & 0x55555555) + (x & 0x55555555);
123     x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
124     x = ((x >> 4) & 0x0f0f0f0f) + (x & 0x0f0f0f0f);
125     x = ((x >> 8) & 0x00ff00ff) + (x & 0x00ff00ff);
126     return ((x >> 16) & 0x0000ffff) + (x & 0x0000ffff);
127   }
128   
129   public static int numberOfLeadingZeros(int value) {
130     value |= value >>> 1;
131     value |= value >>> 2;
132     value |= value >>> 4;
133     value |= value >>> 8;
134     value |= value >>> 16;
135     return bitCount(~value);
136   }
137
138   /**
139    * Returns an <code>Integer</code> object wrapping the value.
140    * In contrast to the <code>Integer</code> constructor, this method
141    * will cache some values.  It is used by boxing conversion.
142    *
143    * @param val the value to wrap
144    * @return the <code>Integer</code>
145    */
146   public static Integer valueOf(int val)
147   {
148     //if (val < MIN_CACHE || val > MAX_CACHE)
149       return new Integer(val);
150     /*else
151       return intCache[val - MIN_CACHE];*/
152   }
153 }