Skip to content

Commit ead1904

Browse files
author
zhourenjian@gmail.com
committed
Improve performance on Number/Boolean related object operations
1 parent 7990468 commit ead1904

File tree

9 files changed

+258
-42
lines changed

9 files changed

+258
-42
lines changed

sources/net.sf.j2s.java.core/src/java/lang/Boolean.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Clazz.load (["java.lang.Comparable", "java.io.Serializable"], "java.lang.Boolean", null, function () {
2-
java.lang.Boolean = Boolean;
2+
java.lang.B00lean = Boolean;
3+
java.lang.Boolean = Boolean = function () {
4+
Clazz.instantialize (this, arguments);
5+
};
36
if (Clazz.supportsNativeObject) {
47
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
58
var p = Clazz.extendedObjectMethods[i];
@@ -11,7 +14,7 @@ Clazz.implementOf (Boolean, [java.io.Serializable, java.lang.Comparable]);
1114
Boolean.equals = Clazz.innerFunctions.equals;
1215
Boolean.getName = Clazz.innerFunctions.getName;
1316
Boolean.serialVersionUID = Boolean.prototype.serialVersionUID = -3665804199014368530;
14-
17+
/*
1518
Clazz.makeConstructor (Boolean,
1619
function (value) {
1720
this.valueOf = function () {
@@ -24,6 +27,20 @@ this.valueOf = function () {
2427
return Boolean.toBoolean (s);
2528
};
2629
}, "~S");
30+
// */
31+
Clazz.makeConstructor (Boolean,
32+
function (s) {
33+
var v = false;
34+
if (arguments.length > 0) {
35+
v = s;
36+
if (typeof s == "string") {
37+
v = Boolean.toBoolean (s);
38+
}
39+
}
40+
this.valueOf = function () {
41+
return !(!v);
42+
};
43+
}, "Object");
2744
Boolean.parseBoolean = Clazz.defineMethod (Boolean, "parseBoolean",
2845
function (s) {
2946
return Boolean.toBoolean (s);
@@ -32,6 +49,7 @@ Clazz.defineMethod (Boolean, "booleanValue",
3249
function () {
3350
return this.valueOf ();
3451
});
52+
/*
3553
Boolean.$valueOf = Clazz.defineMethod (Boolean, "$valueOf",
3654
function (b) {
3755
return (b ? Boolean.TRUE : Boolean.FALSE);
@@ -40,7 +58,15 @@ Boolean.$valueOf = Clazz.defineMethod (Boolean, "$valueOf",
4058
function (s) {
4159
return Boolean.toBoolean (s) ? Boolean.TRUE : Boolean.FALSE;
4260
}, "~S");
43-
Boolean.toString = Clazz.defineMethod (Boolean, "toString",
61+
// */
62+
Boolean.$valueOf = Boolean.prototype.$valueOf = function (s) {
63+
if (typeof s == "string") { // String
64+
return Boolean.toBoolean (s) ? Boolean.TRUE : Boolean.FALSE;
65+
} else {
66+
return (s ? Boolean.TRUE : Boolean.FALSE);
67+
}
68+
};
69+
Boolean.toString = Clazz.overrideMethod (Boolean, "toString",
4470
function (b) {
4571
return b ? "true" : "false";
4672
}, "~B");

sources/net.sf.j2s.java.core/src/java/lang/Byte.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Byte.toString = Byte.prototype.toString = function () {
1212
}
1313
return "" + this.valueOf ();
1414
};
15+
/*
1516
Clazz.makeConstructor (Byte,
1617
function () {
1718
this.valueOf = function () {
@@ -32,6 +33,21 @@ this.valueOf = function () {
3233
return value;
3334
};
3435
}, "String");
36+
// */
37+
Clazz.makeConstructor (Byte,
38+
function (s) {
39+
var v = 0;
40+
if (arguments.length > 0) {
41+
if (typeof s == "string") {
42+
v = Byte.parseByte (s, 10);
43+
} else {
44+
v = s;
45+
}
46+
}
47+
this.valueOf = function () {
48+
return v;
49+
};
50+
}, "Object");
3551
Byte.serialVersionUID = Byte.prototype.serialVersionUID = -7183698231559129828;
3652
Byte.MIN_VALUE = Byte.prototype.MIN_VALUE = -128;
3753
Byte.MAX_VALUE = Byte.prototype.MAX_VALUE = 127;
@@ -40,6 +56,9 @@ Byte.TYPE = Byte.prototype.TYPE = Byte;
4056

4157
Clazz.defineMethod (Byte, "parseByte",
4258
function (s, radix) {
59+
if (arguments.length < 2 || radix == null) {
60+
radix = 10;
61+
}
4362
if (s == null) {
4463
throw new NumberFormatException ("null");
4564
}if (radix < 2) {
@@ -53,14 +72,16 @@ throw new NumberFormatException ("Not a Number : " + s);
5372
}
5473
return integer;
5574
}, "String, Number");
75+
/*
5676
Byte.parseByte = Byte.prototype.parseByte;
5777
Clazz.defineMethod (Byte, "parseByte",
5878
function (s) {
5979
return Byte.parseByte (s, 10);
6080
}, "String");
61-
81+
// */
6282
Byte.parseByte = Byte.prototype.parseByte;
63-
83+
84+
/*
6485
Clazz.defineMethod (Byte, "$valueOf",
6586
function (s) {
6687
return new Byte(Byte.parseByte (s, 10));
@@ -76,8 +97,20 @@ function (s, r) {
7697
return new Byte(Byte.parseByte (s, r));
7798
}, "String, Number");
7899
79-
Byte.$valueOf = Byte.prototype.$valueOf;
80-
Clazz.defineMethod (Byte, "equals",
100+
Byte.$valueOf = Byte.prototype.$valueOf;
101+
// */
102+
103+
Byte.$valueOf = Byte.prototype.$valueOf = function (s, r) {
104+
if (arguments.length == 2) { // String, Number
105+
return new Byte(Byte.parseByte (s, r));
106+
} else if (typeof s == "string") { // String
107+
return new Byte(Byte.parseByte (s, 10));
108+
} else { // Number
109+
return new Byte(s);
110+
}
111+
};
112+
113+
Clazz.overrideMethod (Byte, "equals",
81114
function (s) {
82115
if(s == null || !Clazz.instanceOf(s, Byte) ){
83116
return false;

sources/net.sf.j2s.java.core/src/java/lang/Double.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Double.toString = Double.prototype.toString = function () {
1212
}
1313
return "" + this.valueOf ();
1414
};
15+
/*
1516
Clazz.makeConstructor (Double,
1617
function () {
1718
this.valueOf = function () {
@@ -31,7 +32,21 @@ this.valueOf = function () {
3132
return value;
3233
};
3334
}, "String");
34-
35+
// */
36+
Clazz.makeConstructor (Double,
37+
function (s) {
38+
var v = 0;
39+
if (arguments.length > 0) {
40+
if (typeof s == "string") {
41+
v = Double.parseDouble (s);
42+
} else {
43+
v = s;
44+
}
45+
}
46+
this.valueOf = function () {
47+
return v;
48+
};
49+
}, "Object");
3550
Double.serialVersionUID = Double.prototype.serialVersionUID = -9172774392245257468;
3651
Double.MIN_VALUE = Double.prototype.MIN_VALUE = 4.9e-324;
3752
Double.MAX_VALUE = Double.prototype.MAX_VALUE = 1.7976931348623157e+308;
@@ -62,19 +77,30 @@ return doubleVal;
6277
}, "String");
6378
Double.parseDouble = Double.prototype.parseDouble;
6479

80+
/*
6581
Clazz.defineMethod (Double, "$valueOf",
66-
function (s) {
82+
function (s) {
6783
return new Double(this.parseDouble(s));
6884
}, "String");
69-
85+
7086
Clazz.defineMethod (Double, "$valueOf",
7187
function (v) {
7288
return new Double(v);
7389
}, "Number");
7490
75-
Double.$valueOf = Double.prototype.$valueOf;
91+
Double.$valueOf = Double.prototype.$valueOf;
92+
// */
7693

77-
Clazz.defineMethod (Double, "equals",
94+
Double.$valueOf = Double.prototype.$valueOf = function (s) {
95+
if (typeof s == "string") { // String
96+
return new Double(this.parseDouble(s));
97+
} else {
98+
return new Double(s);
99+
}
100+
};
101+
102+
Double.prototype.hashCode = JavaObject.prototype.hashCode;
103+
Clazz.overrideMethod (Double, "equals",
78104
function (s) {
79105
if(s == null || ! Clazz.instanceOf(s, Double) ){
80106
return false;

sources/net.sf.j2s.java.core/src/java/lang/Float.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Float.toString = Float.prototype.toString = function () {
1212
}
1313
return "" + this.valueOf ();
1414
};
15+
/*
1516
Clazz.makeConstructor (Float,
1617
function () {
1718
this.valueOf = function () {
@@ -36,6 +37,21 @@ this.valueOf = function () {
3637
return value;
3738
};
3839
}, "String");
40+
// */
41+
Clazz.makeConstructor (Float,
42+
function (s) {
43+
var v = 0.0;
44+
if (arguments.length > 0) {
45+
if (typeof s == "string") {
46+
v = Float.parseFloat (s);
47+
} else {
48+
v = s;
49+
}
50+
}
51+
this.valueOf = function () {
52+
return v;
53+
};
54+
}, "Object");
3955
Float.serialVersionUID = Float.prototype.serialVersionUID = -2671257302660747028;
4056
Float.MIN_VALUE = Float.prototype.MIN_VALUE = 3.4028235e+38;
4157
Float.MAX_VALUE = Float.prototype.MAX_VALUE = 1.4e-45;
@@ -56,7 +72,8 @@ throw new NumberFormatException ("Not a Number : " + s);
5672
return floatVal;
5773
}, "String");
5874
Float.parseFloat = Float.prototype.parseFloat;
59-
75+
76+
/*
6077
Clazz.defineMethod (Float, "$valueOf",
6178
function (s) {
6279
return new Float(Float.parseFloat (s, 10));
@@ -67,7 +84,17 @@ function (s) {
6784
return new Float(s);
6885
}, "Number");
6986
70-
Float.$valueOf = Float.prototype.$valueOf;
87+
Float.$valueOf = Float.prototype.$valueOf;
88+
// */
89+
90+
Float.$valueOf = Float.prototype.$valueOf = function (s) {
91+
if (typeof s == "string") { // String
92+
return new Float(Float.parseFloat (s, 10));
93+
} else {
94+
return new Float(s);
95+
}
96+
};
97+
7198
Clazz.defineMethod (Float, "isNaN",
7299
function (num) {
73100
return isNaN (num);
@@ -79,7 +106,8 @@ return !isFinite (num);
79106
}, "Number");
80107
Float.isInfinite = Float.prototype.isInfinite;
81108

82-
Clazz.defineMethod (Float, "equals",
109+
Float.prototype.hashCode = JavaObject.prototype.hashCode;
110+
Clazz.overrideMethod (Float, "equals",
83111
function (s) {
84112
if(s == null || ! Clazz.instanceOf(s, Float) ){
85113
return false;

sources/net.sf.j2s.java.core/src/java/lang/Integer.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Integer.toString = Integer.prototype.toString = function () {
1212
}
1313
return "" + this.valueOf ();
1414
};
15+
/*
1516
Clazz.makeConstructor (Integer,
1617
function () {
1718
this.valueOf = function () {
@@ -32,13 +33,31 @@ this.valueOf = function () {
3233
return value;
3334
};
3435
}, "String");
36+
// */
37+
Clazz.makeConstructor (Integer,
38+
function (s) {
39+
var v = 0;
40+
if (arguments.length > 0) {
41+
if (typeof s == "string") {
42+
v = Integer.parseInt (s, 10);
43+
} else {
44+
v = s;
45+
}
46+
}
47+
this.valueOf = function () {
48+
return v;
49+
};
50+
}, "Object");
3551
Integer.serialVersionUID = Integer.prototype.serialVersionUID = 1360826667806852920;
3652
Integer.MIN_VALUE = Integer.prototype.MIN_VALUE = -0x80000000;
3753
Integer.MAX_VALUE = Integer.prototype.MAX_VALUE = 0x7fffffff;
3854
Integer.TYPE = Integer.prototype.TYPE = Integer;
3955

4056
Clazz.defineMethod (Integer, "parseInt",
4157
function (s, radix) {
58+
if (arguments.length < 2 || radix == null) {
59+
radix = 10;
60+
}
4261
if (s == null) {
4362
throw new NumberFormatException ("null");
4463
}if (radix < 2) {
@@ -52,18 +71,20 @@ throw new NumberFormatException ("Not a Number : " + s);
5271
}
5372
return integer;
5473
}, "String, Number");
74+
/*
5575
Integer.parseInt = Integer.prototype.parseInt;
5676
Clazz.defineMethod (Integer, "parseInt",
5777
function (s) {
5878
return Integer.parseInt (s, 10);
5979
}, "String");
60-
80+
// */
6181
Integer.parseInt = Integer.prototype.parseInt;
6282

83+
/*
6384
Clazz.defineMethod (Integer, "$valueOf",
64-
function (s) {
85+
function (s, r) {
6586
return new Integer(Integer.parseInt (s, 10));
66-
}, "String");
87+
}, "String");
6788
6889
Clazz.defineMethod (Integer, "$valueOf",
6990
function (s) {
@@ -75,8 +96,20 @@ function (s, r) {
7596
return new Integer(Integer.parseInt (s, r));
7697
}, "String, Number");
7798
78-
Integer.$valueOf = Integer.prototype.$valueOf;
79-
Clazz.defineMethod (Integer, "equals",
99+
Integer.$valueOf = Integer.prototype.$valueOf;
100+
// */
101+
102+
Integer.$valueOf = Integer.prototype.$valueOf = function (s, r) {
103+
if (arguments.length == 2) { // String, Number
104+
return new Integer(Integer.parseInt (s, r));
105+
} else if (typeof s == "string") { // String
106+
return new Integer(Integer.parseInt (s, 10));
107+
} else { // Number
108+
return new Integer(s);
109+
}
110+
};
111+
112+
Clazz.overrideMethod (Integer, "equals",
80113
function (s) {
81114
if(s == null || ! Clazz.instanceOf(s, Integer) ){
82115
return false;
@@ -85,15 +118,15 @@ return s.valueOf() == this.valueOf();
85118
}, "Object");
86119
Integer.toHexString = Integer.prototype.toHexString = function (d) {
87120
if(d.valueOf)d=d.valueOf();
88-
return d._numberToString(16);
121+
return d.to$tring(16);
89122
};
90123
Integer.toOctalString = Integer.prototype.toOctalString = function (d) {
91124
if(d.valueOf)d=d.valueOf();
92-
return d._numberToString(8);
125+
return d.to$tring(8);
93126
};
94127
Integer.toBinaryString = Integer.prototype.toBinaryString = function (d) {
95128
if(d.valueOf)d=d.valueOf();
96-
return d._numberToString(2);
129+
return d.to$tring(2);
97130
};
98131
Integer.decode = Clazz.defineMethod (Integer, "decode",
99132
function (nm) {

0 commit comments

Comments
 (0)