-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigitList.java
More file actions
715 lines (652 loc) · 24.4 KB
/
DigitList.java
File metadata and controls
715 lines (652 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
/*
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
*
* The original version of this source code and documentation is copyrighted
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
* materials are provided under terms of a License Agreement between Taligent
* and Sun. This technology is protected by multiple US and International
* patents. This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
*/
package java.text;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
/**
* Digit List. Private to DecimalFormat.
* Handles the transcoding
* between numeric values and strings of characters. Only handles
* non-negative numbers. The division of labor between DigitList and
* DecimalFormat is that DigitList handles the radix 10 representation
* issues; DecimalFormat handles the locale-specific issues such as
* positive/negative, grouping, decimal point, currency, and so on.
*
* A DigitList is really a representation of a floating point value.
* It may be an integer value; we assume that a double has sufficient
* precision to represent all digits of a long.
*
* The DigitList representation consists of a string of characters,
* which are the digits radix 10, from '0' to '9'. It also has a radix
* 10 exponent associated with it. The value represented by a DigitList
* object can be computed by mulitplying the fraction f, where 0 <= f < 1,
* derived by placing all the digits of the list to the right of the
* decimal point, by 10^exponent.
*
* @see Locale
* @see Format
* @see NumberFormat
* @see DecimalFormat
* @see ChoiceFormat
* @see MessageFormat
* @author Mark Davis, Alan Liu
*/
final class DigitList implements Cloneable {
/**
* The maximum number of significant digits in an IEEE 754 double, that
* is, in a Java double. This must not be increased, or garbage digits
* will be generated, and should not be decreased, or accuracy will be lost.
*/
public static final int MAX_COUNT = 19; // == Long.toString(Long.MAX_VALUE).length()
/**
* These data members are intentionally public and can be set directly.
*
* The value represented is given by placing the decimal point before
* digits[decimalAt]. If decimalAt is < 0, then leading zeros between
* the decimal point and the first nonzero digit are implied. If decimalAt
* is > count, then trailing zeros between the digits[count-1] and the
* decimal point are implied.
*
* Equivalently, the represented value is given by f * 10^decimalAt. Here
* f is a value 0.1 <= f < 1 arrived at by placing the digits in Digits to
* the right of the decimal.
*
* DigitList is normalized, so if it is non-zero, figits[0] is non-zero. We
* don't allow denormalized numbers because our exponent is effectively of
* unlimited magnitude. The count value contains the number of significant
* digits present in digits[].
*
* Zero is represented by any DigitList with count == 0 or with each digits[i]
* for all i <= count == '0'.
*/
public int decimalAt = 0;
public int count = 0;
public char[] digits = new char[MAX_COUNT];
private char[] data;
private RoundingMode roundingMode = RoundingMode.HALF_EVEN;
private boolean isNegative = false;
/**
* Return true if the represented number is zero.
*/
boolean isZero() {
for (int i=0; i < count; ++i) {
if (digits[i] != '0') {
return false;
}
}
return true;
}
/**
* Set the rounding mode
*/
void setRoundingMode(RoundingMode r) {
roundingMode = r;
}
/**
* Clears out the digits.
* Use before appending them.
* Typically, you set a series of digits with append, then at the point
* you hit the decimal point, you set myDigitList.decimalAt = myDigitList.count;
* then go on appending digits.
*/
public void clear () {
decimalAt = 0;
count = 0;
}
/**
* Appends a digit to the list, extending the list when necessary.
*/
public void append(char digit) {
if (count == digits.length) {
char[] data = new char[count + 100];
System.arraycopy(digits, 0, data, 0, count);
digits = data;
}
digits[count++] = digit;
}
/**
* Utility routine to get the value of the digit list
* If (count == 0) this throws a NumberFormatException, which
* mimics Long.parseLong().
*/
public final double getDouble() {
if (count == 0) {
return 0.0;
}
StringBuffer temp = getStringBuffer();
temp.append('.');
temp.append(digits, 0, count);
temp.append('E');
temp.append(decimalAt);
return Double.parseDouble(temp.toString());
}
/**
* Utility routine to get the value of the digit list.
* If (count == 0) this returns 0, unlike Long.parseLong().
*/
public final long getLong() {
// for now, simple implementation; later, do proper IEEE native stuff
if (count == 0) {
return 0;
}
// We have to check for this, because this is the one NEGATIVE value
// we represent. If we tried to just pass the digits off to parseLong,
// we'd get a parse failure.
if (isLongMIN_VALUE()) {
return Long.MIN_VALUE;
}
StringBuffer temp = getStringBuffer();
temp.append(digits, 0, count);
for (int i = count; i < decimalAt; ++i) {
temp.append('0');
}
return Long.parseLong(temp.toString());
}
public final BigDecimal getBigDecimal() {
if (count == 0) {
if (decimalAt == 0) {
return BigDecimal.ZERO;
} else {
return new BigDecimal("0E" + decimalAt);
}
}
if (decimalAt == count) {
return new BigDecimal(digits, 0, count);
} else {
return new BigDecimal(digits, 0, count).scaleByPowerOfTen(decimalAt - count);
}
}
/**
* Return true if the number represented by this object can fit into
* a long.
* @param isPositive true if this number should be regarded as positive
* @param ignoreNegativeZero true if -0 should be regarded as identical to
* +0; otherwise they are considered distinct
* @return true if this number fits into a Java long
*/
boolean fitsIntoLong(boolean isPositive, boolean ignoreNegativeZero) {
// Figure out if the result will fit in a long. We have to
// first look for nonzero digits after the decimal point;
// then check the size. If the digit count is 18 or less, then
// the value can definitely be represented as a long. If it is 19
// then it may be too large.
// Trim trailing zeros. This does not change the represented value.
while (count > 0 && digits[count - 1] == '0') {
--count;
}
if (count == 0) {
// Positive zero fits into a long, but negative zero can only
// be represented as a double. - bug 4162852
return isPositive || ignoreNegativeZero;
}
if (decimalAt < count || decimalAt > MAX_COUNT) {
return false;
}
if (decimalAt < MAX_COUNT) return true;
// At this point we have decimalAt == count, and count == MAX_COUNT.
// The number will overflow if it is larger than 9223372036854775807
// or smaller than -9223372036854775808.
for (int i=0; i<count; ++i) {
char dig = digits[i], max = LONG_MIN_REP[i];
if (dig > max) return false;
if (dig < max) return true;
}
// At this point the first count digits match. If decimalAt is less
// than count, then the remaining digits are zero, and we return true.
if (count < decimalAt) return true;
// Now we have a representation of Long.MIN_VALUE, without the leading
// negative sign. If this represents a positive value, then it does
// not fit; otherwise it fits.
return !isPositive;
}
/**
* Set the digit list to a representation of the given double value.
* This method supports fixed-point notation.
* @param isNegative Boolean value indicating whether the number is negative.
* @param source Value to be converted; must not be Inf, -Inf, Nan,
* or a value <= 0.
* @param maximumFractionDigits The most fractional digits which should
* be converted.
*/
public final void set(boolean isNegative, double source, int maximumFractionDigits) {
set(isNegative, source, maximumFractionDigits, true);
}
/**
* Set the digit list to a representation of the given double value.
* This method supports both fixed-point and exponential notation.
* @param isNegative Boolean value indicating whether the number is negative.
* @param source Value to be converted; must not be Inf, -Inf, Nan,
* or a value <= 0.
* @param maximumDigits The most fractional or total digits which should
* be converted.
* @param fixedPoint If true, then maximumDigits is the maximum
* fractional digits to be converted. If false, total digits.
*/
final void set(boolean isNegative, double source, int maximumDigits, boolean fixedPoint) {
set(isNegative, Double.toString(source), maximumDigits, fixedPoint);
}
/**
* Generate a representation of the form DDDDD, DDDDD.DDDDD, or
* DDDDDE+/-DDDDD.
*/
final void set(boolean isNegative, String s, int maximumDigits, boolean fixedPoint) {
this.isNegative = isNegative;
int len = s.length();
char[] source = getDataChars(len);
s.getChars(0, len, source, 0);
decimalAt = -1;
count = 0;
int exponent = 0;
// Number of zeros between decimal point and first non-zero digit after
// decimal point, for numbers < 1.
int leadingZerosAfterDecimal = 0;
boolean nonZeroDigitSeen = false;
for (int i = 0; i < len; ) {
char c = source[i++];
if (c == '.') {
decimalAt = count;
} else if (c == 'e' || c == 'E') {
exponent = parseInt(source, i, len);
break;
} else {
if (!nonZeroDigitSeen) {
nonZeroDigitSeen = (c != '0');
if (!nonZeroDigitSeen && decimalAt != -1)
++leadingZerosAfterDecimal;
}
if (nonZeroDigitSeen) {
digits[count++] = c;
}
}
}
if (decimalAt == -1) {
decimalAt = count;
}
if (nonZeroDigitSeen) {
decimalAt += exponent - leadingZerosAfterDecimal;
}
if (fixedPoint) {
// The negative of the exponent represents the number of leading
// zeros between the decimal and the first non-zero digit, for
// a value < 0.1 (e.g., for 0.00123, -decimalAt == 2). If this
// is more than the maximum fraction digits, then we have an underflow
// for the printed representation.
if (-decimalAt > maximumDigits) {
// Handle an underflow to zero when we round something like
// 0.0009 to 2 fractional digits.
count = 0;
return;
} else if (-decimalAt == maximumDigits) {
// If we round 0.0009 to 3 fractional digits, then we have to
// create a new one digit in the least significant location.
if (shouldRoundUp(0)) {
count = 1;
++decimalAt;
digits[0] = '1';
} else {
count = 0;
}
return;
}
// else fall through
}
// Eliminate trailing zeros.
while (count > 1 && digits[count - 1] == '0') {
--count;
}
// Eliminate digits beyond maximum digits to be displayed.
// Round up if appropriate.
round(fixedPoint ? (maximumDigits + decimalAt) : maximumDigits);
}
/**
* Round the representation to the given number of digits.
* @param maximumDigits The maximum number of digits to be shown.
* Upon return, count will be less than or equal to maximumDigits.
*/
private final void round(int maximumDigits) {
// Eliminate digits beyond maximum digits to be displayed.
// Round up if appropriate.
if (maximumDigits >= 0 && maximumDigits < count) {
if (shouldRoundUp(maximumDigits)) {
// Rounding up involved incrementing digits from LSD to MSD.
// In most cases this is simple, but in a worst case situation
// (9999..99) we have to adjust the decimalAt value.
for (;;) {
--maximumDigits;
if (maximumDigits < 0) {
// We have all 9's, so we increment to a single digit
// of one and adjust the exponent.
digits[0] = '1';
++decimalAt;
maximumDigits = 0; // Adjust the count
break;
}
++digits[maximumDigits];
if (digits[maximumDigits] <= '9') break;
// digits[maximumDigits] = '0'; // Unnecessary since we'll truncate this
}
++maximumDigits; // Increment for use as count
}
count = maximumDigits;
// Eliminate trailing zeros.
while (count > 1 && digits[count-1] == '0') {
--count;
}
}
}
/**
* Return true if truncating the representation to the given number
* of digits will result in an increment to the last digit. This
* method implements the rounding modes defined in the
* java.math.RoundingMode class.
* [bnf]
* @param maximumDigits the number of digits to keep, from 0 to
* <code>count-1</code>. If 0, then all digits are rounded away, and
* this method returns true if a one should be generated (e.g., formatting
* 0.09 with "#.#").
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @return true if digit <code>maximumDigits-1</code> should be
* incremented
*/
private boolean shouldRoundUp(int maximumDigits) {
if (maximumDigits < count) {
switch(roundingMode) {
case UP:
for (int i=maximumDigits; i<count; ++i) {
if (digits[i] != '0') {
return true;
}
}
break;
case DOWN:
break;
case CEILING:
for (int i=maximumDigits; i<count; ++i) {
if (digits[i] != '0') {
return !isNegative;
}
}
break;
case FLOOR:
for (int i=maximumDigits; i<count; ++i) {
if (digits[i] != '0') {
return isNegative;
}
}
break;
case HALF_UP:
if (digits[maximumDigits] >= '5') {
return true;
}
break;
case HALF_DOWN:
if (digits[maximumDigits] > '5') {
return true;
} else if (digits[maximumDigits] == '5' ) {
for (int i=maximumDigits+1; i<count; ++i) {
if (digits[i] != '0') {
return true;
}
}
}
break;
case HALF_EVEN:
// Implement IEEE half-even rounding
if (digits[maximumDigits] > '5') {
return true;
} else if (digits[maximumDigits] == '5' ) {
for (int i=maximumDigits+1; i<count; ++i) {
if (digits[i] != '0') {
return true;
}
}
return maximumDigits > 0 && (digits[maximumDigits-1] % 2 != 0);
}
break;
case UNNECESSARY:
for (int i=maximumDigits; i<count; ++i) {
if (digits[i] != '0') {
throw new ArithmeticException(
"Rounding needed with the rounding mode being set to RoundingMode.UNNECESSARY");
}
}
break;
default:
assert false;
}
}
return false;
}
/**
* Utility routine to set the value of the digit list from a long
*/
public final void set(boolean isNegative, long source) {
set(isNegative, source, 0);
}
/**
* Set the digit list to a representation of the given long value.
* @param isNegative Boolean value indicating whether the number is negative.
* @param source Value to be converted; must be >= 0 or ==
* Long.MIN_VALUE.
* @param maximumDigits The most digits which should be converted.
* If maximumDigits is lower than the number of significant digits
* in source, the representation will be rounded. Ignored if <= 0.
*/
public final void set(boolean isNegative, long source, int maximumDigits) {
this.isNegative = isNegative;
// This method does not expect a negative number. However,
// "source" can be a Long.MIN_VALUE (-9223372036854775808),
// if the number being formatted is a Long.MIN_VALUE. In that
// case, it will be formatted as -Long.MIN_VALUE, a number
// which is outside the legal range of a long, but which can
// be represented by DigitList.
if (source <= 0) {
if (source == Long.MIN_VALUE) {
decimalAt = count = MAX_COUNT;
System.arraycopy(LONG_MIN_REP, 0, digits, 0, count);
} else {
decimalAt = count = 0; // Values <= 0 format as zero
}
} else {
// Rewritten to improve performance. I used to call
// Long.toString(), which was about 4x slower than this code.
int left = MAX_COUNT;
int right;
while (source > 0) {
digits[--left] = (char)('0' + (source % 10));
source /= 10;
}
decimalAt = MAX_COUNT - left;
// Don't copy trailing zeros. We are guaranteed that there is at
// least one non-zero digit, so we don't have to check lower bounds.
for (right = MAX_COUNT - 1; digits[right] == '0'; --right)
;
count = right - left + 1;
System.arraycopy(digits, left, digits, 0, count);
}
if (maximumDigits > 0) round(maximumDigits);
}
/**
* Set the digit list to a representation of the given BigDecimal value.
* This method supports both fixed-point and exponential notation.
* @param isNegative Boolean value indicating whether the number is negative.
* @param source Value to be converted; must not be a value <= 0.
* @param maximumDigits The most fractional or total digits which should
* be converted.
* @param fixedPoint If true, then maximumDigits is the maximum
* fractional digits to be converted. If false, total digits.
*/
final void set(boolean isNegative, BigDecimal source, int maximumDigits, boolean fixedPoint) {
String s = source.toString();
extendDigits(s.length());
set(isNegative, s, maximumDigits, fixedPoint);
}
/**
* Set the digit list to a representation of the given BigInteger value.
* @param isNegative Boolean value indicating whether the number is negative.
* @param source Value to be converted; must be >= 0.
* @param maximumDigits The most digits which should be converted.
* If maximumDigits is lower than the number of significant digits
* in source, the representation will be rounded. Ignored if <= 0.
*/
final void set(boolean isNegative, BigInteger source, int maximumDigits) {
this.isNegative = isNegative;
String s = source.toString();
int len = s.length();
extendDigits(len);
s.getChars(0, len, digits, 0);
decimalAt = len;
int right;
for (right = len - 1; right >= 0 && digits[right] == '0'; --right)
;
count = right + 1;
if (maximumDigits > 0) {
round(maximumDigits);
}
}
/**
* equality test between two digit lists.
*/
public boolean equals(Object obj) {
if (this == obj) // quick check
return true;
if (!(obj instanceof DigitList)) // (1) same object?
return false;
DigitList other = (DigitList) obj;
if (count != other.count ||
decimalAt != other.decimalAt)
return false;
for (int i = 0; i < count; i++)
if (digits[i] != other.digits[i])
return false;
return true;
}
/**
* Generates the hash code for the digit list.
*/
public int hashCode() {
int hashcode = decimalAt;
for (int i = 0; i < count; i++) {
hashcode = hashcode * 37 + digits[i];
}
return hashcode;
}
/**
* Creates a copy of this object.
* @return a clone of this instance.
*/
public Object clone() {
try {
DigitList other = (DigitList) super.clone();
char[] newDigits = new char[digits.length];
System.arraycopy(digits, 0, newDigits, 0, digits.length);
other.digits = newDigits;
other.tempBuffer = null;
return other;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
/**
* Returns true if this DigitList represents Long.MIN_VALUE;
* false, otherwise. This is required so that getLong() works.
*/
private boolean isLongMIN_VALUE() {
if (decimalAt != count || count != MAX_COUNT) {
return false;
}
for (int i = 0; i < count; ++i) {
if (digits[i] != LONG_MIN_REP[i]) return false;
}
return true;
}
private static final int parseInt(char[] str, int offset, int strLen) {
char c;
boolean positive = true;
if ((c = str[offset]) == '-') {
positive = false;
offset++;
} else if (c == '+') {
offset++;
}
int value = 0;
while (offset < strLen) {
c = str[offset++];
if (c >= '0' && c <= '9') {
value = value * 10 + (c - '0');
} else {
break;
}
}
return positive ? value : -value;
}
// The digit part of -9223372036854775808L
private static final char[] LONG_MIN_REP = "9223372036854775808".toCharArray();
public String toString() {
if (isZero()) {
return "0";
}
StringBuffer buf = getStringBuffer();
buf.append("0.");
buf.append(digits, 0, count);
buf.append("x10^");
buf.append(decimalAt);
return buf.toString();
}
private StringBuffer tempBuffer;
private StringBuffer getStringBuffer() {
if (tempBuffer == null) {
tempBuffer = new StringBuffer(MAX_COUNT);
} else {
tempBuffer.setLength(0);
}
return tempBuffer;
}
private void extendDigits(int len) {
if (len > digits.length) {
digits = new char[len];
}
}
private final char[] getDataChars(int length) {
if (data == null || data.length < length) {
data = new char[length];
}
return data;
}
}