changes.
[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 }