Skip to content

Commit b96fb7d

Browse files
committed
Double methods in Clazz.js; PT fix for map.toJSON
Double methods for -0 and NaN corrections; allows non-string map keys in toJSON
1 parent 7ba7642 commit b96fb7d

File tree

10 files changed

+83
-60
lines changed

10 files changed

+83
-60
lines changed
144 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20221112121850
1+
20221205113414
144 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20221112121850
1+
20221205113414
29.8 KB
Binary file not shown.

sources/net.sf.j2s.java.core/src/javajs/util/PT.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
package javajs.util;
2929

3030
import java.lang.reflect.Array;
31+
import java.util.Arrays;
3132
import java.util.Map;
33+
import java.util.Set;
3234
import java.util.Map.Entry;
3335

3436
import javajs.api.JSONEncodable;
@@ -870,10 +872,14 @@ public static String toJSON(String infoType, Object info) {
870872
if (info instanceof Map) {
871873
sb.append("{ ");
872874
String sep = "";
873-
for (String key : ((Map<String, ?>) info).keySet()) {
874-
if (key == null)
875-
key = "null";
876-
sb.append(sep).append(packageJSON(key, toJSON(null, ((Map<?, ?>) info).get(key))));
875+
Set<?> keys = ((Map<?, ?>) info).keySet();
876+
Object[] okeys = keys.toArray();
877+
Arrays.sort(okeys);
878+
for (int i = 0, n = okeys.length; i < n; i++) {
879+
String key = okeys[i].toString();
880+
if (key == null)
881+
key = "null";
882+
sb.append(sep).append(packageJSON(key, toJSON(null, ((Map<?, ?>) info).get(okeys[i]))));
877883
sep = ",";
878884
}
879885
sb.append(" }");
@@ -895,12 +901,13 @@ public static String toJSON(String infoType, Object info) {
895901
sb.append("[");
896902
int n = AU.getLength(info);
897903
Object o = null;
898-
/** @j2sNative
899-
* o = info[0];
900-
* typeof o != "number" && typeof 0 != "boolean" && (o = null);
901-
*/
902-
{}
903-
if (o != null) {
904+
/**
905+
* @j2sNative o = info[0]; typeof o != "number" && typeof 0 != "boolean" && (o =
906+
* null);
907+
*/
908+
{
909+
}
910+
if (o != null) {
904911
sb.appendO(info);
905912
} else {
906913
for (int i = 0; i < n; i++) {
@@ -1567,13 +1574,6 @@ public static boolean isWhitespace(char ch) {
15671574
public static final double FRACTIONAL_PRECISION = 100000d;
15681575
public static final double CARTESIAN_PRECISION = 10000d;
15691576

1570-
public static void fixPtFloats(T3 pt, double d) {
1571-
//this will equate float and double as long as -256 <= x <= 256
1572-
pt.x = (float) (Math.round(pt.x * d) / d);
1573-
pt.y = (float) (Math.round(pt.y * d) / d);
1574-
pt.z = (float) (Math.round(pt.z * d) / d);
1575-
}
1576-
15771577
public static double fixDouble(double d, double f) {
15781578
return Math.round(d * f) / f;
15791579
}
@@ -1823,13 +1823,6 @@ public static double toDouble(float f) {
18231823
}
18241824
}
18251825

1826-
public static void fixPtDoubles(T3d pt, double f) {
1827-
//this will equate float and double as long as -256 <= x <= 256
1828-
pt.x = Math.round(pt.x * f) / f;
1829-
pt.y = Math.round(pt.y * f) / f;
1830-
pt.z = Math.round(pt.z * f) / f;
1831-
}
1832-
18331826
//static {
18341827
//
18351828
// double d = 790.8999998888;

sources/net.sf.j2s.java.core/src/javax/swing/SwingUtilities.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -813,21 +813,21 @@ public static boolean isRightMouseButton(MouseEvent anEvent) {
813813
return ((anEvent.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK);
814814
}
815815

816-
// /**
817-
// * Compute the width of the string using a font with the specified
818-
// * "metrics" (sizes).
819-
// *
820-
// * @param fm a FontMetrics object to compute with
821-
// * @param str the String to compute
822-
// * @return an int containing the string width
823-
// */
824-
// public static int computeStringWidth(FontMetrics fm,String str) {
825-
// // You can't assume that a string's width is the sum of its
826-
// // characters' widths in Java2D -- it may be smaller due to
827-
// // kerning, etc.
828-
// return SwingUtilities2.stringWidth(null, fm, str);
829-
// }
830-
//
816+
/**
817+
* Compute the width of the string using a font with the specified
818+
* "metrics" (sizes).
819+
*
820+
* @param fm a FontMetrics object to compute with
821+
* @param str the String to compute
822+
* @return an int containing the string width
823+
*/
824+
public static int computeStringWidth(FontMetrics fm,String str) {
825+
// You can't assume that a string's width is the sum of its
826+
// characters' widths in Java2D -- it may be smaller due to
827+
// kerning, etc.
828+
return SwingUtilities2.stringWidth(null, fm, str);
829+
}
830+
831831
/**
832832
* Compute and return the location of the icons origin, the
833833
* location of origin of the text baseline, and a possibly clipped

sources/net.sf.j2s.java.core/src/test/Test_Double.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ strictfp class Test_Double extends Test_{
77

88
public static void main(String[] args) {
99

10+
assert(Double.isNaN(new Double(Double.NaN)));
1011

11-
Double d = new Double("-0");
12-
Double d1 = Double.valueOf(-0);
12+
try {
13+
Double.isNaN(Double.parseDouble(""));
14+
assert false;
15+
} catch(NumberFormatException e) {
16+
System.out.println(e);
17+
}
18+
19+
Double d = new Double("-0"); // will be -0
20+
Double d1 = Double.valueOf(-0); // will be 0
1321
System.out.println(d);
1422
System.out.println(d1);
1523
System.out.println("" + d);

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
// Google closure compiler cannot handle Clazz.new or Clazz.super
99

10+
// BH 2022.12.03 fix for Double.isInfinite should not be true for NaN
11+
// BH 2022.12.03 fix for Double.parseDouble("") and new Double(NaN) should be NaN, not 0
1012
// BH 2022.09.20 fix for Class.forName not loading static inner classes directly
1113
// BH 2022.09.20 fix for default toString for classes using "." name not "$" name for inner classes
1214
// BH 2022.09.15 fix for new Error() failing; just letting java.lang.Error subclass Throwable
@@ -5353,7 +5355,7 @@ m$(Float, "c$$S", function(v){
53535355
}, 1);
53545356

53555357
m$(Float, "c$$D", function(v){
5356-
v || (v = 0);
5358+
v || v != v || (v == 0) || (v = 0);
53575359
v = (v < minFloat ? -Infinity : v > maxFloat ? Infinity : v);
53585360
this.valueOf=function(){return v;}
53595361
}, 1);
@@ -5444,12 +5446,13 @@ return isNaN(this.valueOf());
54445446

54455447
m$(Float,"isInfinite$F",
54465448
function(num){
5447-
return !Number.isFinite(num);
5449+
return num == num && !Number.isFinite(num);
54485450
}, 1);
54495451

54505452
m$(Float,"isInfinite$",
54515453
function(){
5452-
return !Number.isFinite(this.valueOf());
5454+
var v = this.valueOf();
5455+
return v == v && !Number.isFinite(this.valueOf());
54535456
});
54545457

54555458
m$(Float,"equals$O",
@@ -5462,7 +5465,11 @@ m$(Float, "$box$", function(v) {
54625465
});
54635466

54645467
Clazz._setDeclared("Double", java.lang.Double=Double=function(){
5465-
if (arguments[0] === null || typeof arguments[0] != "object")this.c$(arguments[0]);
5468+
if (typeof arguments[0] == "number") {
5469+
this.c$$D(arguments[0]);
5470+
} else if (arguments[0] === null || typeof arguments[0] != "object") {
5471+
this.c$(arguments[0]);
5472+
}
54665473
});
54675474
decorateAsNumber(Double,"Double", "double", "D");
54685475

@@ -5484,12 +5491,13 @@ return Clazz._floatToString(this.valueOf());
54845491
};
54855492

54865493
m$(Double, "c$$D", function(v){
5487-
v || (v = 0);
5494+
v || v != v || (v == 0) || (v = 0);
54885495
this.valueOf=function(){return v;};
54895496
}, 1);
54905497

54915498
m$(Double,"c$", function(v){
5492-
v || v == null || v != v || (v == 0) || (v = 0);
5499+
// -0 here becomes 0, from Double.valueOf(d)
5500+
v || v == null || v != v || (v = 0);
54935501
if (typeof v != "number")
54945502
v = Double.parseDouble$S(v);
54955503
this.valueOf=function(){return v;}
@@ -5512,9 +5520,12 @@ Double.prototype.isInfinite$ = Float.prototype.isInfinite$;
55125520

55135521
m$(Double,"parseDouble$S",
55145522
function(s){
5515-
if(s==null){
5523+
if(s == null) {
55165524
throw Clazz.new_(NumberFormatException.c$$S, ["null"]);
55175525
}
5526+
if(s.length == 0) {
5527+
throw Clazz.new_(NumberFormatException.c$$S, ["empty String"]);
5528+
}
55185529
if (s.indexOf("NaN") >= 0)
55195530
return NaN;
55205531
var v=Number(s);
@@ -5537,7 +5548,7 @@ return Clazz.new_(Double.c$$S, [v]);
55375548

55385549
m$(Double,"valueOf$D",
55395550
function(v){
5540-
return Clazz.new_(Double.c$$D, [v]);
5551+
return Clazz.new_(Double.c$, [v]);
55415552
}, 1);
55425553

55435554
// Double.prototype.equals =

sources/net.sf.j2s.java.core/srcjs/swingjs2.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14037,14 +14037,16 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
1403714037

1403814038
})(self.J2S, self.jQuery, window, document);
1403914039
// j2sClazz.js
14040-
// NOTE: updates to this file should be copies to j2sjmol.js
14040+
// NOTE: updates to this file should be copied to j2sjmol.js
1404114041

1404214042
// latest author: Bob Hanson, St. Olaf College, hansonr@stolaf.edu
1404314043

1404414044
// NOTES by Bob Hanson
1404514045

1404614046
// Google closure compiler cannot handle Clazz.new or Clazz.super
1404714047

14048+
// BH 2022.12.03 fix for Double.isInfinite should not be true for NaN
14049+
// BH 2022.12.03 fix for Double.parseDouble("") and new Double(NaN) should be NaN, not 0
1404814050
// BH 2022.09.20 fix for Class.forName not loading static inner classes directly
1404914051
// BH 2022.09.20 fix for default toString for classes using "." name not "$" name for inner classes
1405014052
// BH 2022.09.15 fix for new Error() failing; just letting java.lang.Error subclass Throwable
@@ -19391,7 +19393,7 @@ m$(Float, "c$$S", function(v){
1939119393
}, 1);
1939219394

1939319395
m$(Float, "c$$D", function(v){
19394-
v || (v = 0);
19396+
v || v != v || (v == 0) || (v = 0);
1939519397
v = (v < minFloat ? -Infinity : v > maxFloat ? Infinity : v);
1939619398
this.valueOf=function(){return v;}
1939719399
}, 1);
@@ -19482,12 +19484,13 @@ return isNaN(this.valueOf());
1948219484

1948319485
m$(Float,"isInfinite$F",
1948419486
function(num){
19485-
return !Number.isFinite(num);
19487+
return num == num && !Number.isFinite(num);
1948619488
}, 1);
1948719489

1948819490
m$(Float,"isInfinite$",
1948919491
function(){
19490-
return !Number.isFinite(this.valueOf());
19492+
var v = this.valueOf();
19493+
return v == v && !Number.isFinite(this.valueOf());
1949119494
});
1949219495

1949319496
m$(Float,"equals$O",
@@ -19500,7 +19503,11 @@ m$(Float, "$box$", function(v) {
1950019503
});
1950119504

1950219505
Clazz._setDeclared("Double", java.lang.Double=Double=function(){
19503-
if (arguments[0] === null || typeof arguments[0] != "object")this.c$(arguments[0]);
19506+
if (typeof arguments[0] == "number") {
19507+
this.c$$D(arguments[0]);
19508+
} else if (arguments[0] === null || typeof arguments[0] != "object") {
19509+
this.c$(arguments[0]);
19510+
}
1950419511
});
1950519512
decorateAsNumber(Double,"Double", "double", "D");
1950619513

@@ -19522,12 +19529,13 @@ return Clazz._floatToString(this.valueOf());
1952219529
};
1952319530

1952419531
m$(Double, "c$$D", function(v){
19525-
v || (v = 0);
19532+
v || v != v || (v == 0) || (v = 0);
1952619533
this.valueOf=function(){return v;};
1952719534
}, 1);
1952819535

1952919536
m$(Double,"c$", function(v){
19530-
v || v == null || v != v || (v == 0) || (v = 0);
19537+
// -0 here becomes 0, from Double.valueOf(d)
19538+
v || v == null || v != v || (v = 0);
1953119539
if (typeof v != "number")
1953219540
v = Double.parseDouble$S(v);
1953319541
this.valueOf=function(){return v;}
@@ -19550,9 +19558,12 @@ Double.prototype.isInfinite$ = Float.prototype.isInfinite$;
1955019558

1955119559
m$(Double,"parseDouble$S",
1955219560
function(s){
19553-
if(s==null){
19561+
if(s == null) {
1955419562
throw Clazz.new_(NumberFormatException.c$$S, ["null"]);
1955519563
}
19564+
if(s.length == 0) {
19565+
throw Clazz.new_(NumberFormatException.c$$S, ["empty String"]);
19566+
}
1955619567
if (s.indexOf("NaN") >= 0)
1955719568
return NaN;
1955819569
var v=Number(s);
@@ -19575,7 +19586,7 @@ return Clazz.new_(Double.c$$S, [v]);
1957519586

1957619587
m$(Double,"valueOf$D",
1957719588
function(v){
19578-
return Clazz.new_(Double.c$$D, [v]);
19589+
return Clazz.new_(Double.c$, [v]);
1957919590
}, 1);
1958019591

1958119592
// Double.prototype.equals =

0 commit comments

Comments
 (0)