From 73e7bfc1dcdc87450a9429ff3a05a7a6fb873eeb Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 28 Feb 2008 08:33:52 -0700 Subject: [PATCH 1/4] Added constructor for java.util.HashMap --- classpath/java/util/HashMap.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/classpath/java/util/HashMap.java b/classpath/java/util/HashMap.java index b94bb54fd3..b357ad1696 100644 --- a/classpath/java/util/HashMap.java +++ b/classpath/java/util/HashMap.java @@ -32,6 +32,13 @@ public class HashMap implements Map { this(0); } + public HashMap(Map map) { + this(map.size()); + for (Map.Entry entry : map.entrySet()) { + put(entry.getKey(), entry.getValue()); + } + } + public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{"); From 0d5b7bd126347d2dcacec4e3f5a23f8699f4b533 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 28 Feb 2008 08:35:16 -0700 Subject: [PATCH 2/4] Support for floating point conversions and tests Floats and doubles can now be read from strings, using the C standard library functions for this purpose (strtof and strtod). The code also relies on standard library functions to implement isNaN() and isInfinite() --- classpath/java-lang.cpp | 64 +++++++++++++++++++++++++++++++++ classpath/java/lang/Double.java | 27 ++++++++++++-- classpath/java/lang/Float.java | 31 ++++++++++++++-- 3 files changed, 118 insertions(+), 4 deletions(-) diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index 08cb08bc65..f23464fc12 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -431,6 +431,70 @@ Java_java_lang_System_doMapLibraryName(JNIEnv* e, jclass, jstring name) return r; } +extern "C" JNIEXPORT jboolean JNICALL +Java_java_lang_Double_isInfinite(JNIEnv*, jclass, jdouble val) +{ + return !isfinite(val); +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_java_lang_Double_isNaN(JNIEnv*, jclass, jdouble val) +{ + return isnan(val); +} + +extern "C" JNIEXPORT jdouble JNICALL +Java_java_lang_Double_doubleFromString(JNIEnv*e, jclass, jstring s, + jintArray numDoublesRead) +{ + const char* chars = e->GetStringUTFChars(s, 0); + double d = 0.0; + int numRead = 0; + + if (chars) { + char* lastRead; + d = strtod(chars, &lastRead); + if ((lastRead != chars) && ((chars + strlen(chars)) == lastRead)) { + numRead = 1; + } + e->ReleaseStringUTFChars(s, chars); + } + e->SetIntArrayRegion(numDoublesRead, 0, 1, &numRead); + return d; +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_java_lang_Float_isInfinite(JNIEnv*, jclass, jfloat val) +{ + return !isfinite(val); +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_java_lang_Float_isNaN(JNIEnv*, jclass, jfloat val) +{ + return isnan(val); +} + +extern "C" JNIEXPORT jfloat JNICALL +Java_java_lang_Float_floatFromString(JNIEnv*e, jclass, jstring s, + jintArray numFloatsRead) +{ + const char* chars = e->GetStringUTFChars(s, 0); + float f = 0.0; + int numRead = 0; + + if (chars) { + char* lastRead; + f = strtof(chars, &lastRead); + if ((lastRead != chars) && ((chars + strlen(chars)) == lastRead)) { + numRead = 1; + } + e->ReleaseStringUTFChars(s, chars); + } + e->SetIntArrayRegion(numFloatsRead, 0, 1, &numRead); + return f; +} + extern "C" JNIEXPORT jdouble JNICALL Java_java_lang_Math_sin(JNIEnv*, jclass, jdouble val) { diff --git a/classpath/java/lang/Double.java b/classpath/java/lang/Double.java index d9dc8314a5..a58f9fcb39 100644 --- a/classpath/java/lang/Double.java +++ b/classpath/java/lang/Double.java @@ -31,6 +31,10 @@ public final class Double extends Number { return new Double(value); } + public static Double valueOf(String s) { + return new Double(s); + } + public boolean equals(Object o) { return o instanceof Double && ((Double) o).value == value; } @@ -74,9 +78,22 @@ public final class Double extends Number { return value; } + public boolean isInfinite() { + return isInfinite(value); + } + + public boolean isNaN() { + return isNaN(value); + } + public static double parseDouble(String s) { - // todo - throw new NumberFormatException(s); + int[] numRead = new int[1]; + double d = doubleFromString(s, numRead); + if (numRead[0] == 1) { + return d; + } else { + throw new NumberFormatException(s); + } } public static native int fillBufferWithDouble(double value, byte[] buffer, @@ -85,4 +102,10 @@ public final class Double extends Number { public static native long doubleToRawLongBits(double value); public static native double longBitsToDouble(long bits); + + public static native boolean isInfinite(double value); + + public static native boolean isNaN(double value); + + public static native double doubleFromString(String s, int[] numRead); } diff --git a/classpath/java/lang/Float.java b/classpath/java/lang/Float.java index 85e2b54d72..1e2c62972b 100644 --- a/classpath/java/lang/Float.java +++ b/classpath/java/lang/Float.java @@ -15,6 +15,10 @@ public final class Float extends Number { private final float value; + public Float(String value) { + this.value = parseFloat(value); + } + public Float(float value) { this.value = value; } @@ -23,6 +27,10 @@ public final class Float extends Number { return new Float(value); } + public static Float valueOf(String s) { + return new Float(s); + } + public boolean equals(Object o) { return o instanceof Float && ((Float) o).value == value; } @@ -63,12 +71,31 @@ public final class Float extends Number { return (double) value; } + public boolean isInfinite() { + return isInfinite(value); + } + + public boolean isNaN() { + return isNaN(value); + } + public static float parseFloat(String s) { - // todo - throw new NumberFormatException(s); + int[] numRead = new int[1]; + float f = floatFromString(s, numRead); + if (numRead[0] == 1) { + return f; + } else { + throw new NumberFormatException(s); + } } public static native int floatToRawIntBits(float value); public static native float intBitsToFloat(int bits); + + public static native boolean isInfinite(float value); + + public static native boolean isNaN(float value); + + public static native float floatFromString(String s, int[] numRead); } From 11d218f9566df638af256e7b162360d68109a426 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 28 Feb 2008 10:03:24 -0700 Subject: [PATCH 3/4] Slight fix for improved type safety --- classpath/java-lang.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index f23464fc12..be5b7408aa 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -449,7 +449,7 @@ Java_java_lang_Double_doubleFromString(JNIEnv*e, jclass, jstring s, { const char* chars = e->GetStringUTFChars(s, 0); double d = 0.0; - int numRead = 0; + jint numRead = 0; if (chars) { char* lastRead; @@ -481,7 +481,7 @@ Java_java_lang_Float_floatFromString(JNIEnv*e, jclass, jstring s, { const char* chars = e->GetStringUTFChars(s, 0); float f = 0.0; - int numRead = 0; + jint numRead = 0; if (chars) { char* lastRead; From e23f2bafd5c6c377d33ee32309b7f0d20d78b1f4 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 28 Feb 2008 11:02:58 -0700 Subject: [PATCH 4/4] Implemented trivial impolementation of java.util.Random.nextDouble() --- classpath/java/util/Random.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/classpath/java/util/Random.java b/classpath/java/util/Random.java index 975b5b8b13..2ab3f95a45 100644 --- a/classpath/java/util/Random.java +++ b/classpath/java/util/Random.java @@ -18,4 +18,8 @@ public class Random { public int nextInt() { return (int)(Math.random()*Integer.MAX_VALUE); } + + public double nextDouble() { + return Math.random(); + } }