diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 173a68933d74..56d89c868291 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,16 @@ +2001-07-23 Tom Tromey + + * 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. + 2001-07-22 Jeff Sturm * configure.host: Enable hash synchronization for alpha*-*. diff --git a/libjava/Makefile.am b/libjava/Makefile.am index 27e92f3d1c8c..debd21904f86 100644 --- a/libjava/Makefile.am +++ b/libjava/Makefile.am @@ -847,6 +847,7 @@ java/lang/UnsatisfiedLinkError.java \ java/lang/UnsupportedOperationException.java \ java/lang/VerifyError.java \ java/lang/VirtualMachineError.java \ +java/lang/VMClassLoader.java \ java/lang/Void.java \ java/io/BufferedInputStream.java \ java/io/BufferedOutputStream.java \ diff --git a/libjava/Makefile.in b/libjava/Makefile.in index 4c485e87528c..f792edb126b6 100644 --- a/libjava/Makefile.in +++ b/libjava/Makefile.in @@ -617,6 +617,7 @@ java/lang/UnsatisfiedLinkError.java \ java/lang/UnsupportedOperationException.java \ java/lang/VerifyError.java \ java/lang/VirtualMachineError.java \ +java/lang/VMClassLoader.java \ java/lang/Void.java \ java/io/BufferedInputStream.java \ java/io/BufferedOutputStream.java \ @@ -1663,8 +1664,9 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \ .deps/java/lang/ThreadLocal.P .deps/java/lang/Throwable.P \ .deps/java/lang/UnknownError.P .deps/java/lang/UnsatisfiedLinkError.P \ .deps/java/lang/UnsupportedOperationException.P \ -.deps/java/lang/VerifyError.P .deps/java/lang/VirtualMachineError.P \ -.deps/java/lang/Void.P .deps/java/lang/dtoa.P .deps/java/lang/e_acos.P \ +.deps/java/lang/VMClassLoader.P .deps/java/lang/VerifyError.P \ +.deps/java/lang/VirtualMachineError.P .deps/java/lang/Void.P \ +.deps/java/lang/dtoa.P .deps/java/lang/e_acos.P \ .deps/java/lang/e_asin.P .deps/java/lang/e_atan2.P \ .deps/java/lang/e_exp.P .deps/java/lang/e_fmod.P \ .deps/java/lang/e_log.P .deps/java/lang/e_pow.P \ diff --git a/libjava/gcj/Makefile.in b/libjava/gcj/Makefile.in index 4419fa6cd60a..6634da2a957a 100644 --- a/libjava/gcj/Makefile.in +++ b/libjava/gcj/Makefile.in @@ -95,6 +95,7 @@ LIBGCJTESTSPEC = @LIBGCJTESTSPEC@ LIBGCJ_CFLAGS = @LIBGCJ_CFLAGS@ LIBGCJ_CXXFLAGS = @LIBGCJ_CXXFLAGS@ LIBGCJ_JAVAFLAGS = @LIBGCJ_JAVAFLAGS@ +LIBICONV = @LIBICONV@ LIBLTDL = @LIBLTDL@ LIBTOOL = @LIBTOOL@ LN_S = @LN_S@ diff --git a/libjava/gcj/javaprims.h b/libjava/gcj/javaprims.h index a6111f8f777e..5608aed010fb 100644 --- a/libjava/gcj/javaprims.h +++ b/libjava/gcj/javaprims.h @@ -197,10 +197,13 @@ extern "Java" class Thread; class ThreadDeath; class ThreadGroup; + class ThreadLocal; + class ThreadLocal$Value; class Throwable; class UnknownError; class UnsatisfiedLinkError; class UnsupportedOperationException; + class VMClassLoader; class VerifyError; class VirtualMachineError; class Void; diff --git a/libjava/java/lang/Boolean.java b/libjava/java/lang/Boolean.java index 027fd24da830..91edeb92d7c0 100644 --- a/libjava/java/lang/Boolean.java +++ b/libjava/java/lang/Boolean.java @@ -1,100 +1,177 @@ -/* Copyright (C) 1998, 1999, 2000 Free Software Foundation +/* Boolean.java -- object wrapper for boolean + Copyright (C) 1998, 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; import java.io.Serializable; - + /** - * @author Warren Levy - * @date September 3, 1998. - */ -/* 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 Boolean extends Object implements Serializable + * Instances of class Boolean represent primitive + * boolean values. + * + * @author Paul Fisher + * @since JDK1.0 + */ +public final class Boolean implements Serializable { - public static final Boolean FALSE = new Boolean(false); - public static final Boolean TRUE = new Boolean(true); - - // This initialization is seemingly circular, but it is accepted - // by javac, and is handled specially by gcc. - public static final Class TYPE = boolean.class; - - /* The boolean value of the instance. */ - private boolean value; - - private static final long serialVersionUID = -3665804199014368530L; - - public Boolean(boolean boolVal) - { - value = boolVal; - } - - public Boolean(String strVal) - { - value = (strVal == null ? false : strVal.equalsIgnoreCase("true")); - } - - public boolean booleanValue() - { - return value; - } - - public boolean equals(Object obj) - { - /* Don't need to compare obj to null as instanceof will do this. */ - if (obj instanceof Boolean) - return value == ((Boolean) obj).value; - return false; - } - - public static boolean getBoolean(String property) - { - /* TBD: If a security manager exists and it doesn't permit accessing - * the property, it will throw an exception. Should we catch it? + static final long serialVersionUID = -3665804199014368530L; + + /** + * This field is a Boolean object representing the + * primitive value true. This instance is returned + * by the static valueOf() methods if they return + * a Boolean representing true. */ - try - { - String val = System.getProperty(property); - return val == null ? false : val.equalsIgnoreCase("true"); - } - catch (SecurityException e) - { - return false; - } - } - - public int hashCode() - { - /* These values are from the Java Lang. Spec. (Sec 20.4.7). - * TBD: They could be made private static final fields but they're only - * used here (and shouldn't be used anywhere else), though it might be - * useful to grep on something like JAVA_HASH_* values for us as - * developers. + public static final Boolean TRUE = new Boolean(true); + + /** + * This field is a Boolean object representing the + * primitive value false. This instance is returned + * by the static valueOf() methods if they return + * a Boolean representing false. */ - return value ? 1231 : 1237; - } + public static final Boolean FALSE = new Boolean(false); - public String toString() - { - return value ? "true" : "false"; - } + /** + * The primitive type boolean is represented by this + * Class object. + */ + public static final Class TYPE = VMClassLoader.getPrimitiveClass("boolean"); + + /** + * The immutable value of this Boolean. + */ + private final boolean value; + + /** + * Create a Boolean object representing the value of the + * argument value. In general the use of the static + * method valueof(boolean) is more efficient since it will + * not create a new object. + * + * @param value the primitive value of this Boolean + */ + public Boolean(boolean value) { + this.value = value; + } + + /** + * Creates a Boolean object representing the primitive + * true if and only if s matches + * the string "true" ignoring case, otherwise the object will represent + * the primitive false. In general the use of the static + * method valueof(String) is more efficient since it will + * not create a new object. + * + * @param s the String representation of true + * or false + */ + public Boolean(String s) { + value = "true".equalsIgnoreCase(s); + } - public static Boolean valueOf(String str) - { - if (str == null) - return FALSE; - else - /* This returns a Boolean (big B), not a boolean (little b). */ - return str.equalsIgnoreCase("true") ? TRUE : FALSE; - } + /** + * Return the primitive boolean value of this + * Boolean object. + */ + public boolean booleanValue() { + return value; + } + + /** + * Returns the Boolean TRUE if the given boolean is + * true, otherwise it will return the Boolean + * FALSE. + * + * @since 1.4 + */ + public static Boolean valueOf(boolean b) { + return b ? TRUE : FALSE; + } + + /** + * Returns the Boolean TRUE if and only if the given + * String is equal, ignoring case, to the the String "true", otherwise + * it will return the Boolean FALSE. + */ + public static Boolean valueOf(String s) { + return "true".equalsIgnoreCase(s) ? TRUE : FALSE; + } + + /** + * Returns the integer 1231 if this object represents + * the primitive true and the integer 1237 + * otherwise. + */ + public int hashCode() { + return (value) ? 1231 : 1237; + } + + /** + * If the obj is an instance of Boolean and + * has the same primitive value as this object then true + * is returned. In all other cases, including if the obj + * is null, false is returned. + * + * @param obj possibly an instance of any Class + * @return false is obj is an instance of + * Boolean and has the same primitive value as this + * object. + */ + public boolean equals(Object obj) { + return (obj instanceof Boolean && value == ((Boolean)obj).value); + } + + /** + * If the value of the system property name matches + * "true" ignoring case then the function returns true. + */ + public static boolean getBoolean(String name) { + String val = System.getProperty(name); + return ("true".equalsIgnoreCase(val)); + } + + /** + * Returns "true" if the value of the give boolean is true and + * returns "false" if the value of the given boolean is false. + * + * @since 1.4 + */ + public static String toString(boolean b) + { + return b ? "true" : "false"; + } + + /** + * Returns "true" if the value of this object is true and + * returns "false" if the value of this object is false. + */ + public String toString() + { + return (value) ? "true" : "false"; + } } diff --git a/libjava/java/lang/Byte.java b/libjava/java/lang/Byte.java index 347e2524fae8..964e073b7351 100644 --- a/libjava/java/lang/Byte.java +++ b/libjava/java/lang/Byte.java @@ -1,88 +1,193 @@ -/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation +/* Byte.java -- object wrapper for byte + Copyright (C) 1998, 2001 Free Software Foundation, Inc. - This file is part of libgcj. +This file is part of GNU Classpath. + +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. */ -This software is copyrighted work licensed under the terms of the -Libgcj License. Please consult the file "LIBGCJ_LICENSE" for -details. */ package java.lang; /** - * @author Per Bothner - * @date April 17, 1998. + * Instances of class Byte represent primitive byte + * values. + * + * Additionally, this class provides various helper functions and variables + * useful to bytes. + * + * @author Paul Fisher + * @author John Keiser + * @author Per Bothner + * @since JDK 1.0 */ -/* Written using "Java Class Libraries", 2nd edition, plus online - * API docs for JDK 1.2 beta from http://www.javasoft.com. - * Status: Believed complete and correct. - * Includes JDK 1.2 methods. - */ - -public final class Byte extends Number implements Comparable +public final class Byte extends Number implements Comparable { - byte value; + static final long serialVersionUID = -7183698231559129828L; - public final static byte MIN_VALUE = -128; - public final static byte MAX_VALUE = 127; + /** + * The minimum value a byte can represent is -128. + */ + public static final byte MIN_VALUE = -128; - // This initialization is seemingly circular, but it is accepted - // by javac, and is handled specially by gcc. - public static final Class TYPE = byte.class; + /** + * The maximum value a byte can represent is 127. + */ + public static final byte MAX_VALUE = 127; - private static final long serialVersionUID = -7183698231559129828L; + /** + * The primitive type byte is represented by this + * Class object. + */ + public static final Class TYPE = VMClassLoader.getPrimitiveClass("byte"); - public Byte(byte value) + /** + * The immutable value of this Byte. + */ + private final byte value; + + /** + * Create a Byte object representing the value of the + * byte argument. + * + * @param value the value to use + */ + public Byte(byte value) { this.value = value; } - public Byte(String str) - throws NumberFormatException + /** + * Create a Byte object representing the value specified + * by the String argument. + * + * @param s the string to convert. + */ + public Byte(String s) throws NumberFormatException { - this.value = parseByte(str, 10); + value = parseByte(s, 10); } - public byte byteValue() + /** + * Return a hashcode representing this Object. + * + * Byte's hash code is calculated by simply returning its + * value. + * + * @return this Object's hash code. + */ + public int hashCode() { return value; } - public short shortValue() + /** + * Returns true if obj is an instance of + * Byte and represents the same byte value. + * @return whether these Objects are semantically equal. + */ + public boolean equals(Object obj) { - return value; + return ((obj instanceof Byte) && (value == ((Byte)obj).byteValue())); } - public int intValue() + /** + * Converts the byte to a String and assumes + * a radix of 10. + * @param i the byte to convert to String + * @return the String representation of the argument. + */ + public static String toString(byte i) { - return value; + return Integer.toString ((int) i); } - public long longValue () + /** + * Converts the Byte value to a String and + * assumes a radix of 10. + * @return the String representation of this Byte. + */ + public String toString() { - return value; + return Integer.toString ((int) value); + } + + /** + * Creates a new Byte object using the String, + * assuming a radix of 10. + * @param s the String to convert. + * @return the new Byte. + * @see #Byte(java.lang.String) + * @see #parseByte(java.lang.String) + * @exception NumberFormatException thrown if the String + * cannot be parsed as a byte. + */ + public static Byte valueOf(String s) throws NumberFormatException + { + return new Byte(parseByte(s)); } - public float floatValue () + /** + * Creates a new Byte 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 Byte. + * @see #parseByte(java.lang.String,int) + * @exception NumberFormatException thrown if the String + * cannot be parsed as a byte. + */ + public static Byte valueOf(String s, int radix) + throws NumberFormatException { - return (float) value; + return new Byte(parseByte(s, radix)); } - public double doubleValue () + /** + * Converts the specified String into a byte. + * This function assumes a radix of 10. + * + * @param s the String to convert + * @return the byte value of the String + * argument. + * @exception NumberFormatException thrown if the String + * cannot be parsed as a byte. + */ + public static byte parseByte(String s) throws NumberFormatException { - return (double) value; + return parseByte(s, 10); } - public static Byte decode(String str) - throws NumberFormatException - { - int i = (Integer.decode(str)).intValue(); - if (i < MIN_VALUE || i > MAX_VALUE) - throw new NumberFormatException(); - return new Byte((byte) i); - } - - public static byte parseByte(String str, int radix) - throws NumberFormatException + /** + * Converts the specified String into a byte + * using the specified radix (base). + * + * @param str the String to convert + * @param radix the radix (base) to use in the conversion + * @return the String argument converted to byte. + * @exception NumberFormatException thrown if the String + * cannot be parsed as a byte. + */ + public static byte parseByte(String str, int radix) + throws NumberFormatException { int i = Integer.parseInt(str, radix); if (i < MIN_VALUE || i > MAX_VALUE) @@ -90,55 +195,109 @@ public final class Byte extends Number implements Comparable return (byte) i; } - public static byte parseByte(String str) - throws NumberFormatException + /** + * Convert the specified String into a Byte. + * 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 a Byte. + * @exception NumberFormatException thrown if the String + * cannot be parsed as a byte. + */ + public static Byte decode(String str) throws NumberFormatException { - return parseByte(str, 10); + int i = (Integer.decode(str)).intValue(); + if (i < MIN_VALUE || i > MAX_VALUE) + throw new NumberFormatException(); + return new Byte((byte) i); } - - public static Byte valueOf(String str, int radix) - throws NumberFormatException - { - return new Byte(parseByte(str, radix)); - } - - public static Byte valueOf(String str) - throws NumberFormatException - { - return valueOf(str, 10); - } - - // Added in JDK 1.2 - public int compareTo(Byte anotherByte) - { - return this.value - anotherByte.value; - } - - // Added in JDK 1.2 - /** @throws ClassCastException */ - public int compareTo(Object o) - { - return this.value - ((Byte) o).value; - } - - public boolean equals(Object obj) - { - return (obj instanceof Byte) && ((Byte)obj).value == value; - } - - // Verified that hashCode is returns plain value (see Boolean_1 test). - public int hashCode() + + /** Return the value of this Byte as an short. + ** @return the value of this Byte as an short. + **/ + public byte byteValue() { return value; } - public String toString() + /** Return the value of this Byte as an short. + ** @return the value of this Byte as an short. + **/ + public short shortValue() { - return Integer.toString((int) value); + return value; } - public static String toString(byte value) + /** Return the value of this Byte as an int. + ** @return the value of this Byte as an int. + **/ + public int intValue() { - return Integer.toString((int) value); + return value; + } + + /** Return the value of this Byte as a long. + ** @return the value of this Byte as a long. + **/ + public long longValue() + { + return value; + } + + /** Return the value of this Byte as a float. + ** @return the value of this Byte as a float. + **/ + public float floatValue() + { + return value; + } + + /** Return the value of this Byte as a double. + ** @return the value of this Byte as a double. + **/ + public double doubleValue() + { + return value; + } + + /** + * Compare two Bytes numerically by comparing their + * byte values. + * @return a positive value if this Byte is greater + * in value than the argument Byte; a negative value + * if this Byte is smaller in value than the argument + * Byte; and 0, zero, if this + * Byte is equal in value to the argument + * Byte. + */ + public int compareTo(Byte b) + { + return (int)(value - b.byteValue()); + } + + /** + * Behaves like compareTo(java.lang.Byte) unless the Object + * is not a Byte. Then it throws a + * ClassCastException. + * @exception ClassCastException if the argument is not a + * Byte. + */ + public int compareTo(Object o) + { + return compareTo((Byte)o); } } diff --git a/libjava/java/lang/Integer.java b/libjava/java/lang/Integer.java index 9ddfa312f06f..414d93d43a85 100644 --- a/libjava/java/lang/Integer.java +++ b/libjava/java/lang/Integer.java @@ -1,246 +1,187 @@ -/* 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); - } - - public byte byteValue() - { - return (byte) value; - } - - public double doubleValue() - { - return (double) value; - } - - public float floatValue() - { - return (float) value; - } - - public int intValue() - { - return value; - } - - public long longValue() - { - return value; - } - - public short shortValue() - { - return (short) value; - } - - // Added in JDK 1.2 - public int compareTo(Integer anotherInteger) - { - if (this.value == anotherInteger.value) - return 0; - - // Returns just -1 or 1 on inequality; doing math might overflow the int. - if (this.value > anotherInteger.value) - return 1; - - return -1; - } - - // Added in JDK 1.2 - /** @throws ClassCastException */ - public int compareTo(Object o) - { - return this.compareTo((Integer) o); - } - - public static Integer decode(String str) throws NumberFormatException - { - boolean isNeg = false; - int index = 0; - int radix = 10; - final int len; - - if ((len = str.length()) == 0) - throw new NumberFormatException(); - - // Negative numbers are always radix 10. - if (str.charAt(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); - - index++; - if (str.charAt(index) == 'x') - { - radix = 16; - index++; - } - else - radix = 8; - } - - 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); - } - - public static Integer getInteger(String prop) - { - return getInteger(prop, null); - } - - public static Integer getInteger(String prop, int defval) - { - Integer val = getInteger(prop, null); - return val == null ? new Integer(defval) : val; - } - - public static Integer getInteger(String prop, Integer defobj) - { - try - { - String val = System.getProperty(prop); - if (val != null) - return decode(val); - } - catch (NumberFormatException ex) - { - } - return defobj; + value = parseInt(s, 10); } + /** + * 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; } - public static int parseInt(String str) throws NumberFormatException + /** + * 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 parseInt(str, 10); + return obj instanceof Integer && value == ((Integer)obj).value; } - public static int parseInt(String str, int radix) throws NumberFormatException + /** + * 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) { - final int len; - - 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 getInteger(nm, null); } - private static int parseInt(String str, int index, int len, boolean isNeg, - int radix) throws NumberFormatException + /** + * 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) { - int val = 0; - int digval; + Integer result = getInteger(nm, null); + return (result == null) ? new Integer(val) : result; + } - 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++) + /** + * 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 { - 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 decode(nm); + } + catch (NumberFormatException e) + { + return def; } - - return isNeg ? -(val) : val; - } - - public static String toBinaryString(int num) - { - return toUnsignedString(num, 1); - } - - public static String toHexString(int num) - { - return toUnsignedString(num, 4); - } - - public static String toOctalString(int num) - { - return toUnsignedString(num, 3); } private static String toUnsignedString(int num, int exp) @@ -260,16 +201,70 @@ public final class Integer extends Number implements Comparable return String.valueOf(buffer, i, 32-i); } + /** + * 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) + { + return toUnsignedString(i, 4); + } + + /** + * 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); + } + + /** + * 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); + } + + /** + * 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) + { + // 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); + } + + /** + * 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(this.value); - } - - public static String toString(int num) - { - return String.valueOf (num); + 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. + */ public static String toString(int num, int radix) { // Use optimized method for the typical case. @@ -310,14 +305,266 @@ public final class Integer extends Number implements Comparable return String.valueOf(buffer, i, 33-i); } - public static Integer valueOf(String str) throws NumberFormatException + /** + * 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 { - return new Integer(parseInt(str, 10)); + return new Integer(parseInt(s)); } - public static Integer valueOf(String str, int radix) - throws NumberFormatException + /** + * 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 new Integer(parseInt(str, radix)); + return new Integer(parseInt(s, radix)); + } + + /** + * 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(s, 10); + } + + /** + * 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; + + 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); + } + + private static int parseInt(String str, int index, int len, boolean isNeg, + int radix) + throws NumberFormatException + { + 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 isNeg ? -(val) : val; + } + + /** + * 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 + { + 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)); + } + + /** Return the value of this Integer as a byte. + ** @return the value of this Integer as a byte. + **/ + public byte byteValue() + { + return (byte) value; + } + + /** Return the value of this Integer as a short. + ** @return the value of this Integer as a short. + **/ + public short shortValue() + { + return (short) value; + } + + /** Return the value of this Integer as an int. + ** @return the value of this Integer as an int. + **/ + public int intValue() + { + return value; + } + + /** Return the value of this Integer as a long. + ** @return the value of this Integer as a long. + **/ + public long longValue() + { + return value; + } + + /** Return the value of this Integer as a float. + ** @return the value of this Integer as a float. + **/ + public float floatValue() + { + return value; + } + + /** Return the value of this Integer as a double. + ** @return the value of this Integer as a double. + **/ + public double doubleValue() + { + return value; + } + + /** + * 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) + { + 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; + } + + /** + * 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 compareTo((Integer)o); } } diff --git a/libjava/java/lang/Long.java b/libjava/java/lang/Long.java index c33a43b6c2cb..addb7e9d83c4 100644 --- a/libjava/java/lang/Long.java +++ b/libjava/java/lang/Long.java @@ -1,260 +1,203 @@ -/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation +/* java.lang.Long + 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 18, 1998. + * Instances of class Double represent primitive + * double values. + * + * Additionally, this class provides various helper functions and variables + * related to longs. + * + * @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 Long extends Number implements Comparable { - public static final long MAX_VALUE = 0x7FFFFFFFFFFFFFFFL; + // compatible with JDK 1.0.2+ + static final long serialVersionUID = 4290774380558885855L; + + /** + * The minimum value a long can represent is + * -9223372036854775808. + */ public static final long MIN_VALUE = 0x8000000000000000L; - // This initialization is seemingly circular, but it is accepted - // by javac, and is handled specially by gcc. - public static final Class TYPE = long.class; + /** + * The maximum value a long can represent is + * 9223372036854775807. + */ + public static final long MAX_VALUE = 0x7fffffffffffffffL; - /* The long value of the instance. */ - private long value; + /** + * The primitive type long is represented by this + * Class object. + */ + public static final Class TYPE = VMClassLoader.getPrimitiveClass ("long"); - private static final long serialVersionUID = 4290774380558885855L; + /** + * The immutable value of this Long. + */ + private final long value; - public Long(long val) + /** + * Create a Long object representing the value of the + * long argument. + * + * @param value the value to use + */ + public Long(long value) { - value = val; + this.value = value; } - public Long(String str) throws NumberFormatException + /** + * Create a Long object representing the value of the + * argument after conversion to a long. + * + * @param s the string to convert. + */ + public Long(String s) throws NumberFormatException { - value = parseLong(str, 10); - } - - public byte byteValue() - { - return (byte) value; - } - - public double doubleValue() - { - return (double) value; - } - - public float floatValue() - { - return (float) value; - } - - public int intValue() - { - return (int) value; - } - - public long longValue() - { - return value; - } - - public short shortValue() - { - return (short) value; - } - - // Added in JDK 1.2 - public int compareTo(Long anotherLong) - { - if (this.value == anotherLong.value) - return 0; - - // Returns just -1 or 1 on inequality; doing math might overflow the long. - if (this.value > anotherLong.value) - return 1; - - return -1; - } - - // Added in JDK 1.2 - /** @throws ClassCastException */ - public int compareTo(Object o) - { - return this.compareTo((Long) o); - } - - // Added in JDK 1.2 - public static Long decode(String str) throws NumberFormatException - { - boolean isNeg = false; - int index = 0; - int radix = 10; - final int len; - - if ((len = str.length()) == 0) - throw new NumberFormatException(); - - // Negative numbers are always radix 10. - if (str.charAt(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 Long(0L); - - index++; - if (str.charAt(index) == 'x') - { - radix = 16; - index++; - } - else - radix = 8; - } - - if (index >= len) - throw new NumberFormatException(); - - return new Long(parseLong(str, index, len, isNeg, radix)); + value = parseLong(s, 10); } + /** + * If the Object is not null, is an + * instanceof Long, and represents + * the same primitive long value return + * true. Otherwise false is returned. + */ public boolean equals(Object obj) { - return (obj instanceof Long && ((Long) obj).value == value); - } - - public static Long getLong(String prop) - { - return getLong(prop, null); - } - - public static Long getLong(String prop, long defval) - { - Long val = getLong(prop, null); - return val == null ? new Long(defval) : val; - } - - public static Long getLong(String prop, Long defobj) - { - try - { - String val = System.getProperty(prop); - if (val != null) - return decode(val); - } - catch (NumberFormatException ex) - { - } - return defobj; + return obj instanceof Long && ((Long)obj).value == value; } + /** + * Return a hashcode representing this Object. + * + * Long's hash code is calculated by simply returning its + * value. + * + * @return this Object's hash code. + */ public int hashCode() { - return (int)(this.longValue()^(this.longValue()>>>32)); + return (int)(value^(value>>>32)); } - public static long parseLong(String str) throws NumberFormatException + /** + * Get the specified system property as a Long. + * + * A method similar to Integer's decode() will be + * used to interpret the value of the property. + * + * @param nm the name of the system property + * @return the system property as an Long, or + * null if the property is not found or cannot be + * decoded as a Long. + * @see java.lang.System#getProperty(java.lang.String) + * @see java.lang.Integer#decode(int) + */ + public static Long getLong(String nm) { - return parseLong(str, 10); + return getLong(nm, null); } - public static long parseLong(String str, int radix) - throws NumberFormatException + /** + * Get the specified system property as an Long, or use a + * default long value if the property is not found or is not + * decodable. + * + * A method similar to Integer's decode() 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 a Long, or the default + * value if the property is not found or cannot be decoded as a + * Long. + * @see java.lang.System#getProperty(java.lang.String) + * @see java.lang.Integer#decode(int) + * @see #getLong(java.lang.String,java.lang.Long) + */ + public static Long getLong(String nm, long val) { - final int len; - - 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 parseLong(str, index, len, isNeg, radix); + Long result = getLong(nm, null); + return (result == null) ? new Long(val) : result; } - private static long parseLong(String str, int index, int len, boolean isNeg, - int radix) throws NumberFormatException + /** + * Get the specified system property as an Long, or use a + * default Long 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 Long, or the default + * value if the property is not found or cannot be decoded as an + * Long. + * @see java.lang.System#getProperty(java.lang.String) + * @see java.lang.Integer#decode(int) + * @see #getLong(java.lang.String,long) + */ + public static Long getLong(String nm, Long def) { - long val = 0; - int digval; - - long 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++) + String val = System.getProperty(nm); + if (val == null) + return def; + try { - 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 decode(nm); + } + catch (NumberFormatException e) + { + return def; } - - return isNeg ? -(val) : val; - } - - public static String toBinaryString(long num) - { - return toUnsignedString(num, 1); - } - - public static String toHexString(long num) - { - return toUnsignedString(num, 4); - } - - public static String toOctalString(long num) - { - return toUnsignedString(num, 3); } private static String toUnsignedString(long num, int exp) { // Use an array large enough for a binary number. int radix = 1 << exp; - long mask = radix - 1; + int mask = radix - 1; char[] buffer = new char[64]; int i = 64; do { - buffer[--i] = Character.forDigit((int) (num & mask), radix); + buffer[--i] = Character.forDigit((int) num & mask, radix); num = num >>> exp; } while (num != 0); @@ -262,11 +205,44 @@ public final class Long extends Number implements Comparable return String.valueOf(buffer, i, 64-i); } - public String toString() + /** + * Converts the long to a String assuming it is + * unsigned in base 16. + * @param i the long to convert to String + * @return the String representation of the argument. + */ + public static String toHexString(long i) { - return toString(this.value); + return toUnsignedString(i, 4); } + /** + * Converts the long to a String assuming it is + * unsigned in base 8. + * @param i the long to convert to String + * @return the String representation of the argument. + */ + public static String toOctalString(long i) + { + return toUnsignedString(i, 3); + } + + /** + * Converts the long to a String assuming it is + * unsigned in base 2. + * @param i the long to convert to String + * @return the String representation of the argument. + */ + public static String toBinaryString(long i) { + return toUnsignedString(i, 1); + } + + /** + * Converts the long to a String and assumes + * a radix of 10. + * @param num the long to convert to String + * @return the String representation of the argument. + */ public static String toString(long num) { // Use the Integer toString for efficiency if possible. @@ -304,6 +280,23 @@ public final class Long extends Number implements Comparable return String.valueOf(buffer, i, 20-i); } + /** + * Converts the Long value to a String and + * assumes a radix of 10. + * @return the String representation of this Long. + */ + public String toString() + { + return toString(value); + } + + /** + * Converts the long to a String using + * the specified radix (base). + * @param num the long 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(long num, int radix) { // Use optimized method for the typical case. @@ -348,15 +341,242 @@ public final class Long extends Number implements Comparable return String.valueOf(buffer, i, 65-i); } - - public static Long valueOf(String str) throws NumberFormatException + + /** + * Creates a new Long object using the String, + * assuming a radix of 10. + * @param s the String to convert. + * @return the new Long. + * @see #Long(java.lang.String) + * @see #parseLong(java.lang.String) + * @exception NumberFormatException thrown if the String + * cannot be parsed as a long. + */ + public static Long valueOf(String s) throws NumberFormatException { - return new Long(parseLong(str, 10)); + return new Long(parseLong(s)); } - public static Long valueOf(String str, int radix) - throws NumberFormatException + /** + * Creates a new Long 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 Long. + * @see #parseLong(java.lang.String,int) + * @exception NumberFormatException thrown if the String + * cannot be parsed as a long. + */ + public static Long valueOf(String s, int radix) throws NumberFormatException { - return new Long(parseLong(str, radix)); + return new Long(parseLong(s, radix)); + } + + /** + * Converts the specified String into a long. + * This function assumes a radix of 10. + * + * @param s the String to convert + * @return the long value of the String + * argument. + * @exception NumberFormatException thrown if the String + * cannot be parsed as a long. + */ + public static long parseLong(String s) throws NumberFormatException + { + return parseLong(s, 10); + } + + /** + * Converts the specified String into a long + * 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 long. + * @exception NumberFormatException thrown if the String + * cannot be parsed as a long. + */ + public static long parseLong(String str, int radix) + throws NumberFormatException + { + final int len; + + 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 parseLong(str, index, len, isNeg, radix); + } + + public static Long decode(String str) throws NumberFormatException + { + boolean isNeg = false; + int index = 0; + int radix = 10; + final int len; + + if ((len = str.length()) == 0) + throw new NumberFormatException(); + + // Negative numbers are always radix 10. + if (str.charAt(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 Long(0L); + + index++; + if (str.charAt(index) == 'x') + { + radix = 16; + index++; + } + else + radix = 8; + } + + if (index >= len) + throw new NumberFormatException(); + + return new Long(parseLong(str, index, len, isNeg, radix)); + } + + private static long parseLong(String str, int index, int len, boolean isNeg, + int radix) throws NumberFormatException + { + long val = 0; + int digval; + + long 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 isNeg ? -(val) : val; + } + + /** Return the value of this Long as an short. + ** @return the value of this Long as an short. + **/ + public byte byteValue() + { + return (byte) value; + } + + /** Return the value of this Long as an short. + ** @return the value of this Long as an short. + **/ + public short shortValue() + { + return (short) value; + } + + /** Return the value of this Long as an int. + ** @return the value of this Long as an int. + **/ + public int intValue() + { + return (int) value; + } + + /** Return the value of this Long as a long. + ** @return the value of this Long as a long. + **/ + public long longValue() + { + return value; + } + + /** Return the value of this Long as a float. + ** @return the value of this Long as a float. + **/ + public float floatValue() + { + return value; + } + + /** Return the value of this Long as a double. + ** @return the value of this Long as a double. + **/ + public double doubleValue() + { + return value; + } + + /** + * Compare two Longs numerically by comparing their + * long values. + * @return a positive value if this Long is greater + * in value than the argument Long; a negative value + * if this Long is smaller in value than the argument + * Long; and 0, zero, if this + * Long is equal in value to the argument + * Long. + * + * @since 1.2 + */ + public int compareTo(Long l) + { + if (this.value == l.value) + return 0; + + // Returns just -1 or 1 on inequality; doing math might overflow the long. + if (this.value > l.value) + return 1; + + return -1; + } + + /** + * Behaves like compareTo(java.lang.Long) unless the Object + * is not a Long. Then it throws a + * ClassCastException. + * @exception ClassCastException if the argument is not a + * Long. + * + * @since 1.2 + */ + public int compareTo(Object o) + { + return compareTo((Long)o); } } diff --git a/libjava/java/lang/Number.java b/libjava/java/lang/Number.java index 92d98afbfb67..7eb8bee22da5 100644 --- a/libjava/java/lang/Number.java +++ b/libjava/java/lang/Number.java @@ -1,41 +1,83 @@ -/* Copyright (C) 1998, 1999, 2000 Free Software Foundation +/* java.lang.Number + Copyright (C) 1998, 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; import java.io.Serializable; - + /** - * @author Warren Levy - * @date September 2, 1998. - */ -/* 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. - */ - + ** Number is a generic superclass of all the numeric classes, namely + ** Byte, Short, Integer, + ** Long, Float, and Double. + ** + ** It provides ways to convert from any one value to any other. + ** + ** @author Paul Fisher + ** @author John Keiser + ** @author Warren Levy + ** @since JDK1.0 + **/ public abstract class Number implements Serializable { - public byte byteValue() // Became non-abstract in JDK 1.2 + /** Return the value of this Number as a byte. + ** @return the value of this Number as a byte. + **/ + public byte byteValue() { return (byte) intValue(); } - public abstract double doubleValue(); - public abstract float floatValue(); - public abstract int intValue(); - public abstract long longValue(); - - public short shortValue() // Became non-abstract in JDK 1.2 + /** Return the value of this Number as a short. + ** @return the value of this Number as a short. + **/ + public short shortValue() { return (short) intValue(); } + /** Return the value of this Number as an int. + ** @return the value of this Number as an int. + **/ + public abstract int intValue(); + + /** Return the value of this Number as a long. + ** @return the value of this Number as a long. + **/ + public abstract long longValue(); + + /** Return the value of this Number as a float. + ** @return the value of this Number as a float. + **/ + public abstract float floatValue(); + + /** Return the value of this Number as a float. + ** @return the value of this Number as a float. + **/ + public abstract double doubleValue(); + private static final long serialVersionUID = -8742448824652078965L; } diff --git a/libjava/java/lang/Short.java b/libjava/java/lang/Short.java index 66eb4fdfeedd..db3cac0f095b 100644 --- a/libjava/java/lang/Short.java +++ b/libjava/java/lang/Short.java @@ -1,144 +1,307 @@ -/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation +/* java.lang.Short + Copyright (C) 1998, 2001 Free Software Foundation, Inc. - This file is part of libgcj. +This file is part of GNU Classpath. + +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. */ -This software is copyrighted work licensed under the terms of the -Libgcj License. Please consult the file "LIBGCJ_LICENSE" for -details. */ package java.lang; /** - * @author Per Bothner - * @date April 17, 1998. + * Instances of class Short represent primitive + * short values. + * + * Additionally, this class provides various helper functions and variables + * related to shorts. + * + * @author Paul Fisher + * @author John Keiser + * @since JDK 1.0 */ -/* Written using "Java Class Libraries", 2nd edition, plus online - * API docs for JDK 1.2 beta from http://www.javasoft.com. - * Status: Believed complete and correct. - * Includes JDK 1.2 methods. - */ - public final class Short extends Number implements Comparable { - short value; + static final long serialVersionUID = 7515723908773894738L; - public final static short MIN_VALUE = -32768; - public final static short MAX_VALUE = 32767; + /** + * The minimum value a short can represent is -32768. + */ + public static final short MIN_VALUE = -32768; - // This initialization is seemingly circular, but it is accepted - // by javac, and is handled specially by gcc. - public static final Class TYPE = short.class; + /** + * The minimum value a short can represent is 32767. + */ + public static final short MAX_VALUE = 32767; - private static final long serialVersionUID = 7515723908773894738L; + /** + * The primitive type short is represented by this + * Class object. + */ + public static final Class TYPE = VMClassLoader.getPrimitiveClass("short"); + /** + * The immutable value of this Short. + */ + private final short value; + + /** + * Create a Short object representing the value of the + * short argument. + * + * @param value the value to use + */ public Short(short value) { this.value = value; } - public Short(String str) - throws NumberFormatException + /** + * Create a Short object representing the value of the + * argument after conversion to a short. + * + * @param s the string to convert. + */ + public Short(String s) throws NumberFormatException { - this.value = parseShort(str, 10); + value = parseShort(s, 10); } - public byte byteValue() - { - return (byte) value; - } - - public short shortValue() - { - return value; - } - - public int intValue() - { - return value; - } - - public long longValue () - { - return value; - } - - public float floatValue () - { - return (float) value; - } - - public double doubleValue () - { - return (double) value; - } - - public static Short decode(String str) - throws NumberFormatException - { - int i = (Integer.decode(str)).intValue(); - if (i < MIN_VALUE || i > MAX_VALUE) - throw new NumberFormatException(); - return new Short((short) i); - } - - public static short parseShort(String str, int radix) - throws NumberFormatException - { - int i = Integer.parseInt(str, radix); - if (i < MIN_VALUE || i > MAX_VALUE) - throw new NumberFormatException(); - return (short) i; - } - - public static short parseShort(String str) - throws NumberFormatException - { - return parseShort(str, 10); - } - - public static Short valueOf(String str, int radix) - throws NumberFormatException - { - return new Short(parseShort(str, radix)); - } - - public static Short valueOf(String str) - throws NumberFormatException - { - return valueOf(str, 10); - } - - // Added in JDK 1.2 - public int compareTo(Short anotherShort) - { - return this.value - anotherShort.value; - } - - // Added in JDK 1.2 - /** @throws ClassCastException */ - public int compareTo(Object o) - { - return this.value - ((Short) o).value; - } - - public boolean equals(Object obj) - { - return (obj instanceof Short) && ((Short) obj).value == value; - } - - // Verified that hashCode is returns plain value (see Short_1 test). + /** + * Return a hashcode representing this Object. + * + * Short's hash code is calculated by simply returning its + * value. + * + * @return this Object's hash code. + */ public int hashCode() { return value; } - public String toString() + /** + * If the Object is not null, is an + * instanceof Short, and represents + * the same primitive short value return + * true. Otherwise false is returned. + */ + public boolean equals(Object obj) { - return Integer.toString((int) value); + return obj instanceof Short && ((Short)obj).value == value; } - public static String toString(short value) + /** + * Converts the short to a String and assumes + * a radix of 10. + * @param i the short to convert to String + * @return the String representation of the argument. + */ + public static String toString(short i) { - return Integer.toString((int) value); + return Integer.toString((int) i); + } + + /** + * Converts the Short value to a String and + * assumes a radix of 10. + * @return the String representation of this Short. + */ + public String toString() + { + return Integer.toString ((int) value); + } + + /** + * Creates a new Short object using the String, + * assuming a radix of 10. + * @param s the String to convert. + * @return the new Short. + * @see #Short(java.lang.String) + * @see #parseShort(java.lang.String) + * @exception NumberFormatException thrown if the String + * cannot be parsed as a short. + */ + public static Short valueOf(String s) throws NumberFormatException + { + return new Short(parseShort(s)); + } + + /** + * Creates a new Short 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 Short. + * @see #parseShort(java.lang.String,int) + * @exception NumberFormatException thrown if the String + * cannot be parsed as a short. + */ + public static Short valueOf(String s, int radix) + throws NumberFormatException + { + return new Short(parseShort(s, radix)); + } + + /** + * Converts the specified String into a short. + * This function assumes a radix of 10. + * + * @param s the String to convert + * @return the short value of the String + * argument. + * @exception NumberFormatException thrown if the String + * cannot be parsed as a short. + */ + public static short parseShort(String s) throws NumberFormatException + { + return parseShort(s, 10); + } + + /** + * Converts the specified String into a short + * 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 short. + * @exception NumberFormatException thrown if the String + * cannot be parsed as a short. + */ + public static short parseShort(String s, int radix) + throws NumberFormatException + { + int i = Integer.parseInt(s, radix); + if (i < MIN_VALUE || i > MAX_VALUE) + throw new NumberFormatException(); + return (short) i; + } + + /** + * Convert the specified String into a Short. + * 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 s the String to interpret. + * @return the value of the String as a Short. + * @exception NumberFormatException thrown if the String + * cannot be parsed as a short. + */ + public static Short decode(String s) throws NumberFormatException + { + int i = (Integer.decode(s)).intValue(); + if (i < MIN_VALUE || i > MAX_VALUE) + throw new NumberFormatException(); + return new Short((short) i); + } + + /** Return the value of this Short as an short. + ** @return the value of this Short as an short. + **/ + public byte byteValue() + { + return (byte) value; + } + + /** Return the value of this Short as an short. + ** @return the value of this Short as an short. + **/ + public short shortValue() + { + return value; + } + + /** Return the value of this Short as an int. + ** @return the value of this Short as an int. + **/ + public int intValue() + { + return value; + } + + /** Return the value of this Short as a long. + ** @return the value of this Short as a long. + **/ + public long longValue() + { + return value; + } + + /** Return the value of this Short as a float. + ** @return the value of this Short as a float. + **/ + public float floatValue() + { + return value; + } + + /** Return the value of this Short as a double. + ** @return the value of this Short as a double. + **/ + public double doubleValue() + { + return value; + } + + /** + * Compare two Shorts numerically by comparing their + * short values. + * @return a positive value if this Short is greater + * in value than the argument Short; a negative value + * if this Short is smaller in value than the argument + * Short; and 0, zero, if this + * Short is equal in value to the argument + * Short. + * + * @since 1.2 + */ + public int compareTo(Short s) + { + return value - s.value; + } + + /** + * Behaves like compareTo(java.lang.Short) unless the Object + * is not a Short. Then it throws a + * ClassCastException. + * @exception ClassCastException if the argument is not a + * Short. + * + * @since 1.2 + */ + public int compareTo(Object o) + { + return compareTo((Short)o); } } diff --git a/libjava/java/lang/VMClassLoader.java b/libjava/java/lang/VMClassLoader.java new file mode 100644 index 000000000000..de0db8874344 --- /dev/null +++ b/libjava/java/lang/VMClassLoader.java @@ -0,0 +1,85 @@ +/* + * java.lang.ClassLoader: part of the Java Class Libraries project. + * Copyright (C) 1998, 2001 Free Software Foundation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +package java.lang; + +/** + * java.lang.VMClassLoader is a package-private helper for VMs to implement + * on behalf of java.lang.ClassLoader. + * + * @author John Keiser + * @version 1.1.0, Sep 22 1998 + * @since CP1.1 + */ + +class VMClassLoader { + + /** + * Helper to define a class using a string of bytes. + * + * @param name the name to give the class. null if unknown. + * @param data the data representing the classfile, in classfile format. + * @param offset the offset into the data where the classfile starts. + * @param len the length of the classfile data in the array. + * @return the class that was defined. + * @exception ClassFormatError if the byte array is not in proper classfile format. + */ + // Not yet needed for libgcj. +// final static native Class defineClass(ClassLoader cl, String name, +// byte[] data, int offset, int len) throws ClassFormatError; + + /** + * Helper to resolve all references to other classes from this class. + * @param c the class to resolve. + */ + // Not yet needed for libgcj. + // final static native void resolveClass(Class c); + + /** + * Helper for java.lang.Integer, Byte, etc. to get the TYPE class + * at initialization time. If there are multiple classloaders, this + * method may be called once per ClassLoader per type. + * + * @param type name of the primitive type; i.e. "int", "byte", etc. + * @return a "bogus" class representing the primitive type. + */ + static final Class getPrimitiveClass(String type) + { + if ("int".equals (type)) + return int.class; + else if ("long".equals (type)) + return int.class; + else if ("boolean".equals (type)) + return int.class; + else if ("short".equals (type)) + return int.class; + else if ("char".equals (type)) + return int.class; + else if ("byte".equals (type)) + return int.class; + else if ("float".equals (type)) + return int.class; + else if ("double".equals (type)) + return int.class; + else if ("void".equals (type)) + return int.class; + return null; + } +}