1 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 * @author Warren Levy <warrenl@cygnus.com>
13 * @date September 11, 1998.
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.
21 public final class Integer extends Number implements Comparable
23 public static final int MAX_VALUE = 0x7FFFFFFF;
24 public static final int MIN_VALUE = 0x80000000;
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;
30 /* The int value of the instance. */
33 private static final long serialVersionUID = 1360826667806852920L;
35 public Integer(int val)
40 public Integer(String str) throws NumberFormatException
42 value = parseInt(str, 10);
45 public byte byteValue()
50 public double doubleValue()
52 return (double) value;
55 public float floatValue()
65 public long longValue()
70 public short shortValue()
76 public int compareTo(Integer anotherInteger)
78 if (this.value == anotherInteger.value)
81 // Returns just -1 or 1 on inequality; doing math might overflow the int.
82 if (this.value > anotherInteger.value)
89 /** @throws ClassCastException */
90 public int compareTo(Object o)
92 return this.compareTo((Integer) o);
95 public static Integer decode(String str) throws NumberFormatException
97 boolean isNeg = false;
102 if ((len = str.length()) == 0)
103 throw new NumberFormatException();
105 // Negative numbers are always radix 10.
106 if (str.charAt(0) == '-')
112 else if (str.charAt(index) == '#')
117 else if (str.charAt(index) == '0')
119 // Check if str is just "0"
121 return new Integer(0);
124 if (str.charAt(index) == 'x')
134 throw new NumberFormatException();
136 return new Integer(parseInt(str, index, len, isNeg, radix));
139 public boolean equals(Object obj)
141 return (obj instanceof Integer && ((Integer) obj).value == value);
144 public static Integer getInteger(String prop)
146 return getInteger(prop, null);
149 public static Integer getInteger(String prop, int defval)
151 Integer val = getInteger(prop, null);
152 return val == null ? new Integer(defval) : val;
155 public static Integer getInteger(String prop, Integer defobj)
159 return decode(System.getProperty(prop));
161 catch (NumberFormatException ex)
167 public int hashCode()
172 public static int parseInt(String str) throws NumberFormatException
174 return parseInt(str, 10);
177 public static int parseInt(String str, int radix) throws NumberFormatException
181 if ((len = str.length()) == 0 ||
182 radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
183 throw new NumberFormatException();
185 boolean isNeg = false;
187 if (str.charAt(index) == '-')
194 throw new NumberFormatException();
196 return parseInt(str, index, len, isNeg, radix);
199 private static int parseInt(String str, int index, int len, boolean isNeg,
200 int radix) throws NumberFormatException
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)
211 for ( ; index < len; index++)
213 if (val < 0 || val > max)
214 throw new NumberFormatException();
216 if ((digval = Character.digit(str.charAt(index), radix)) < 0)
217 throw new NumberFormatException();
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();
226 return isNeg ? -(val) : val;
229 public static String toBinaryString(int num)
231 return toUnsignedString(num, 1);
234 public static String toHexString(int num)
236 return toUnsignedString(num, 4);
239 public static String toOctalString(int num)
241 return toUnsignedString(num, 3);
244 private static String toUnsignedString(int num, int exp)
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];
253 buffer[--i] = Character.forDigit(num & mask, radix);
258 return String.valueOf(buffer, i, 32-i);
261 public String toString()
263 return toString(this.value);
266 public static String toString(int num)
268 // Use an arrary large enough for "-2147483648"; i.e. 11 chars.
269 char[] buffer = new char[11];
278 // Must be MIN_VALUE, so handle this special case.
288 buffer[--i] = (char) ((int) '0' + (num % 10));
296 return String.valueOf(buffer, i, 11-i);
299 public static String toString(int num, int radix)
301 // Use optimized method for the typical case.
303 radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
304 return toString(num);
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];
316 // When the value is MIN_VALUE, it overflows when made positive
319 buffer[--i] = Character.forDigit(-(num + radix) % radix, radix);
320 num = -(num / radix);
328 buffer[--i] = Character.forDigit(num % radix, radix);
336 return String.valueOf(buffer, i, 33-i);
339 public static Integer valueOf(String str) throws NumberFormatException
341 return new Integer(parseInt(str, 10));
344 public static Integer valueOf(String str, int radix)
345 throws NumberFormatException
347 return new Integer(parseInt(str, radix));