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 () {
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;
17 Clazz.defineMethod (c$, "isZero",
19 for (var i = 0; i < this.count; ++i) {
20 if (this.digits[i] != '0') {
25 Clazz.defineMethod (c$, "setRoundingMode",
27 this.roundingMode = r;
28 }, "java.math.RoundingMode");
29 Clazz.defineMethod (c$, "clear",
34 Clazz.defineMethod (c$, "append",
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);
40 }this.digits[this.count++] = digit;
42 Clazz.defineMethod (c$, "getDouble",
44 if (this.count == 0) {
46 }var temp = this.getStringBuffer ();
48 temp.append (this.digits, 0, this.count);
50 temp.append ("" + this.decimalAt);
51 return Double.parseDouble (temp.toString ());
53 Clazz.defineMethod (c$, "getLong",
55 if (this.count == 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) {
64 return Long.parseLong (temp.toString ());
66 Clazz.defineMethod (c$, "fitsIntoLong",
67 function (isPositive, ignoreNegativeZero) {
68 while (this.count > 0 && this.digits[this.count - 1] == '0') {
71 if (this.count == 0) {
72 return isPositive || ignoreNegativeZero;
73 }if (this.decimalAt < this.count || this.decimalAt > 19) {
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;
82 if (this.count < this.decimalAt) return true;
85 Clazz.defineMethod (c$, "setDouble",
86 function (isNegative, source, maximumFractionDigits) {
87 this.set (isNegative, source, maximumFractionDigits, true);
89 Clazz.defineMethod (c$, "set",
90 function (isNegative, source, maximumDigits, fixedPoint) {
91 this.set (isNegative, Double.toString (source), maximumDigits, fixedPoint);
93 Clazz.defineMethod (c$, "set",
94 function (isNegative, s, maximumDigits, fixedPoint) {
95 this.isNegative = isNegative;
97 var source = this.getDataChars (len);
98 s.getChars (0, len, source, 0);
102 var leadingZerosAfterDecimal = 0;
103 var nonZeroDigitSeen = false;
104 for (var i = 0; i < len; ) {
107 this.decimalAt = this.count;
108 } else if (c == 'e' || c == 'E') {
109 exponent = java.text.DigitList.parseInt (source, i, len);
112 if (!nonZeroDigitSeen) {
113 nonZeroDigitSeen = (c != '0');
114 if (!nonZeroDigitSeen && this.decimalAt != -1) ++leadingZerosAfterDecimal;
115 }if (nonZeroDigitSeen) {
116 this.digits[this.count++] = c;
118 if (this.decimalAt == -1) {
119 this.decimalAt = this.count;
120 }if (nonZeroDigitSeen) {
121 this.decimalAt += exponent - leadingZerosAfterDecimal;
123 if (-this.decimalAt > maximumDigits) {
126 } else if (-this.decimalAt == maximumDigits) {
127 if (this.shouldRoundUp (0)) {
130 this.digits[0] = '1';
134 }}while (this.count > 1 && this.digits[this.count - 1] == '0') {
137 this.round (fixedPoint ? (maximumDigits + this.decimalAt) : maximumDigits);
139 Clazz.defineMethod (c$, "round",
140 function (maximumDigits) {
141 if (maximumDigits >= 0 && maximumDigits < this.count) {
142 if (this.shouldRoundUp (maximumDigits)) {
145 if (maximumDigits < 0) {
146 this.digits[0] = '1';
150 }this.digits[maximumDigits] = String.fromCharCode (this.digits[maximumDigits].charCodeAt (0) + 1);
151 if (this.digits[maximumDigits] <= '9') break;
154 }this.count = maximumDigits;
155 while (this.count > 1 && this.digits[this.count - 1] == '0') {
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') {
169 case java.math.RoundingMode.DOWN:
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;
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;
183 case java.math.RoundingMode.HALF_UP:
184 if (this.digits[maximumDigits] >= '5') {
187 case java.math.RoundingMode.HALF_DOWN:
188 if (this.digits[maximumDigits] > '5') {
190 } else if (this.digits[maximumDigits] == '5') {
191 for (var i = maximumDigits + 1; i < this.count; ++i) {
192 if (this.digits[i] != '0') {
196 case java.math.RoundingMode.HALF_EVEN:
197 if (this.digits[maximumDigits] > '5') {
199 } else if (this.digits[maximumDigits] == '5') {
200 for (var i = maximumDigits + 1; i < this.count; ++i) {
201 if (this.digits[i] != '0') {
204 return maximumDigits > 0 && ((this.digits[maximumDigits - 1]).charCodeAt (0) % 2 != 0);
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");
216 Clazz.defineMethod (c$, "setExp",
217 function (isNegative, source) {
218 this.setLong (isNegative, source, 0);
220 Clazz.defineMethod (c$, "setLong",
221 function (isNegative, source, maximumDigits) {
222 this.isNegative = isNegative;
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);
228 this.decimalAt = this.count = 0;
232 while (source >= 1) {
233 this.digits[--left] = String.fromCharCode (48 + (source % 10));
234 source = Clazz.doubleToInt (source / 10);
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);
242 Clazz.overrideMethod (c$, "equals",
244 if (this === obj) return true;
245 if (!(Clazz.instanceOf (obj, java.text.DigitList))) return false;
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;
252 Clazz.overrideMethod (c$, "hashCode",
254 var hashcode = this.decimalAt;
255 for (var i = 0; i < this.count; i++) {
256 hashcode = hashcode * 37 + (this.digits[i]).charCodeAt (0);
260 Clazz.defineMethod (c$, "clone",
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;
270 if (Clazz.exceptionOf (e, CloneNotSupportedException)) {
271 throw new InternalError ();
277 Clazz.defineMethod (c$, "isLongMIN_VALUE",
279 if (this.decimalAt != this.count || this.count != 19) {
281 }for (var i = 0; i < this.count; ++i) {
282 if (this.digits[i] != java.text.DigitList.LONG_MIN_REP[i]) return false;
286 c$.parseInt = Clazz.defineMethod (c$, "parseInt",
287 function (str, offset, strLen) {
290 if ((c = str[offset]) == '-') {
293 } else if (c == '+') {
296 while (offset < strLen) {
298 if (c >= '0' && c <= '9') {
299 value = value * 10 + (c.charCodeAt (0) - 48);
303 return positive ? value : -value;
305 Clazz.overrideMethod (c$, "toString",
307 if (this.isZero ()) {
309 }var buf = this.getStringBuffer ();
311 buf.append (this.digits, 0, this.count);
313 buf.append ("" + this.decimalAt);
314 return buf.toString ();
316 Clazz.defineMethod (c$, "getStringBuffer",
318 if (this.tempBuffer == null) {
319 this.tempBuffer = new StringBuffer (19);
321 this.tempBuffer.setLength (0);
322 }return this.tempBuffer;
324 Clazz.defineMethod (c$, "getDataChars",
326 if (this.data == null || this.data.length < length) {
327 this.data = Clazz.newCharArray (length, '\0');
330 Clazz.defineStatics (c$,
332 c$.LONG_MIN_REP = c$.prototype.LONG_MIN_REP = "9223372036854775808".toCharArray ();