Initial revision
[gcc.git] / libjava / java / lang / Integer.java
1 /* Copyright (C) 1998, 1999 Cygnus Solutions
2
3 This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
8
9 package java.lang;
10
11 /**
12 * @author Warren Levy <warrenl@cygnus.com>
13 * @date September 11, 1998.
14 */
15 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
16 * "The Java Language Specification", ISBN 0-201-63451-1
17 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
18 * Status: Believed complete and correct.
19 */
20
21 public final class Integer extends Number implements Comparable
22 {
23 public static final int MAX_VALUE = 0x7FFFFFFF;
24 public static final int MIN_VALUE = 0x80000000;
25
26 // This initialization is seemingly circular, but it is accepted
27 // by javac, and is handled specially by gcc.
28 public static final Class TYPE = int.class;
29
30 /* The int value of the instance. */
31 private int value;
32
33 public Integer(int val)
34 {
35 value = val;
36 }
37
38 public Integer(String str) throws NumberFormatException
39 {
40 value = parseInt(str, 10);
41 }
42
43 public byte byteValue()
44 {
45 return (byte) value;
46 }
47
48 public double doubleValue()
49 {
50 return (double) value;
51 }
52
53 public float floatValue()
54 {
55 return (float) value;
56 }
57
58 public int intValue()
59 {
60 return value;
61 }
62
63 public long longValue()
64 {
65 return value;
66 }
67
68 public short shortValue()
69 {
70 return (short) value;
71 }
72
73 // Added in JDK 1.2
74 public int compareTo(Integer anotherInteger)
75 {
76 if (this.value == anotherInteger.value)
77 return 0;
78
79 // Returns just -1 or 1 on inequality; doing math might overflow the int.
80 if (this.value > anotherInteger.value)
81 return 1;
82
83 return -1;
84 }
85
86 // Added in JDK 1.2
87 public int compareTo(Object o) throws ClassCastException
88 {
89 if (!(o instanceof Integer))
90 throw new ClassCastException();
91
92 return this.compareTo((Integer) o);
93 }
94
95 public static Integer decode(String str) throws NumberFormatException
96 {
97 boolean isNeg = false;
98 int index = 0;
99 int radix = 10;
100 final int len;
101
102 if (str == null || (len = str.length()) == 0)
103 throw new NumberFormatException();
104
105 // Negative numbers are always radix 10.
106 if (str.charAt(0) == '-')
107 {
108 radix = 10;
109 index++;
110 isNeg = true;
111 }
112 else if (str.charAt(index) == '#')
113 {
114 radix = 16;
115 index++;
116 }
117 else if (str.charAt(index) == '0')
118 {
119 // Check if str is just "0"
120 if (len == 1)
121 return new Integer(0);
122
123 index++;
124 if (str.charAt(index) == 'x')
125 {
126 radix = 16;
127 index++;
128 }
129 else
130 radix = 8;
131 }
132
133 if (index >= len)
134 throw new NumberFormatException();
135
136 return new Integer(parseInt(str, index, len, isNeg, radix));
137 }
138
139 public boolean equals(Object obj)
140 {
141 return (obj != null && (obj instanceof Integer)
142 && ((Integer) obj).value == value);
143 }
144
145 public static Integer getInteger(String prop)
146 {
147 return getInteger(prop, null);
148 }
149
150 public static Integer getInteger(String prop, int defval)
151 {
152 Integer val = getInteger(prop, null);
153 return val == null ? new Integer(defval) : val;
154 }
155
156 public static Integer getInteger(String prop, Integer defobj)
157 {
158 try
159 {
160 return decode(System.getProperty(prop));
161 }
162 catch (NumberFormatException ex)
163 {
164 return defobj;
165 }
166 }
167
168 public int hashCode()
169 {
170 return value;
171 }
172
173 public static int parseInt(String str) throws NumberFormatException
174 {
175 return parseInt(str, 10);
176 }
177
178 public static int parseInt(String str, int radix) throws NumberFormatException
179 {
180 final int len;
181
182 if (str == null || (len = str.length()) == 0 ||
183 radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
184 throw new NumberFormatException();
185
186 boolean isNeg = false;
187 int index = 0;
188 if (str.charAt(index) == '-')
189 if (len > 1)
190 {
191 isNeg = true;
192 index++;
193 }
194 else
195 throw new NumberFormatException();
196
197 return parseInt(str, index, len, isNeg, radix);
198 }
199
200 private static int parseInt(String str, int index, int len, boolean isNeg,
201 int radix) throws NumberFormatException
202 {
203 int val = 0;
204 int digval;
205
206 for ( ; index < len; index++)
207 {
208 // The the previous loop iteration left us with a negative
209 // value (which can only be the most negative value, but we
210 // don't check that), then having more digits is wrong.
211 if (val == MIN_VALUE)
212 throw new NumberFormatException();
213
214 if ((digval = Character.digit(str.charAt(index), radix)) < 0)
215 throw new NumberFormatException();
216
217 // Throw an exception for overflow if result is negative.
218 // However, we special-case the most negative value.
219 val *= radix;
220 if (val < 0 || val + digval < 0)
221 {
222 if (isNeg && val + digval == MIN_VALUE)
223 {
224 // Ok.
225 }
226 else
227 throw new NumberFormatException();
228 }
229 val += digval;
230 }
231
232 return isNeg ? -(val) : val;
233 }
234
235 public static String toBinaryString(int num)
236 {
237 return toUnsignedString(num, 1);
238 }
239
240 public static String toHexString(int num)
241 {
242 return toUnsignedString(num, 4);
243 }
244
245 public static String toOctalString(int num)
246 {
247 return toUnsignedString(num, 3);
248 }
249
250 private static String toUnsignedString(int num, int exp)
251 {
252 // Use an array large enough for a binary number.
253 int radix = 1 << exp;
254 int mask = radix - 1;
255 char[] buffer = new char[32];
256 int i = 32;
257 do
258 {
259 buffer[--i] = Character.forDigit(num & mask, radix);
260 num = num >>> exp;
261 }
262 while (num != 0);
263
264 return String.valueOf(buffer, i, 32-i);
265 }
266
267 public String toString()
268 {
269 return toString(this.value);
270 }
271
272 public static String toString(int num)
273 {
274 // Use an arrary large enough for "-2147483648"; i.e. 11 chars.
275 char[] buffer = new char[11];
276 int i = 11;
277 boolean isNeg;
278 if (num < 0)
279 {
280 isNeg = true;
281 num = -(num);
282 if (num < 0)
283 {
284 // Must be MIN_VALUE, so handle this special case.
285 buffer[--i] = '8';
286 num = 214748364;
287 }
288 }
289 else
290 isNeg = false;
291
292 do
293 {
294 buffer[--i] = (char) ((int) '0' + (num % 10));
295 num /= 10;
296 }
297 while (num > 0);
298
299 if (isNeg)
300 buffer[--i] = '-';
301
302 return String.valueOf(buffer, i, 11-i);
303 }
304
305 public static String toString(int num, int radix)
306 {
307 // Use optimized method for the typical case.
308 if (radix == 10 ||
309 radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
310 return toString(num);
311
312 // For negative numbers, print out the absolute value w/ a leading '-'.
313 // Use an array large enough for a binary number.
314 char[] buffer = new char[33];
315 int i = 33;
316 boolean isNeg;
317 if (num < 0)
318 {
319 isNeg = true;
320 num = -(num);
321
322 // When the value is MIN_VALUE, it overflows when made positive
323 if (num < 0)
324 {
325 buffer[--i] = Character.forDigit(-(num + radix) % radix, radix);
326 num = -(num / radix);
327 }
328 }
329 else
330 isNeg = false;
331
332 do
333 {
334 buffer[--i] = Character.forDigit(num % radix, radix);
335 num /= radix;
336 }
337 while (num > 0);
338
339 if (isNeg)
340 buffer[--i] = '-';
341
342 return String.valueOf(buffer, i, 33-i);
343 }
344
345 public static Integer valueOf(String str) throws NumberFormatException
346 {
347 return new Integer(parseInt(str, 10));
348 }
349
350 public static Integer valueOf(String str, int radix)
351 throws NumberFormatException
352 {
353 return new Integer(parseInt(str, radix));
354 }
355 }