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