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