From 1c83443dabf89bc2ca8ed7f10b1925dd028271a4 Mon Sep 17 00:00:00 2001 From: tromey Date: Mon, 23 Jul 2001 20:01:29 +0000 Subject: * gcj/javaprims.h: Rebuilt class list. * Makefile.in: Rebuilt. * Makefile.am (core_java_source_files): Added VMClassLoader. * java/lang/VMClassLoader.java: New file. * java/lang/Boolean.java: Merged with Classpath. * java/lang/Byte.java: Merged with Classpath. * java/lang/Integer.java: Merged with Classpath. * java/lang/Long.java: Merged with Classpath. * java/lang/Number.java: Merged with Classpath. * java/lang/Short.java: Merged with Classpath. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@44274 138bc75d-0d04-0410-961f-82ee72b054a4 --- libjava/java/lang/Integer.java | 607 +++++++++++++++++++++++++++++------------ 1 file changed, 427 insertions(+), 180 deletions(-) (limited to 'libjava/java/lang/Integer.java') diff --git a/libjava/java/lang/Integer.java b/libjava/java/lang/Integer.java index 9ddfa312f06..414d93d43a8 100644 --- a/libjava/java/lang/Integer.java +++ b/libjava/java/lang/Integer.java @@ -1,182 +1,368 @@ -/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation +/* java.lang.Integer + Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. - This file is part of libgcj. +This file is part of GNU Classpath. -This software is copyrighted work licensed under the terms of the -Libgcj License. Please consult the file "LIBGCJ_LICENSE" for -details. */ +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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +As a special exception, if you link this library with other files to +produce an executable, this library does not by itself cause the +resulting executable to be covered by the GNU General Public License. +This exception does not however invalidate any other reasons why the +executable file might be covered by the GNU General Public License. */ + + package java.lang; /** - * @author Warren Levy - * @date September 11, 1998. + * Instances of class Integer represent primitive + * int values. + * + * Additionally, this class provides various helper functions and variables + * related to ints. + * + * @author Paul Fisher + * @author John Keiser + * @author Warren Levy + * @since JDK 1.0 */ -/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 - * "The Java Language Specification", ISBN 0-201-63451-1 - * plus online API docs for JDK 1.2 beta from http://www.javasoft.com. - * Status: Believed complete and correct. - */ - public final class Integer extends Number implements Comparable { - public static final int MAX_VALUE = 0x7FFFFFFF; - public static final int MIN_VALUE = 0x80000000; - - // This initialization is seemingly circular, but it is accepted - // by javac, and is handled specially by gcc. - public static final Class TYPE = int.class; - - /* The int value of the instance. */ - private int value; - + // compatible with JDK 1.0.2+ private static final long serialVersionUID = 1360826667806852920L; - public Integer(int val) + /** + * The minimum value an int can represent is -2147483648. + */ + public static final int MIN_VALUE = 0x80000000; + + /** + * The maximum value an int can represent is 2147483647. + */ + public static final int MAX_VALUE = 0x7fffffff; + + /** + * The primitive type int is represented by this + * Class object. + */ + public static final Class TYPE = VMClassLoader.getPrimitiveClass ("int"); + + /** + * The immutable value of this Integer. + */ + private final int value; + + /** + * Create an Integer object representing the value of the + * int argument. + * + * @param value the value to use + */ + public Integer(int value) { - value = val; + this.value = value; } - public Integer(String str) throws NumberFormatException + /** + * Create an Integer object representing the value of the + * argument after conversion to an int. + * + * @param s the string to convert. + */ + public Integer(String s) throws NumberFormatException { - value = parseInt(str, 10); + value = parseInt(s, 10); } - public byte byteValue() + /** + * 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 (byte) value; + return value; } - public double doubleValue() + /** + * 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 (double) value; + return obj instanceof Integer && value == ((Integer)obj).value; } - public float floatValue() + /** + * 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 (float) value; + return getInteger(nm, null); } - public int intValue() + /** + * 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 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) + */ + public static Integer getInteger(String nm, int val) { - return value; + Integer result = getInteger(nm, null); + return (result == null) ? new Integer(val) : result; } - public long longValue() + /** + * 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) { - return value; + String val = System.getProperty(nm); + if (val == null) return def; + try + { + return decode(nm); + } + catch (NumberFormatException e) + { + return def; + } } - public short shortValue() + private static String toUnsignedString(int num, int exp) { - return (short) value; + // 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; + } + while (num != 0); + + return String.valueOf(buffer, i, 32-i); } - // Added in JDK 1.2 - public int compareTo(Integer anotherInteger) + /** + * 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. + */ + public static String toHexString(int i) { - if (this.value == anotherInteger.value) - return 0; + return toUnsignedString(i, 4); + } - // Returns just -1 or 1 on inequality; doing math might overflow the int. - if (this.value > anotherInteger.value) - return 1; + /** + * 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. + */ + public static String toOctalString(int i) + { + return toUnsignedString(i, 3); + } - return -1; + /** + * 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. + */ + public static String toBinaryString(int i) + { + return toUnsignedString(i, 1); } - // Added in JDK 1.2 - /** @throws ClassCastException */ - public int compareTo(Object o) + /** + * 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. + */ + public static String toString(int i) { - return this.compareTo((Integer) o); + // 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); } - public static Integer decode(String str) throws NumberFormatException + /** + * Converts the Integer value to a String and + * assumes a radix of 10. + * @return the String representation of this Integer. + */ + public String toString() { - boolean isNeg = false; - int index = 0; - int radix = 10; - final int len; + return toString (value); + } - if ((len = str.length()) == 0) - throw new NumberFormatException(); + /** + * 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. + */ + public static String toString(int num, int radix) + { + // Use optimized method for the typical case. + if (radix == 10 || + radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) + return toString(num); - // Negative numbers are always radix 10. - if (str.charAt(0) == '-') + // 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) { - 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); + num = -(num); - index++; - if (str.charAt(index) == 'x') + // When the value is MIN_VALUE, it overflows when made positive + if (num < 0) { - radix = 16; - index++; + buffer[--i] = Character.forDigit(-(num + radix) % radix, radix); + num = -(num / radix); } - else - radix = 8; } + else + isNeg = false; - if (index >= len) - throw new NumberFormatException(); - - return new Integer(parseInt(str, index, len, isNeg, radix)); - } - - public boolean equals(Object obj) - { - return (obj instanceof Integer && ((Integer) obj).value == value); - } + do + { + buffer[--i] = Character.forDigit(num % radix, radix); + num /= radix; + } + while (num > 0); - public static Integer getInteger(String prop) - { - return getInteger(prop, null); - } + if (isNeg) + buffer[--i] = '-'; - public static Integer getInteger(String prop, int defval) - { - Integer val = getInteger(prop, null); - return val == null ? new Integer(defval) : val; + return String.valueOf(buffer, i, 33-i); } - public static Integer getInteger(String prop, Integer defobj) + /** + * 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 { - try - { - String val = System.getProperty(prop); - if (val != null) - return decode(val); - } - catch (NumberFormatException ex) - { - } - return defobj; + return new Integer(parseInt(s)); } - public int hashCode() + /** + * 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. + */ + public static Integer valueOf(String s, int radix) + throws NumberFormatException { - return value; + return new Integer(parseInt(s, radix)); } - public static int parseInt(String str) 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 the String + * argument. + * @exception NumberFormatException thrown if the String + * cannot be parsed as an int. + */ + public static int parseInt(String s) throws NumberFormatException { - return parseInt(str, 10); + return parseInt(s, 10); } - public static int parseInt(String str, int radix) throws NumberFormatException + /** + * Converts the specified String into an int + * using the specified radix (base). + * + * @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. + */ + public static int parseInt(String str, int radix) + throws NumberFormatException { final int len; @@ -199,7 +385,8 @@ public final class Integer extends Number implements Comparable } private static int parseInt(String str, int index, int len, boolean isNeg, - int radix) throws NumberFormatException + int radix) + throws NumberFormatException { int val = 0; int digval; @@ -228,96 +415,156 @@ public final class Integer extends Number implements Comparable return isNeg ? -(val) : val; } - public static String toBinaryString(int num) + /** + * 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: + * + * + * + * + * + * + * + * + *
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. + */ + public static Integer decode(String str) throws NumberFormatException { - return toUnsignedString(num, 1); + 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); + + index++; + if (str.charAt(index) == 'x' || str.charAt(index) == 'X') + { + radix = 16; + index++; + } + else + radix = 8; + } + + if (index >= len) + throw new NumberFormatException("empty value"); + + return new Integer(parseInt(str, index, len, isNeg, radix)); } - public static String toHexString(int num) + /** Return the value of this Integer as a byte. + ** @return the value of this Integer as a byte. + **/ + public byte byteValue() { - return toUnsignedString(num, 4); + return (byte) value; } - public static String toOctalString(int num) + /** Return the value of this Integer as a short. + ** @return the value of this Integer as a short. + **/ + public short shortValue() { - return toUnsignedString(num, 3); + return (short) value; } - private static String toUnsignedString(int num, int exp) + /** Return the value of this Integer as an int. + ** @return the value of this Integer as an int. + **/ + public int intValue() { - // 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; - } - while (num != 0); - - return String.valueOf(buffer, i, 32-i); + return value; } - public String toString() + /** Return the value of this Integer as a long. + ** @return the value of this Integer as a long. + **/ + public long longValue() { - return toString(this.value); + return value; } - public static String toString(int num) + /** Return the value of this Integer as a float. + ** @return the value of this Integer as a float. + **/ + public float floatValue() { - return String.valueOf (num); + return value; } - public static String toString(int num, int radix) + /** Return the value of this Integer as a double. + ** @return the value of this Integer as a double. + **/ + public double doubleValue() { - // Use optimized method for the typical case. - if (radix == 10 || - radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) - return toString(num); - - // 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 value; } - public static Integer valueOf(String str) throws NumberFormatException + /** + * 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. + * + * @since 1.2 + */ + public int compareTo(Integer i) { - return new Integer(parseInt(str, 10)); + if (this.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; } - public static Integer valueOf(String str, int radix) - throws NumberFormatException + /** + * 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. + * + * @since 1.2 + */ + public int compareTo(Object o) { - return new Integer(parseInt(str, radix)); + return compareTo((Integer)o); } } -- cgit v1.2.1