Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / text / DigitList.js
1 Clazz.declarePackage ("java.text");
2 Clazz.load (["java.math.RoundingMode"], "java.text.DigitList", ["java.lang.ArithmeticException", "$.Double", "$.InternalError", "$.Long", "$.StringBuffer"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.decimalAt = 0;
5 this.count = 0;
6 this.digits = null;
7 this.data = null;
8 this.roundingMode = null;
9 this.isNegative = false;
10 this.tempBuffer = null;
11 Clazz.instantialize (this, arguments);
12 }, java.text, "DigitList", null, Cloneable);
13 Clazz.prepareFields (c$, function () {
14 this.digits =  Clazz.newCharArray (19, '\0');
15 this.roundingMode = java.math.RoundingMode.HALF_EVEN;
16 });
17 Clazz.defineMethod (c$, "isZero", 
18 function () {
19 for (var i = 0; i < this.count; ++i) {
20 if (this.digits[i] != '0') {
21 return false;
22 }}
23 return true;
24 });
25 Clazz.defineMethod (c$, "setRoundingMode", 
26 function (r) {
27 this.roundingMode = r;
28 }, "java.math.RoundingMode");
29 Clazz.defineMethod (c$, "clear", 
30 function () {
31 this.decimalAt = 0;
32 this.count = 0;
33 });
34 Clazz.defineMethod (c$, "append", 
35 function (digit) {
36 if (this.count == this.digits.length) {
37 var data =  Clazz.newCharArray (this.count + 100, '\0');
38 System.arraycopy (this.digits, 0, data, 0, this.count);
39 this.digits = data;
40 }this.digits[this.count++] = digit;
41 }, "~S");
42 Clazz.defineMethod (c$, "getDouble", 
43 function () {
44 if (this.count == 0) {
45 return 0.0;
46 }var temp = this.getStringBuffer ();
47 temp.append ('.');
48 temp.append (this.digits, 0, this.count);
49 temp.append ('E');
50 temp.append ("" + this.decimalAt);
51 return Double.parseDouble (temp.toString ());
52 });
53 Clazz.defineMethod (c$, "getLong", 
54 function () {
55 if (this.count == 0) {
56 return 0;
57 }if (this.isLongMIN_VALUE ()) {
58 return -9223372036854775808;
59 }var temp = this.getStringBuffer ();
60 temp.append (this.digits, 0, this.count);
61 for (var i = this.count; i < this.decimalAt; ++i) {
62 temp.append ('0');
63 }
64 return Long.parseLong (temp.toString ());
65 });
66 Clazz.defineMethod (c$, "fitsIntoLong", 
67 function (isPositive, ignoreNegativeZero) {
68 while (this.count > 0 && this.digits[this.count - 1] == '0') {
69 --this.count;
70 }
71 if (this.count == 0) {
72 return isPositive || ignoreNegativeZero;
73 }if (this.decimalAt < this.count || this.decimalAt > 19) {
74 return false;
75 }if (this.decimalAt < 19) return true;
76 for (var i = 0; i < this.count; ++i) {
77 var dig = this.digits[i];
78 var max = java.text.DigitList.LONG_MIN_REP[i];
79 if (dig > max) return false;
80 if (dig < max) return true;
81 }
82 if (this.count < this.decimalAt) return true;
83 return !isPositive;
84 }, "~B,~B");
85 Clazz.defineMethod (c$, "setDouble", 
86 function (isNegative, source, maximumFractionDigits) {
87 this.set (isNegative, source, maximumFractionDigits, true);
88 }, "~B,~N,~N");
89 Clazz.defineMethod (c$, "set", 
90 function (isNegative, source, maximumDigits, fixedPoint) {
91 this.set (isNegative, Double.toString (source), maximumDigits, fixedPoint);
92 }, "~B,~N,~N,~B");
93 Clazz.defineMethod (c$, "set", 
94 function (isNegative, s, maximumDigits, fixedPoint) {
95 this.isNegative = isNegative;
96 var len = s.length;
97 var source = this.getDataChars (len);
98 s.getChars (0, len, source, 0);
99 this.decimalAt = -1;
100 this.count = 0;
101 var exponent = 0;
102 var leadingZerosAfterDecimal = 0;
103 var nonZeroDigitSeen = false;
104 for (var i = 0; i < len; ) {
105 var c = source[i++];
106 if (c == '.') {
107 this.decimalAt = this.count;
108 } else if (c == 'e' || c == 'E') {
109 exponent = java.text.DigitList.parseInt (source, i, len);
110 break;
111 } else {
112 if (!nonZeroDigitSeen) {
113 nonZeroDigitSeen = (c != '0');
114 if (!nonZeroDigitSeen && this.decimalAt != -1) ++leadingZerosAfterDecimal;
115 }if (nonZeroDigitSeen) {
116 this.digits[this.count++] = c;
117 }}}
118 if (this.decimalAt == -1) {
119 this.decimalAt = this.count;
120 }if (nonZeroDigitSeen) {
121 this.decimalAt += exponent - leadingZerosAfterDecimal;
122 }if (fixedPoint) {
123 if (-this.decimalAt > maximumDigits) {
124 this.count = 0;
125 return;
126 } else if (-this.decimalAt == maximumDigits) {
127 if (this.shouldRoundUp (0)) {
128 this.count = 1;
129 ++this.decimalAt;
130 this.digits[0] = '1';
131 } else {
132 this.count = 0;
133 }return;
134 }}while (this.count > 1 && this.digits[this.count - 1] == '0') {
135 --this.count;
136 }
137 this.round (fixedPoint ? (maximumDigits + this.decimalAt) : maximumDigits);
138 }, "~B,~S,~N,~B");
139 Clazz.defineMethod (c$, "round", 
140  function (maximumDigits) {
141 if (maximumDigits >= 0 && maximumDigits < this.count) {
142 if (this.shouldRoundUp (maximumDigits)) {
143 for (; ; ) {
144 --maximumDigits;
145 if (maximumDigits < 0) {
146 this.digits[0] = '1';
147 ++this.decimalAt;
148 maximumDigits = 0;
149 break;
150 }this.digits[maximumDigits] = String.fromCharCode (this.digits[maximumDigits].charCodeAt (0) + 1);
151 if (this.digits[maximumDigits] <= '9') break;
152 }
153 ++maximumDigits;
154 }this.count = maximumDigits;
155 while (this.count > 1 && this.digits[this.count - 1] == '0') {
156 --this.count;
157 }
158 }}, "~N");
159 Clazz.defineMethod (c$, "shouldRoundUp", 
160  function (maximumDigits) {
161 if (maximumDigits < this.count) {
162 switch (this.roundingMode) {
163 case java.math.RoundingMode.UP:
164 for (var i = maximumDigits; i < this.count; ++i) {
165 if (this.digits[i] != '0') {
166 return true;
167 }}
168 break;
169 case java.math.RoundingMode.DOWN:
170 break;
171 case java.math.RoundingMode.CEILING:
172 for (var i = maximumDigits; i < this.count; ++i) {
173 if (this.digits[i] != '0') {
174 return !this.isNegative;
175 }}
176 break;
177 case java.math.RoundingMode.FLOOR:
178 for (var i = maximumDigits; i < this.count; ++i) {
179 if (this.digits[i] != '0') {
180 return this.isNegative;
181 }}
182 break;
183 case java.math.RoundingMode.HALF_UP:
184 if (this.digits[maximumDigits] >= '5') {
185 return true;
186 }break;
187 case java.math.RoundingMode.HALF_DOWN:
188 if (this.digits[maximumDigits] > '5') {
189 return true;
190 } else if (this.digits[maximumDigits] == '5') {
191 for (var i = maximumDigits + 1; i < this.count; ++i) {
192 if (this.digits[i] != '0') {
193 return true;
194 }}
195 }break;
196 case java.math.RoundingMode.HALF_EVEN:
197 if (this.digits[maximumDigits] > '5') {
198 return true;
199 } else if (this.digits[maximumDigits] == '5') {
200 for (var i = maximumDigits + 1; i < this.count; ++i) {
201 if (this.digits[i] != '0') {
202 return true;
203 }}
204 return maximumDigits > 0 && ((this.digits[maximumDigits - 1]).charCodeAt (0) % 2 != 0);
205 }break;
206 case java.math.RoundingMode.UNNECESSARY:
207 for (var i = maximumDigits; i < this.count; ++i) {
208 if (this.digits[i] != '0') {
209 throw  new ArithmeticException ("Rounding needed with the rounding mode being set to RoundingMode.UNNECESSARY");
210 }}
211 break;
212 default:
213 }
214 }return false;
215 }, "~N");
216 Clazz.defineMethod (c$, "setExp", 
217 function (isNegative, source) {
218 this.setLong (isNegative, source, 0);
219 }, "~B,~N");
220 Clazz.defineMethod (c$, "setLong", 
221 function (isNegative, source, maximumDigits) {
222 this.isNegative = isNegative;
223 if (source <= 0) {
224 if (source == -9223372036854775808) {
225 this.decimalAt = this.count = 19;
226 System.arraycopy (java.text.DigitList.LONG_MIN_REP, 0, this.digits, 0, this.count);
227 } else {
228 this.decimalAt = this.count = 0;
229 }} else {
230 var left = 19;
231 var right;
232 while (source >= 1) {
233 this.digits[--left] = String.fromCharCode (48 + (source % 10));
234 source = Clazz.doubleToInt (source / 10);
235 }
236 this.decimalAt = 19 - left;
237 for (right = 18; this.digits[right] == '0'; --right) ;
238 this.count = right - left + 1;
239 System.arraycopy (this.digits, left, this.digits, 0, this.count);
240 }if (maximumDigits > 0) this.round (maximumDigits);
241 }, "~B,~N,~N");
242 Clazz.overrideMethod (c$, "equals", 
243 function (obj) {
244 if (this === obj) return true;
245 if (!(Clazz.instanceOf (obj, java.text.DigitList))) return false;
246 var other = obj;
247 if (this.count != other.count || this.decimalAt != other.decimalAt) return false;
248 for (var i = 0; i < this.count; i++) if (this.digits[i] != other.digits[i]) return false;
249
250 return true;
251 }, "~O");
252 Clazz.overrideMethod (c$, "hashCode", 
253 function () {
254 var hashcode = this.decimalAt;
255 for (var i = 0; i < this.count; i++) {
256 hashcode = hashcode * 37 + (this.digits[i]).charCodeAt (0);
257 }
258 return hashcode;
259 });
260 Clazz.defineMethod (c$, "clone", 
261 function () {
262 try {
263 var other = Clazz.superCall (this, java.text.DigitList, "clone", []);
264 var newDigits =  Clazz.newCharArray (this.digits.length, '\0');
265 System.arraycopy (this.digits, 0, newDigits, 0, this.digits.length);
266 other.digits = newDigits;
267 other.tempBuffer = null;
268 return other;
269 } catch (e) {
270 if (Clazz.exceptionOf (e, CloneNotSupportedException)) {
271 throw  new InternalError ();
272 } else {
273 throw e;
274 }
275 }
276 });
277 Clazz.defineMethod (c$, "isLongMIN_VALUE", 
278  function () {
279 if (this.decimalAt != this.count || this.count != 19) {
280 return false;
281 }for (var i = 0; i < this.count; ++i) {
282 if (this.digits[i] != java.text.DigitList.LONG_MIN_REP[i]) return false;
283 }
284 return true;
285 });
286 c$.parseInt = Clazz.defineMethod (c$, "parseInt", 
287  function (str, offset, strLen) {
288 var c;
289 var positive = true;
290 if ((c = str[offset]) == '-') {
291 positive = false;
292 offset++;
293 } else if (c == '+') {
294 offset++;
295 }var value = 0;
296 while (offset < strLen) {
297 c = str[offset++];
298 if (c >= '0' && c <= '9') {
299 value = value * 10 + (c.charCodeAt (0) - 48);
300 } else {
301 break;
302 }}
303 return positive ? value : -value;
304 }, "~A,~N,~N");
305 Clazz.overrideMethod (c$, "toString", 
306 function () {
307 if (this.isZero ()) {
308 return "0";
309 }var buf = this.getStringBuffer ();
310 buf.append ("0.");
311 buf.append (this.digits, 0, this.count);
312 buf.append ("x10^");
313 buf.append ("" + this.decimalAt);
314 return buf.toString ();
315 });
316 Clazz.defineMethod (c$, "getStringBuffer", 
317  function () {
318 if (this.tempBuffer == null) {
319 this.tempBuffer =  new StringBuffer (19);
320 } else {
321 this.tempBuffer.setLength (0);
322 }return this.tempBuffer;
323 });
324 Clazz.defineMethod (c$, "getDataChars", 
325  function (length) {
326 if (this.data == null || this.data.length < length) {
327 this.data =  Clazz.newCharArray (length, '\0');
328 }return this.data;
329 }, "~N");
330 Clazz.defineStatics (c$,
331 "MAX_COUNT", 19);
332 c$.LONG_MIN_REP = c$.prototype.LONG_MIN_REP = "9223372036854775808".toCharArray ();
333 });