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