1 /* Copyright (C) 1998, 1999 Cygnus Solutions
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 public Integer(int val)
38 public Integer(String str) throws NumberFormatException
40 value = parseInt(str, 10);
43 public byte byteValue()
48 public double doubleValue()
50 return (double) value;
53 public float floatValue()
63 public long longValue()
68 public short shortValue()
74 public int compareTo(Integer anotherInteger)
76 if (this.value == anotherInteger.value)
79 // Returns just -1 or 1 on inequality; doing math might overflow the int.
80 if (this.value > anotherInteger.value)
87 public int compareTo(Object o) throws ClassCastException
89 if (!(o instanceof Integer))
90 throw new ClassCastException();
92 return this.compareTo((Integer) o);
95 public static Integer decode(String str) throws NumberFormatException
97 boolean isNeg = false;
102 if (str == null || (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 != null && (obj instanceof Integer)
142 && ((Integer) obj).value == value);
145 public static Integer getInteger(String prop)
147 return getInteger(prop, null);
150 public static Integer getInteger(String prop, int defval)
152 Integer val = getInteger(prop, null);
153 return val == null ? new Integer(defval) : val;
156 public static Integer getInteger(String prop, Integer defobj)
160 return decode(System.getProperty(prop));
162 catch (NumberFormatException ex)
168 public int hashCode()
173 public static int parseInt(String str) throws NumberFormatException
175 return parseInt(str, 10);
178 public static int parseInt(String str, int radix) throws NumberFormatException
182 if (str == null || (len = str.length()) == 0 ||
183 radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
184 throw new NumberFormatException();
186 boolean isNeg = false;
188 if (str.charAt(index) == '-')
195 throw new NumberFormatException();
197 return parseInt(str, index, len, isNeg, radix);
200 private static int parseInt(String str, int index, int len, boolean isNeg,
201 int radix) throws NumberFormatException
206 for ( ; index < len; index++)
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();
214 if ((digval = Character.digit(str.charAt(index), radix)) < 0)
215 throw new NumberFormatException();
217 // Throw an exception for overflow if result is negative.
218 // However, we special-case the most negative value.
220 if (val < 0 || val + digval < 0)
222 if (isNeg && val + digval == MIN_VALUE)
227 throw new NumberFormatException();
232 return isNeg ? -(val) : val;
235 public static String toBinaryString(int num)
237 return toUnsignedString(num, 1);
240 public static String toHexString(int num)
242 return toUnsignedString(num, 4);
245 public static String toOctalString(int num)
247 return toUnsignedString(num, 3);
250 private static String toUnsignedString(int num, int exp)
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];
259 buffer[--i] = Character.forDigit(num & mask, radix);
264 return String.valueOf(buffer, i, 32-i);
267 public String toString()
269 return toString(this.value);
272 public static String toString(int num)
274 // Use an arrary large enough for "-2147483648"; i.e. 11 chars.
275 char[] buffer = new char[11];
284 // Must be MIN_VALUE, so handle this special case.
294 buffer[--i] = (char) ((int) '0' + (num % 10));
302 return String.valueOf(buffer, i, 11-i);
305 public static String toString(int num, int radix)
307 // Use optimized method for the typical case.
309 radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
310 return toString(num);
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];
322 // When the value is MIN_VALUE, it overflows when made positive
325 buffer[--i] = Character.forDigit(-(num + radix) % radix, radix);
326 num = -(num / radix);
334 buffer[--i] = Character.forDigit(num % radix, radix);
342 return String.valueOf(buffer, i, 33-i);
345 public static Integer valueOf(String str) throws NumberFormatException
347 return new Integer(parseInt(str, 10));
350 public static Integer valueOf(String str, int radix)
351 throws NumberFormatException
353 return new Integer(parseInt(str, radix));