OSDN Git Service

* java/lang/Byte.java: Remove redundant instanceof and null checks.
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / Integer.java
1 /* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
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   private static final long serialVersionUID = 1360826667806852920L;
34
35   public Integer(int val)
36   {
37     value = val;
38   }
39
40   public Integer(String str) throws NumberFormatException
41   {
42     value = parseInt(str, 10);
43   }
44
45   public byte byteValue()
46   {
47     return (byte) value;
48   }
49
50   public double doubleValue()
51   {
52     return (double) value;
53   }
54
55   public float floatValue()
56   {
57     return (float) value;
58   }
59
60   public int intValue()
61   {
62     return value;
63   }
64
65   public long longValue()
66   {
67     return value;
68   }
69
70   public short shortValue()
71   {
72     return (short) value;
73   }
74
75   // Added in JDK 1.2
76   public int compareTo(Integer anotherInteger)
77   {
78     if (this.value == anotherInteger.value)
79       return 0;
80
81     // Returns just -1 or 1 on inequality; doing math might overflow the int.
82     if (this.value > anotherInteger.value)
83       return 1;
84
85     return -1;
86   }
87
88   // Added in JDK 1.2
89   /** @throws ClassCastException */
90   public int compareTo(Object o)
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 ((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 instanceof Integer && ((Integer) obj).value == value);
142   }
143
144   public static Integer getInteger(String prop)
145   {
146     return getInteger(prop, null);
147   }
148
149   public static Integer getInteger(String prop, int defval)
150   {
151     Integer val = getInteger(prop, null);
152     return val == null ? new Integer(defval) : val;
153   }
154
155   public static Integer getInteger(String prop, Integer defobj)
156   {
157     try
158     {
159       return decode(System.getProperty(prop));
160     }
161     catch (NumberFormatException ex)
162     {
163       return defobj;
164     }
165   }
166
167   public int hashCode()
168   {
169     return value;
170   }
171
172   public static int parseInt(String str) throws NumberFormatException
173   {
174     return parseInt(str, 10);
175   }
176
177   public static int parseInt(String str, int radix) throws NumberFormatException
178   {
179     final int len;
180
181     if ((len = str.length()) == 0 ||
182         radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
183       throw new NumberFormatException();
184
185     boolean isNeg = false;
186     int index = 0;
187     if (str.charAt(index) == '-')
188       if (len > 1)
189         {
190           isNeg = true;
191           index++;
192         }
193       else
194         throw new NumberFormatException();
195
196     return parseInt(str, index, len, isNeg, radix);
197   }
198
199   private static int parseInt(String str, int index, int len, boolean isNeg,
200                                 int radix) throws NumberFormatException
201   {
202     int val = 0;
203     int digval;
204
205     int max = MAX_VALUE / radix;
206     // We can't directly write `max = (MAX_VALUE + 1) / radix'.
207     // So instead we fake it.
208     if (isNeg && MAX_VALUE % radix == radix - 1)
209       ++max;
210
211     for ( ; index < len; index++)
212       {
213         if (val < 0 || val > max)
214           throw new NumberFormatException();
215
216         if ((digval = Character.digit(str.charAt(index), radix)) < 0)
217           throw new NumberFormatException();
218
219         // Throw an exception for overflow if result is negative.
220         // However, we special-case the most negative value.
221         val = val * radix + digval;
222         if (val < 0 && (! isNeg || val != MIN_VALUE))
223           throw new NumberFormatException();
224       }
225
226     return isNeg ? -(val) : val;
227   }
228
229   public static String toBinaryString(int num)
230   {
231     return toUnsignedString(num, 1);
232   }
233
234   public static String toHexString(int num)
235   {
236     return toUnsignedString(num, 4);
237   }
238
239   public static String toOctalString(int num)
240   {
241     return toUnsignedString(num, 3);
242   }
243
244   private static String toUnsignedString(int num, int exp)
245   {
246     // Use an array large enough for a binary number.
247     int radix = 1 << exp;
248     int mask = radix - 1;
249     char[] buffer = new char[32];
250     int i = 32;
251     do
252       {
253         buffer[--i] = Character.forDigit(num & mask, radix);
254         num = num >>> exp;
255       }
256     while (num != 0);
257
258     return String.valueOf(buffer, i, 32-i);
259   }
260
261   public String toString()
262   {
263     return toString(this.value);
264   }
265
266   public static String toString(int num)
267   {
268     // Use an arrary large enough for "-2147483648"; i.e. 11 chars.
269     char[] buffer = new char[11];
270     int i = 11;
271     boolean isNeg;
272     if (num < 0)
273       {
274         isNeg = true;
275         num = -(num);
276         if (num < 0)
277           {
278             // Must be MIN_VALUE, so handle this special case.
279             buffer[--i] = '8';
280             num = 214748364;
281           }
282       }
283     else
284       isNeg = false;
285
286     do
287       {
288         buffer[--i] = (char) ((int) '0' + (num % 10));
289         num /= 10;
290       }
291     while (num > 0);
292
293     if (isNeg)
294       buffer[--i] = '-';
295
296     return String.valueOf(buffer, i, 11-i);
297   }
298
299   public static String toString(int num, int radix)
300   {
301     // Use optimized method for the typical case.
302     if (radix == 10 ||
303         radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
304       return toString(num);
305
306     // For negative numbers, print out the absolute value w/ a leading '-'.
307     // Use an array large enough for a binary number.
308     char[] buffer = new char[33];
309     int i = 33;
310     boolean isNeg;
311     if (num < 0)
312       {
313         isNeg = true;
314         num = -(num);
315
316         // When the value is MIN_VALUE, it overflows when made positive
317         if (num < 0)
318           {
319             buffer[--i] = Character.forDigit(-(num + radix) % radix, radix);
320             num = -(num / radix);
321           }
322       }
323     else
324       isNeg = false;
325
326     do
327       {
328         buffer[--i] = Character.forDigit(num % radix, radix);
329         num /= radix;
330       }
331     while (num > 0);
332
333     if (isNeg)
334       buffer[--i] = '-';
335
336     return String.valueOf(buffer, i, 33-i);
337   }
338
339   public static Integer valueOf(String str) throws NumberFormatException
340   {
341     return new Integer(parseInt(str, 10));
342   }
343
344   public static Integer valueOf(String str, int radix)
345                                 throws NumberFormatException
346   {
347     return new Integer(parseInt(str, radix));
348   }
349 }