From f1cc2b1b4911cb97fd6ee9107cfbf59d446c6c9c Mon Sep 17 00:00:00 2001 From: tromey Date: Thu, 13 Jun 2002 18:16:26 +0000 Subject: * java/lang/natString.cc (init): Handle case where DONT_COPY is true and OFFSET!=0. * java/lang/String.java (String(char[],int,int,boolean): New constructor. * java/lang/Long.java: Imported new version from Classpath. * java/lang/Number.java: Likewise. * java/lang/Integer.java: Likewise. * java/lang/Long.java: Likewise. * java/lang/Float.java: Likewise. * java/lang/Boolean.java: Likewise. * java/lang/Double.java: Likewise. * java/lang/Void.java: Likewise. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@54595 138bc75d-0d04-0410-961f-82ee72b054a4 --- libjava/java/lang/Integer.java | 748 +++++++++++++++++++++-------------------- 1 file changed, 386 insertions(+), 362 deletions(-) (limited to 'libjava/java/lang/Integer.java') diff --git a/libjava/java/lang/Integer.java b/libjava/java/lang/Integer.java index 128dbcaf77e..f9d901804f7 100644 --- a/libjava/java/lang/Integer.java +++ b/libjava/java/lang/Integer.java @@ -1,5 +1,5 @@ -/* java.lang.Integer - Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. +/* Integer.java -- object wrapper for int + Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -48,36 +48,45 @@ package java.lang; * @author Paul Fisher * @author John Keiser * @author Warren Levy - * @since JDK 1.0 + * @author Eric Blake + * @since 1.0 + * @status updated to 1.4 */ public final class Integer extends Number implements Comparable { - // compatible with JDK 1.0.2+ + /** + * Compatible with JDK 1.0.2+. + */ private static final long serialVersionUID = 1360826667806852920L; /** - * The minimum value an int can represent is -2147483648. + * The minimum value an int can represent is -2147483648 (or + * -231). */ public static final int MIN_VALUE = 0x80000000; /** - * The maximum value an int can represent is 2147483647. + * The maximum value an int can represent is 2147483647 (or + * 231 - 1). */ public static final int MAX_VALUE = 0x7fffffff; /** - * The primitive type int is represented by this + * The primitive type int is represented by this * Class object. + * @since 1.1 */ - public static final Class TYPE = VMClassLoader.getPrimitiveClass ('I'); + public static final Class TYPE = VMClassLoader.getPrimitiveClass('I'); /** * The immutable value of this Integer. + * + * @serial the wrapped int */ private final int value; /** - * Create an Integer object representing the value of the + * Create an Integer object representing the value of the * int argument. * * @param value the value to use @@ -88,135 +97,73 @@ public final class Integer extends Number implements Comparable } /** - * Create an Integer object representing the value of the + * Create an Integer object representing the value of the * argument after conversion to an int. * - * @param s the string to convert. + * @param s the string to convert + * @throws NumberFormatException if the String does not contain an int + * @see #valueOf(String) */ - public Integer(String s) throws NumberFormatException + public Integer(String s) { - value = parseInt(s, 10); + value = parseInt(s, 10, false); } /** - * Return a hashcode representing this Object. - * - * Integer's hash code is calculated by simply returning its - * value. - * - * @return this Object's hash code. - */ - public int hashCode() - { - return value; - } - - /** - * If the Object is not null, is an - * instanceof Integer, and represents - * the same primitive int value return - * true. Otherwise false is returned. - */ - public boolean equals(Object obj) - { - return obj instanceof Integer && value == ((Integer)obj).value; - } - - /** - * Get the specified system property as an Integer. - * - * The decode() method will be used to interpret the value of - * the property. - * @param nm the name of the system property - * @return the system property as an Integer, or - * null if the property is not found or cannot be - * decoded as an Integer. - * @see java.lang.System#getProperty(java.lang.String) - * @see #decode(int) - */ - public static Integer getInteger(String nm) - { - return getInteger(nm, null); - } - - /** - * Get the specified system property as an Integer, or use a - * default int value if the property is not found or is not - * decodable. - * - * The decode() method will be used to interpret the value of - * the property. + * Converts the int to a String using + * the specified radix (base). If the radix exceeds + * Character.MIN_RADIX or Character.MAX_RADIX, 10 + * is used instead. If the result is negative, the leading character is + * '-' ('\\u002D'). The remaining characters come from + * Character.forDigit(digit, radix) ('0'-'9','a'-'z'). * - * @param nm the name of the system property - * @param val the default value to use if the property is not found or not - * a number. - * @return the system property as an Integer, or the default - * value if the property is not found or cannot be decoded as an - * Integer. - * @see java.lang.System#getProperty(java.lang.String) - * @see #decode(int) - * @see #getInteger(java.lang.String,java.lang.Integer) + * @param num the int to convert to String + * @param radix the radix (base) to use in the conversion + * @return the String representation of the argument */ - public static Integer getInteger(String nm, int val) + public static String toString(int num, int radix) { - Integer result = getInteger(nm, null); - return (result == null) ? new Integer(val) : result; - } + if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) + radix = 10; - /** - * Get the specified system property as an Integer, or use a - * default Integer value if the property is not found or is - * not decodable. - * - * The decode() method will be used to interpret the value of - * the property. - * - * @param nm the name of the system property - * @param val the default value to use if the property is not found or not - * a number. - * @return the system property as an Integer, or the default - * value if the property is not found or cannot be decoded as an - * Integer. - * @see java.lang.System#getProperty(java.lang.String) - * @see #decode(int) - * @see #getInteger(java.lang.String,int) - */ - public static Integer getInteger(String nm, Integer def) - { - String val = System.getProperty(nm); - if (val == null) return def; - try - { - return decode(val); - } - catch (NumberFormatException e) + // For negative numbers, print out the absolute value w/ a leading '-'. + // Use an array large enough for a binary number. + char[] buffer = new char[33]; + int i = 33; + boolean isNeg = false; + if (num < 0) { - return def; + isNeg = true; + num = -num; + + // When the value is MIN_VALUE, it overflows when made positive + if (num < 0) + { + buffer[--i] = digits[(int) (-(num + radix) % radix)]; + num = -(num / radix); + } } - } - private static String toUnsignedString(int num, int exp) - { - // Use an array large enough for a binary number. - int radix = 1 << exp; - int mask = radix - 1; - char[] buffer = new char[32]; - int i = 32; do { - buffer[--i] = Character.forDigit(num & mask, radix); - num = num >>> exp; + buffer[--i] = digits[num % radix]; + num /= radix; } - while (num != 0); + while (num > 0); - return String.valueOf(buffer, i, 32-i); + if (isNeg) + buffer[--i] = '-'; + + // Package constructor avoids an array copy. + return new String(buffer, i, 33 - i, true); } /** * Converts the int to a String assuming it is * unsigned in base 16. + * * @param i the int to convert to String - * @return the String representation of the argument. + * @return the String representation of the argument */ public static String toHexString(int i) { @@ -226,8 +173,9 @@ public final class Integer extends Number implements Comparable /** * Converts the int to a String assuming it is * unsigned in base 8. + * * @param i the int to convert to String - * @return the String representation of the argument. + * @return the String representation of the argument */ public static String toOctalString(int i) { @@ -237,8 +185,9 @@ public final class Integer extends Number implements Comparable /** * Converts the int to a String assuming it is * unsigned in base 2. + * * @param i the int to convert to String - * @return the String representation of the argument. + * @return the String representation of the argument */ public static String toBinaryString(int i) { @@ -248,335 +197,410 @@ public final class Integer extends Number implements Comparable /** * Converts the int to a String and assumes * a radix of 10. + * * @param i the int to convert to String - * @return the String representation of the argument. + * @return the String representation of the argument + * @see #toString(int, int) */ public static String toString(int i) { // This is tricky: in libgcj, String.valueOf(int) is a fast native // implementation. In Classpath it just calls back to - // Integer.toString(int,int). - return String.valueOf (i); + // Integer.toString(int, int). + return String.valueOf(i); } /** - * Converts the Integer value to a String and - * assumes a radix of 10. - * @return the String representation of this Integer. - */ - public String toString() - { - return toString (value); - } - - /** - * Converts the int to a String using - * the specified radix (base). - * @param i the int to convert to String. - * @param radix the radix (base) to use in the conversion. - * @return the String representation of the argument. + * Converts the specified String into an int + * using the specified radix (base). The string must not be null + * or empty. It may begin with an optional '-', which will negate the answer, + * provided that there are also valid digits. Each digit is parsed as if by + * Character.digit(d, radix), and must be in the range + * 0 to radix - 1. Finally, the result must be + * within MIN_VALUE to MAX_VALUE, inclusive. + * Unlike Double.parseDouble, you may not have a leading '+'. + * + * @param s the String to convert + * @param radix the radix (base) to use in the conversion + * @return the String argument converted to int + * @throws NumberFormatException if s cannot be parsed as an + * int */ - public static String toString(int num, int radix) + public static int parseInt(String str, int radix) { - if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) - radix = 10; - - // For negative numbers, print out the absolute value w/ a leading '-'. - // Use an array large enough for a binary number. - char[] buffer = new char[33]; - int i = 33; - boolean isNeg; - if (num < 0) - { - isNeg = true; - num = -(num); - - // When the value is MIN_VALUE, it overflows when made positive - if (num < 0) - { - buffer[--i] = Character.forDigit(-(num + radix) % radix, radix); - num = -(num / radix); - } - } - else - isNeg = false; - - do - { - buffer[--i] = Character.forDigit(num % radix, radix); - num /= radix; - } - while (num > 0); - - if (isNeg) - buffer[--i] = '-'; - - return String.valueOf(buffer, i, 33-i); + return parseInt(str, radix, false); } /** - * Creates a new Integer object using the String, - * assuming a radix of 10. - * @param s the String to convert. - * @return the new Integer. - * @see #Integer(java.lang.String) - * @see #parseInt(java.lang.String) - * @exception NumberFormatException thrown if the String - * cannot be parsed as an int. - */ - public static Integer valueOf(String s) throws NumberFormatException + * Converts the specified String into an int. + * This function assumes a radix of 10. + * + * @param s the String to convert + * @return the int value of s + * @throws NumberFormatException if s cannot be parsed as an + * int + * @see #parseInt(String, int) + */ + public static int parseInt(String s) { - return new Integer(parseInt(s)); + return parseInt(s, 10, false); } /** * Creates a new Integer object using the String * and specified radix (base). - * @param s the String to convert. - * @param radix the radix (base) to convert with. - * @return the new Integer. - * @see #parseInt(java.lang.String,int) - * @exception NumberFormatException thrown if the String - * cannot be parsed as an int. + * + * @param s the String to convert + * @param radix the radix (base) to convert with + * @return the new Integer + * @throws NumberFormatException if s cannot be parsed as an + * int + * @see #parseInt(String, int) */ public static Integer valueOf(String s, int radix) - throws NumberFormatException { - return new Integer(parseInt(s, radix)); + return new Integer(parseInt(s, radix, false)); } /** - * Converts the specified String into an int. - * This function assumes a radix of 10. + * Creates a new Integer object using the String, + * assuming a radix of 10. * * @param s the String to convert - * @return the int value of the String - * argument. - * @exception NumberFormatException thrown if the String - * cannot be parsed as an int. + * @return the new Integer + * @throws NumberFormatException if s cannot be parsed as an + * int + * @see #Integer(String) + * @see #parseInt(String) */ - public static int parseInt(String s) throws NumberFormatException + public static Integer valueOf(String s) { - return parseInt(s, 10); + return new Integer(parseInt(s, 10, false)); } /** - * Converts the specified String into an int - * using the specified radix (base). + * Return the value of this Integer as a byte. * - * @param s the String to convert - * @param radix the radix (base) to use in the conversion - * @return the String argument converted to int. - * @exception NumberFormatException thrown if the String - * cannot be parsed as a int. + * @return the byte value */ - public static int parseInt(String str, int radix) - throws NumberFormatException + public byte byteValue() { - final int len; - - if (str == null) - throw new NumberFormatException (); - - if ((len = str.length()) == 0 || - radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) - throw new NumberFormatException(); - - boolean isNeg = false; - int index = 0; - if (str.charAt(index) == '-') - if (len > 1) - { - isNeg = true; - index++; - } - else - throw new NumberFormatException(); - - return parseInt(str, index, len, isNeg, radix); + return (byte) value; } - private static int parseInt(String str, int index, int len, boolean isNeg, - int radix) - throws NumberFormatException + /** + * Return the value of this Integer as a short. + * + * @return the short value + */ + public short shortValue() { - int val = 0; - int digval; - - int max = MAX_VALUE / radix; - // We can't directly write `max = (MAX_VALUE + 1) / radix'. - // So instead we fake it. - if (isNeg && MAX_VALUE % radix == radix - 1) - ++max; - - for ( ; index < len; index++) - { - if (val < 0 || val > max) - throw new NumberFormatException(); - - if ((digval = Character.digit(str.charAt(index), radix)) < 0) - throw new NumberFormatException(); - - // Throw an exception for overflow if result is negative. - // However, we special-case the most negative value. - val = val * radix + digval; - if (val < 0 && (! isNeg || val != MIN_VALUE)) - throw new NumberFormatException(); - } + return (short) value; + } - return isNeg ? -(val) : val; + /** + * Return the value of this Integer. + * @return the int value + */ + public int intValue() + { + return value; } /** - * Convert the specified String into an Integer. - * The String may represent decimal, hexadecimal, or - * octal numbers. - * - * The String argument is interpreted based on the leading - * characters. Depending on what the String begins with, the base will be - * interpreted differently: + * Return the value of this Integer as a long. * - * - * - * - * - * - * - * - *
Leading
Characters
Base
#16
0x16
0X16
08
Anything
Else
10
- * - * @param str the String to interpret. - * @return the value of the String as an Integer. - * @exception NumberFormatException thrown if the String - * cannot be parsed as an int. + * @return the long value */ - public static Integer decode(String str) throws NumberFormatException + public long longValue() { - boolean isNeg = false; - int index = 0; - int radix = 10; - final int len; - - if (str == null || (len = str.length()) == 0) - throw new NumberFormatException("string null or empty"); - - // Negative numbers are always radix 10. - if (str.charAt(index) == '-') - { - radix = 10; - index++; - isNeg = true; - } - else if (str.charAt(index) == '#') - { - radix = 16; - index++; - } - else if (str.charAt(index) == '0') - { - // Check if str is just "0" - if (len == 1) - return new Integer(0); + return value; + } - index++; - if (str.charAt(index) == 'x' || str.charAt(index) == 'X') - { - radix = 16; - index++; - } - else - radix = 8; - } + /** + * Return the value of this Integer as a float. + * + * @return the float value + */ + public float floatValue() + { + return value; + } - if (index >= len) - throw new NumberFormatException("empty value"); + /** + * Return the value of this Integer as a double. + * + * @return the double value + */ + public double doubleValue() + { + return value; + } - return new Integer(parseInt(str, index, len, isNeg, radix)); + /** + * Converts the Integer value to a String and + * assumes a radix of 10. + * + * @return the String representation + */ + public String toString() + { + return String.valueOf(value); } - /** Return the value of this Integer as a byte. - ** @return the value of this Integer as a byte. - **/ - public byte byteValue() + /** + * Return a hashcode representing this Object. Integer's hash + * code is simply its value. + * + * @return this Object's hash code + */ + public int hashCode() { - return (byte) value; + return value; } - /** Return the value of this Integer as a short. - ** @return the value of this Integer as a short. - **/ - public short shortValue() + /** + * Returns true if obj is an instance of + * Integer and represents the same int value. + * + * @param obj the object to compare + * @return whether these Objects are semantically equal + */ + public boolean equals(Object obj) { - return (short) value; + return obj instanceof Integer && value == ((Integer) obj).value; } - /** Return the value of this Integer as an int. - ** @return the value of this Integer as an int. - **/ - public int intValue() + /** + * Get the specified system property as an Integer. The + * decode() method will be used to interpret the value of + * the property. + * + * @param nm the name of the system property + * @return the system property as an Integer, or null if the + * property is not found or cannot be decoded + * @throws SecurityException if accessing the system property is forbidden + * @see System#getProperty(String) + * @see #decode(String) + */ + public static Integer getInteger(String nm) { - return value; + return getInteger(nm, null); } - /** Return the value of this Integer as a long. - ** @return the value of this Integer as a long. - **/ - public long longValue() + /** + * Get the specified system property as an Integer, or use a + * default int value if the property is not found or is not + * decodable. The decode() method will be used to interpret + * the value of the property. + * + * @param nm the name of the system property + * @param val the default value + * @return the value of the system property, or the default + * @throws SecurityException if accessing the system property is forbidden + * @see System#getProperty(String) + * @see #decode(String) + */ + public static Integer getInteger(String nm, int val) { - return value; + Integer result = getInteger(nm, null); + return result == null ? new Integer(val) : result; } - /** Return the value of this Integer as a float. - ** @return the value of this Integer as a float. - **/ - public float floatValue() + /** + * Get the specified system property as an Integer, or use a + * default Integer value if the property is not found or is + * not decodable. The decode() method will be used to + * interpret the value of the property. + * + * @param nm the name of the system property + * @param val the default value + * @return the value of the system property, or the default + * @throws SecurityException if accessing the system property is forbidden + * @see System#getProperty(String) + * @see #decode(String) + */ + public static Integer getInteger(String nm, Integer def) { - return value; + if (nm == null || "".equals(nm)) + return def; + nm = System.getProperty(nm); + if (nm == null) + return def; + try + { + return decode(nm); + } + catch (NumberFormatException e) + { + return def; + } } - /** Return the value of this Integer as a double. - ** @return the value of this Integer as a double. - **/ - public double doubleValue() + /** + * Convert the specified String into an Integer. + * The String may represent decimal, hexadecimal, or + * octal numbers. + * + *

The extended BNF grammar is as follows:
+ *

+   * DecodableString:
+   *      ( [ - ] DecimalNumber )
+   *    | ( [ - ] ( 0x | 0X
+   *              | # ) HexDigit { HexDigit } )
+   *    | ( [ - ] 0 { OctalDigit } )
+   * DecimalNumber:
+   *        DecimalDigit except '0' { DecimalDigit }
+   * DecimalDigit:
+   *        Character.digit(d, 10) has value 0 to 9
+   * OctalDigit:
+   *        Character.digit(d, 8) has value 0 to 7
+   * DecimalDigit:
+   *        Character.digit(d, 16) has value 0 to 15
+   * 
+ * Finally, the value must be in the range MIN_VALUE to + * MAX_VALUE, or an exception is thrown. + * + * @param s the String to interpret + * @return the value of the String as an Integer + * @throws NumberFormatException if s cannot be parsed as a + * int + * @throws NullPointerException if s is null + * @since 1.2 + */ + public static Integer decode(String str) { - return value; + return new Integer(parseInt(str, 10, true)); } /** - * Compare two Integers numerically by comparing their - * int values. - * @return a positive value if this Integer is greater - * in value than the argument Integer; a negative value - * if this Integer is smaller in value than the argument - * Integer; and 0, zero, if this - * Integer is equal in value to the argument - * Integer. + * Compare two Integers numerically by comparing their int + * values. The result is positive if the first is greater, negative if the + * second is greater, and 0 if the two are equal. * + * @param i the Integer to compare + * @return the comparison * @since 1.2 */ public int compareTo(Integer i) { - if (this.value == i.value) + if (value == i.value) return 0; - // Returns just -1 or 1 on inequality; doing math might overflow. - if (this.value > i.value) - return 1; - - return -1; + return value > i.value ? 1 : -1; } /** - * Behaves like compareTo(java.lang.Integer) unless the Object - * is not a Integer. Then it throws a - * ClassCastException. - * @exception ClassCastException if the argument is not a - * Integer. + * Behaves like compareTo(Integer) unless the Object + * is not an Integer. * + * @param o the object to compare + * @return the comparison + * @throws ClassCastException if the argument is not an Integer + * @see #compareTo(Integer) + * @see Comparable * @since 1.2 */ public int compareTo(Object o) { - return compareTo((Integer)o); + return compareTo((Integer) o); + } + + /** + * Helper for converting unsigned numbers to String. + * + * @param num the number + * @param exp log2(digit) (ie. 1, 3, or 4 for binary, oct, hex) + */ + // Package visible for use by Long. + static String toUnsignedString(int num, int exp) + { + // Use an array large enough for a binary number. + int mask = (1 << exp) - 1; + char[] buffer = new char[32]; + int i = 32; + do + { + buffer[--i] = digits[num & mask]; + num >>>= exp; + } + while (num != 0); + + // Package constructor avoids an array copy. + return new String(buffer, i, 32 - i, true); + } + + /** + * Helper for parsing ints, used by Integer, Short, and Byte. + * + * @param str the string to parse + * @param radix the radix to use, must be 10 if decode is true + * @param decode if called from decode + * @return the parsed int value + * @throws NumberFormatException if there is an error + * @throws NullPointerException if decode is true and str if null + * @see #parseInt(String, int) + * @see #decode(String) + * @see Byte#parseInt(String, int) + * @see Short#parseInt(String, int) + */ + static int parseInt(String str, int radix, boolean decode) + { + if (! decode && str == null) + throw new NumberFormatException(); + int index = 0; + int len = str.length(); + boolean isNeg = false; + if (len == 0) + throw new NumberFormatException(); + int ch = str.charAt(index); + if (ch == '-') + { + if (len == 1) + throw new NumberFormatException(); + isNeg = true; + ch = str.charAt(++index); + } + if (decode) + { + if (ch == '0') + { + if (++index == len) + return 0; + if ((str.charAt(index) & ~('x' ^ 'X')) == 'X') + { + radix = 16; + index++; + } + else + radix = 8; + } + else if (ch == '#') + { + radix = 16; + index++; + } + } + if (index == len) + throw new NumberFormatException(); + + int max = MAX_VALUE / radix; + // We can't directly write `max = (MAX_VALUE + 1) / radix'. + // So instead we fake it. + if (isNeg && MAX_VALUE % radix == radix - 1) + ++max; + + int val = 0; + while (index < len) + { + if (val < 0 || val > max) + throw new NumberFormatException(); + + ch = Character.digit(str.charAt(index++), radix); + val = val * radix + ch; + if (ch < 0 || (val < 0 && (! isNeg || val != MIN_VALUE))) + throw new NumberFormatException(); + } + return isNeg ? -val : val; } } -- cgit v1.2.1