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