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