Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / lang / Long.js
1 Clazz.load (["java.lang.Comparable", "$.Number"], "java.lang.Long", null, function () {
2 java.lang.Long = Long = function () {
3 Clazz.instantialize (this, arguments);
4 };
5 Clazz.decorateAsType (Long, "Long", Number, Comparable, null, true);
6 Long.prototype.valueOf = function () { return 0; };
7 Long.toString = Long.prototype.toString = function () {
8         if (arguments.length != 0) {
9                 return "" + arguments[0];
10         } else if (this === Long) {
11                 return "class java.lang.Long"; // Long.class.toString
12         }
13         return "" + this.valueOf ();
14 };
15 Clazz.makeConstructor (Long, 
16 function () {
17 this.valueOf = function () {
18         return 0;
19 };
20 });
21 Clazz.makeConstructor (Long, 
22 function (value) {
23 var v = Math.round (value);
24 this.valueOf = function () {
25         return v;
26 };
27 }, "Number");
28 Clazz.makeConstructor (Long, 
29 function (s) {
30 var value = Long.parseLong (s, 10);
31 this.valueOf = function () {
32         return value;
33 };
34 }, "String");
35 Long.serialVersionUID = Long.prototype.serialVersionUID = 4290774380558885855;
36 Long.MIN_VALUE = Long.prototype.MIN_VALUE = -0x8000000000000000;
37 Long.MAX_VALUE = Long.prototype.MAX_VALUE = 0x7fffffffffffffff;
38 Long.TYPE = Long.prototype.TYPE = Long;
39
40 Clazz.defineMethod (Long, "parseLong", 
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 longVal = parseInt (s, radix);
50 if(isNaN(longVal)){
51 throw  new NumberFormatException ("Not a Number : " + s);
52 }
53 return longVal;
54 }, "String, Number");
55
56 Clazz.defineMethod (Long, "parseLong", 
57 function (s) {
58 return Long.parseLong (s, 10);
59 }, "String");
60
61 Long.parseLong = Long.prototype.parseLong;
62
63 Clazz.defineMethod (Long, "$valueOf", 
64 function (s) {
65 return new Long(Long.parseLong (s, 10));
66 }, "String");
67
68 Clazz.defineMethod (Long, "$valueOf", 
69 function (s) {
70 return new Long(s);
71 }, "Number");
72
73 Clazz.defineMethod (Long, "$valueOf", 
74 function (s, r) {
75 return new Long(Long.parseLong (s, r));
76 }, "String, Number");
77
78 Long.$valueOf = Long.prototype.$valueOf;
79 Clazz.defineMethod (Long, "equals", 
80 function (s) {
81 if(s == null || !Clazz.instanceOf(s, Long) ){
82         return false;
83 }
84 return s.valueOf()  == this.valueOf();
85 }, "Object");
86 Long.toHexString = Long.prototype.toHexString = function (i) {
87         return i.toString (16);
88 };
89 Long.toOctalString = Long.prototype.toOctalString = function (i) {
90         return i.toString (8);
91 };
92 Long.toBinaryString = Long.prototype.toBinaryString = function (i) {
93         return i.toString (2);
94 };
95 Long.decode = Clazz.defineMethod (Long, "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 = Long.$valueOf (nm.substring (index), radix);
116 result = negative ?  new Long (-result.longValue ()) : result;
117 } catch (e) {
118 if (Clazz.instanceOf (e, NumberFormatException)) {
119 var constant = negative ?  String.instantialize ("-" + nm.substring (index)) : nm.substring (index);
120 result = Long.$valueOf (constant, radix);
121 } else {
122 throw e;
123 }
124 }
125 return result;
126 }, "~S");
127 });
128