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 byte[] intToByteArray() {
21     byte[] b = new byte[4];
22     for (int i = 0; i < 4; i++) {
23       int offset = (b.length - 1 - i) * 8;
24       b[i] = (byte) ((value >> offset) & 0xFF);
25     }
26     return b;
27   }
28
29   public int byteArrayToInt(byte [] b) {
30     int val;
31     val = b[0] << 24 + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF);
32     return val;
33   }
34
35   public static int parseInt(String str) {
36     return Integer.parseInt(str, 10);
37   }
38
39   public static int parseInt(String str, int radix) {
40     int value=0;
41     boolean isNeg=false;
42     int start=0;
43     byte[] chars=str.getBytes();
44
45     while(chars[start]==' '||chars[start]=='\t')
46       start++;
47
48     if (chars[start]=='-') {
49       isNeg=true;
50       start++;
51     }
52     boolean cont=true;
53     for(int i=start; cont&&i<str.length(); i++) {
54       byte b=chars[i];
55       int val;
56       if (b>='0'&&b<='9')
57         val=b-'0';
58       else if (b>='a'&&b<='z')
59         val=10+b-'a';
60       else if (b>='A'&&b<='Z')
61         val=10+b-'A';
62       else {
63         cont=false;
64       }
65       if (cont) {
66         if (val>=radix)
67           System.error();
68         value=value*radix+val;
69       }
70     }
71     if (isNeg)
72       value=-value;
73     return value;
74   }
75
76   public String toString() {
77     return String.valueOf(value);
78   }
79
80   public static String toString( int i ) {
81     Integer I = new Integer( i );
82     return I.toString();
83   }
84
85   public int hashCode() {
86     return value;
87   }
88
89   public boolean equals(Object o) {
90     if (o.getType()!=getType())
91       return false;
92     Integer s=(Integer)o;
93     if (s.intValue()!=this.value)
94       return false;
95     return true;
96   }
97 }