Skip to content

Commit c80b63f

Browse files
author
zhourenjian
committed
Add more supports about numbers
1 parent 41a4321 commit c80b63f

File tree

10 files changed

+551
-4
lines changed

10 files changed

+551
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Boolean.prototype.parseBoolean = function(val){
1212

1313
Boolean.$valueOf = Boolean.parseBoolean = Boolean.prototype.parseBoolean;
1414

15-
Boolean.TRUE=Boolean.prototype.TRUE=new Boolean(true);
16-
Boolean.FALSE=Boolean.prototype.FALSE=new Boolean(false);
17-
15+
Boolean.TRUE = Boolean.prototype.TRUE = new Boolean(true);
16+
Boolean.FALSE = Boolean.prototype.FALSE = new Boolean(false);
17+
Boolean.TYPE = Boolean.prototype.TYPE = Boolean;
1818

1919

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
Clazz.load (["java.lang.Comparable", "$.Number"], "java.lang.Byte", null, function () {
2+
java.lang.Byte = Byte = function () {
3+
Clazz.instantialize (this, arguments);
4+
};
5+
Clazz.decorateAsType (Byte, "Byte", Number, Comparable);
6+
Byte.prototype.valueOf = function () { return 0; };
7+
Byte.toString = Byte.prototype.toString = function () {
8+
if (arguments.length != 0) {
9+
return "" + arguments[0];
10+
}
11+
return "" + this.valueOf ();
12+
};
13+
Clazz.makeConstructor (Byte,
14+
function () {
15+
this.valueOf = function () {
16+
return 0;
17+
};
18+
});
19+
Clazz.makeConstructor (Byte,
20+
function (value) {
21+
var v = Math.round (value) & 0xffffffff;
22+
this.valueOf = function () {
23+
return v;
24+
};
25+
}, "Number");
26+
Clazz.makeConstructor (Byte,
27+
function (s) {
28+
var value = Byte.parseByte (s, 10);
29+
this.valueOf = function () {
30+
return value;
31+
};
32+
}, "String");
33+
Byte.serialVersionUID = Byte.prototype.serialVersionUID = -7183698231559129828;
34+
Byte.MIN_VALUE = Byte.prototype.MIN_VALUE = -128;
35+
Byte.MAX_VALUE = Byte.prototype.MAX_VALUE = 127;
36+
Byte.SIZE = Byte.prototype.SIZE = 8;
37+
Byte.TYPE = Byte.prototype.TYPE = Byte;
38+
39+
Clazz.defineMethod (Byte, "parseByte",
40+
function (s, radix) {
41+
if (s == null) {
42+
throw new NumberFormatException ("null");
43+
}if (radix < 2) {
44+
throw new NumberFormatException ("radix " + radix + " less than Character.MIN_RADIX");
45+
}if (radix > 36) {
46+
throw new NumberFormatException ("radix " + radix + " greater than Character.MAX_RADIX");
47+
}
48+
var integer = parseInt (s, radix);
49+
if(isNaN(integer)){
50+
throw new NumberFormatException ("Not a Number : " + s);
51+
}
52+
return integer;
53+
}, "String, Number");
54+
Byte.parseByte = Byte.prototype.parseByte;
55+
Clazz.defineMethod (Byte, "parseByte",
56+
function (s) {
57+
return Byte.parseByte (s, 10);
58+
}, "String");
59+
60+
Byte.parseByte = Byte.prototype.parseByte;
61+
62+
Clazz.defineMethod (Byte, "$valueOf",
63+
function (s) {
64+
return new Byte(Byte.parseByte (s, 10));
65+
}, "String");
66+
67+
Clazz.defineMethod (Byte, "$valueOf",
68+
function (s) {
69+
return new Byte(s);
70+
}, "Number");
71+
72+
Clazz.defineMethod (Byte, "$valueOf",
73+
function (s, r) {
74+
return new Byte(Byte.parseByte (s, r));
75+
}, "String, Number");
76+
77+
Byte.$valueOf = Byte.prototype.$valueOf;
78+
Clazz.defineMethod (Byte, "equals",
79+
function (s) {
80+
if(s == null || !Clazz.instanceOf(s, Byte) ){
81+
return false;
82+
}
83+
return s.valueOf() == this.valueOf();
84+
}, "Object");
85+
Byte.toHexString = Byte.prototype.toHexString = function (i) {
86+
return i.toString (16);
87+
};
88+
Byte.toOctalString = Byte.prototype.toOctalString = function (i) {
89+
return i.toString (8);
90+
};
91+
Byte.toBinaryString = Byte.prototype.toBinaryString = function (i) {
92+
return i.toString (2);
93+
};
94+
Byte.decode = Clazz.defineMethod (Byte, "decode",
95+
function (nm) {
96+
var radix = 10;
97+
var index = 0;
98+
var negative = false;
99+
var result;
100+
if (nm.startsWith ("-")) {
101+
negative = true;
102+
index++;
103+
}if (nm.startsWith ("0x", index) || nm.startsWith ("0X", index)) {
104+
index += 2;
105+
radix = 16;
106+
} else if (nm.startsWith ("#", index)) {
107+
index++;
108+
radix = 16;
109+
} else if (nm.startsWith ("0", index) && nm.length > 1 + index) {
110+
index++;
111+
radix = 8;
112+
}if (nm.startsWith ("-", index)) throw new NumberFormatException ("Negative sign in wrong position");
113+
try {
114+
result = Byte.$valueOf (nm.substring (index), radix);
115+
result = negative ? new Byte (-result.byteValue ()) : result;
116+
} catch (e) {
117+
if (Clazz.instanceOf (e, NumberFormatException)) {
118+
var constant = negative ? String.instantialize ("-" + nm.substring (index)) : nm.substring (index);
119+
result = Byte.$valueOf (constant, radix);
120+
} else {
121+
throw e;
122+
}
123+
}
124+
return result;
125+
}, "~S");
126+
});
127+

sources/net.sf.j2s.java.core/src/java/lang/Character.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,40 @@
5151
* </p>
5252
*
5353
* @since 1.0
54+
*
55+
* @j2sSuffix java.lang.Character.TYPE=java.lang.Character.prototype.TYPE=java.lang.Character;
5456
*/
5557
public final class Character implements Serializable, Comparable<Character> {
5658
private static final long serialVersionUID = 3786198910865385080L;
5759

60+
/**
61+
* The minimum possible Character value.
62+
*/
63+
public static final char MIN_VALUE = '\u0000';
64+
65+
/**
66+
* The maximum possible Character value.
67+
*/
68+
public static final char MAX_VALUE = '\uffff';
69+
70+
/**
71+
* The minimum possible radix used for conversions between Characters and
72+
* integers.
73+
*/
74+
public static final int MIN_RADIX = 2;
75+
76+
/**
77+
* The maximum possible radix used for conversions between Characters and
78+
* integers.
79+
*/
80+
public static final int MAX_RADIX = 36;
81+
82+
/**
83+
* The <code>char</code> {@link Class} object.
84+
*/
85+
@SuppressWarnings("unchecked")
86+
public static final Class<Character> TYPE = null;
87+
5888
private final char value;
5989

6090
/**
@@ -236,6 +266,64 @@ public static int offsetByCodePoints(char[] chs, int begin, int offset, int end,
236266
return 0;
237267
}
238268

269+
/**
270+
* Convenient method to determine the value of character <code>c</code> in
271+
* the supplied radix. The value of <code>radix</code> must be between
272+
* MIN_RADIX and MAX_RADIX.
273+
*
274+
* @param c
275+
* the character
276+
* @param radix
277+
* the radix
278+
* @return if <code>radix</code> lies between {@link #MIN_RADIX} and
279+
* {@link #MAX_RADIX} then the value of the character in the radix,
280+
* otherwise -1.
281+
*/
282+
public static int digit(char c, int radix) {
283+
if (radix >= MIN_RADIX && radix <= MAX_RADIX) {
284+
if (c < 128) {
285+
// Optimized for ASCII
286+
int result = -1;
287+
if ('0' <= c && c <= '9') {
288+
result = c - '0';
289+
} else if ('a' <= c && c <= 'z') {
290+
result = c - ('a' - 10);
291+
} else if ('A' <= c && c <= 'Z') {
292+
result = c - ('A' - 10);
293+
}
294+
return result < radix ? result : -1;
295+
}
296+
// int result = BinarySearch.binarySearchRange(digitKeys, c);
297+
// if (result >= 0 && c <= digitValues[result * 2]) {
298+
// int value = (char) (c - digitValues[result * 2 + 1]);
299+
// if (value >= radix) {
300+
// return -1;
301+
// }
302+
// return value;
303+
// }
304+
}
305+
return -1;
306+
}
307+
308+
/**
309+
* Convenient method to determine the value of character
310+
* <code>codePoint</code> in the supplied radix. The value of
311+
* <code>radix</code> must be between MIN_RADIX and MAX_RADIX.
312+
*
313+
* @param codePoint
314+
* the character, including supplementary characters
315+
* @param radix
316+
* the radix
317+
* @return if <code>radix</code> lies between {@link #MIN_RADIX} and
318+
* {@link #MAX_RADIX} then the value of the character in the radix,
319+
* otherwise -1.
320+
* @j2sIgnore
321+
*/
322+
public static int digit(int codePoint, int radix) {
323+
//return UCharacter.digit(codePoint, radix);
324+
return -1;
325+
}
326+
239327
/**
240328
* @j2sIgnore
241329
*/

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,11 @@ Clazz.instantialize = function (objThis, args) {
14041404
objThis.construct.apply (objThis, args);
14051405
}
14061406
*/
1407+
if (objThis instanceof Number) {
1408+
objThis.valueOf = function () {
1409+
return this;
1410+
};
1411+
}
14071412
var c = objThis.construct;
14081413
if (c != null) {
14091414
if (objThis.con$truct == null) { // no need to init fields
@@ -1473,6 +1478,9 @@ Clazz.innerFunctions = {
14731478

14741479
getResourceAsStream : function (name) {
14751480
var is = null;
1481+
if (name == null) {
1482+
return is;
1483+
}
14761484
if (java.io.InputStream != null) {
14771485
is = new java.io.InputStream ();
14781486
} else {

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

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,127 @@ Thread.currentThread = Thread.prototype.currentThread = function () {
734734
return this.J2S_THREAD;
735735
};
736736

737+
/* public */
738+
Clazz.intCast = function (n) { // 32bit
739+
var b1 = (n & 0xff000000) >> 24;
740+
var b2 = (n & 0xff0000) >> 16;
741+
var b3 = (n & 0xff00) >> 8;
742+
var b4 = n & 0xff;
743+
if ((b1 & 0x80) != 0) {
744+
return -(((b1 & 0x7f) << 24) + (b2 << 16) + (b3 << 8) + b4 + 1);
745+
} else {
746+
return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
747+
}
748+
};
749+
750+
/* public */
751+
Clazz.shortCast = function (s) { // 16bit
752+
var b1 = (n & 0xff00) >> 8;
753+
var b2 = n & 0xff;
754+
if ((b1 & 0x80) != 0) {
755+
return -(((b1 & 0x7f) << 8) + b2 + 1);
756+
} else {
757+
return (b1 << 8) + b4;
758+
}
759+
};
760+
761+
/* public */
762+
Clazz.byteCast = function (b) { // 8bit
763+
if ((b & 0x80) != 0) {
764+
return -((b & 0x7f) + 1);
765+
} else {
766+
return b & 0xff;
767+
}
768+
};
769+
770+
/* public */
771+
Clazz.charCast = function (c) { // 8bit
772+
return String.fromCharCode (c & 0xff).charAt (0);
773+
};
774+
775+
/**
776+
* Warning: Unsafe conversion!
777+
*/
778+
/* public */
779+
Clazz.floatCast = function (f) { // 32bit
780+
return f;
781+
};
782+
783+
/*
784+
* Try to fix JavaScript's shift operator defects on long type numbers.
785+
*/
786+
787+
Clazz.longMasks = [];
788+
789+
Clazz.longReverseMasks = [];
790+
791+
Clazz.longBits = [];
792+
793+
(function () {
794+
var arr = [1];
795+
for (var i = 1; i < 53; i++) {
796+
arr[i] = arr[i - 1] + arr[i - 1]; // * 2 or << 1
797+
}
798+
Clazz.longBits = arr;
799+
Clazz.longMasks[52] = arr[52];
800+
for (var i = 51; i >= 0; i--) {
801+
Clazz.longMasks[i] = Clazz.longMasks[i + 1] + arr[i];
802+
}
803+
Clazz.longReverseMasks[0] = arr[0];
804+
for (var i = 1; i < 52; i++) {
805+
Clazz.longReverseMasks[i] = Clazz.longReverseMasks[i - 1] + arr[i];
806+
}
807+
}) ();
808+
809+
/* public */
810+
Clazz.longLeftShift = function (l, o) { // 64bit
811+
if (o == 0) return l;
812+
if (o >= 64) return 0;
813+
if (o > 52) {
814+
error ("[Java2Script] Error : JavaScript does not support long shift!");
815+
return l;
816+
}
817+
if ((l & Clazz.longMasks[o - 1]) != 0) {
818+
error ("[Java2Script] Error : Such shift operator results in wrong calculation!");
819+
return l;
820+
}
821+
var high = l & Clazz.longMasks[52 - 32 + o];
822+
if (high != 0) {
823+
return high * Clazz.longBits[o] + (l & Clazz.longReverseMasks[32 - o]) << 0;
824+
} else {
825+
return l << o;
826+
}
827+
};
828+
829+
/* public */
830+
Clazz.intLeftShift = function (n, o) { // 32bit
831+
return (n << o) & 0xffffffff;
832+
};
833+
834+
/* public */
835+
Clazz.longRightShift = function (l, o) { // 64bit
836+
if ((l & Clazz.longMasks[52 - 32]) != 0) {
837+
return Math.round((l & Clazz.longMasks[52 - 32]) / Clazz.longBits[32 - o]) + (l & Clazz.longReverseMasks[o]) >> o;
838+
} else {
839+
return l >> o;
840+
}
841+
};
842+
843+
/* public */
844+
Clazz.intRightShift = function (n, o) { // 32bit
845+
return n >> o; // no needs for this shifting wrapper
846+
};
847+
848+
/* public */
849+
Clazz.long0RightShift = function (l, o) { // 64bit
850+
return l >>> o;
851+
};
852+
853+
/* public */
854+
Clazz.int0RightShift = function (n, o) { // 64bit
855+
return n >>> o; // no needs for this shifting wrapper
856+
};
857+
737858
// Compress the common public API method in shorter name
738859
$_L=Clazz.load;$_W=Clazz.declareAnonymous;$_T=Clazz.declareType;$_J=Clazz.declarePackage;$_C=Clazz.decorateAsClass;$_Z=Clazz.instantialize;$_I=Clazz.declareInterface;$_D=Clazz.isClassDefined;$_H=Clazz.pu$h;$_P=Clazz.p0p;$_B=Clazz.prepareCallback;$_N=Clazz.innerTypeInstance;$_K=Clazz.makeConstructor;$_U=Clazz.superCall;$_R=Clazz.superConstructor;$_M=Clazz.defineMethod;$_V=Clazz.overrideMethod;$_S=Clazz.defineStatics;$_E=Clazz.defineEnumConstant;$_F=Clazz.cloneFinals;$_Y=Clazz.prepareFields;$_A=Clazz.newArray;$_O=Clazz.instanceOf;$_G=Clazz.inheritArgs;$_X=Clazz.checkPrivateMethod;$_Q=Clazz.makeFunction;$_s=Clazz.registerSerializableFields;
739860

0 commit comments

Comments
 (0)