1 /*
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
405 @Override
406 public final StringBuffer format(Object number,
407 StringBuffer toAppendTo,
408 FieldPosition fieldPosition) {
409 if (number instanceof Long || number instanceof Integer
410 || number instanceof Short || number instanceof Byte
411 || number instanceof AtomicInteger
412 || number instanceof AtomicLong
413 || (number instanceof BigInteger
414 && ((BigInteger) number).bitLength() < 64)) {
415 return format(((Number) number).longValue(), toAppendTo,
416 fieldPosition);
417 } else if (number instanceof BigDecimal) {
418 return format((BigDecimal) number, toAppendTo, fieldPosition);
419 } else if (number instanceof BigInteger) {
420 return format((BigInteger) number, toAppendTo, fieldPosition);
421 } else if (number instanceof Number) {
422 return format(((Number) number).doubleValue(), toAppendTo, fieldPosition);
423 } else {
424 throw new IllegalArgumentException("Cannot format "
425 + number.getClass().getName() + " as a number");
426 }
427 }
428
429 /**
430 * Formats a double to produce a string representing its compact form.
431 * @param number the double number to format
432 * @param result where the text is to be appended
433 * @param fieldPosition keeps track on the position of the field within
434 * the returned string. For example, to format
435 * a number {@code 1234567.89} in the
436 * {@link java.util.Locale#US US locale}
437 * if the given {@code fieldPosition} is
438 * {@link NumberFormat#INTEGER_FIELD}, the begin
439 * index and end index of {@code fieldPosition}
440 * will be set to 0 and 1, respectively for the
441 * output string {@code 1M}. Similarly, positions
442 * of the prefix and the suffix fields can be
443 * obtained using {@link NumberFormat.Field#PREFIX}
444 * and {@link NumberFormat.Field#SUFFIX} respectively.
445 * @return the {@code StringBuffer} passed in as {@code result}
1035 private void processCompactPatterns() {
1036 int size = compactPatterns.length;
1037 positivePrefixPatterns = new ArrayList<>(size);
1038 negativePrefixPatterns = new ArrayList<>(size);
1039 positiveSuffixPatterns = new ArrayList<>(size);
1040 negativeSuffixPatterns = new ArrayList<>(size);
1041 divisors = new ArrayList<>(size);
1042
1043 for (int index = 0; index < size; index++) {
1044 applyPattern(compactPatterns[index], index);
1045 }
1046 }
1047
1048 /**
1049 * Process a compact pattern at a specific {@code index}
1050 * @param pattern the compact pattern to be processed
1051 * @param index index in the array of compact patterns
1052 *
1053 */
1054 private void applyPattern(String pattern, int index) {
1055
1056 int start = 0;
1057 boolean gotNegative = false;
1058
1059 String positivePrefix = "";
1060 String positiveSuffix = "";
1061 String negativePrefix = "";
1062 String negativeSuffix = "";
1063 String zeros = "";
1064 for (int j = 1; j >= 0 && start < pattern.length(); --j) {
1065
1066 StringBuffer prefix = new StringBuffer();
1067 StringBuffer suffix = new StringBuffer();
1068 boolean inQuote = false;
1069 // The phase ranges from 0 to 2. Phase 0 is the prefix. Phase 1 is
1070 // the section of the pattern with digits. Phase 2 is the suffix.
1071 // The separation of the characters into phases is
1072 // strictly enforced; if phase 1 characters are to appear in the
1073 // suffix, for example, they must be quoted.
1074 int phase = 0;
|
1 /*
2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
405 @Override
406 public final StringBuffer format(Object number,
407 StringBuffer toAppendTo,
408 FieldPosition fieldPosition) {
409 if (number instanceof Long || number instanceof Integer
410 || number instanceof Short || number instanceof Byte
411 || number instanceof AtomicInteger
412 || number instanceof AtomicLong
413 || (number instanceof BigInteger
414 && ((BigInteger) number).bitLength() < 64)) {
415 return format(((Number) number).longValue(), toAppendTo,
416 fieldPosition);
417 } else if (number instanceof BigDecimal) {
418 return format((BigDecimal) number, toAppendTo, fieldPosition);
419 } else if (number instanceof BigInteger) {
420 return format((BigInteger) number, toAppendTo, fieldPosition);
421 } else if (number instanceof Number) {
422 return format(((Number) number).doubleValue(), toAppendTo, fieldPosition);
423 } else {
424 throw new IllegalArgumentException("Cannot format "
425 + (number != null ? number.getClass().getName() : null) + " as a number");
426 }
427 }
428
429 /**
430 * Formats a double to produce a string representing its compact form.
431 * @param number the double number to format
432 * @param result where the text is to be appended
433 * @param fieldPosition keeps track on the position of the field within
434 * the returned string. For example, to format
435 * a number {@code 1234567.89} in the
436 * {@link java.util.Locale#US US locale}
437 * if the given {@code fieldPosition} is
438 * {@link NumberFormat#INTEGER_FIELD}, the begin
439 * index and end index of {@code fieldPosition}
440 * will be set to 0 and 1, respectively for the
441 * output string {@code 1M}. Similarly, positions
442 * of the prefix and the suffix fields can be
443 * obtained using {@link NumberFormat.Field#PREFIX}
444 * and {@link NumberFormat.Field#SUFFIX} respectively.
445 * @return the {@code StringBuffer} passed in as {@code result}
1035 private void processCompactPatterns() {
1036 int size = compactPatterns.length;
1037 positivePrefixPatterns = new ArrayList<>(size);
1038 negativePrefixPatterns = new ArrayList<>(size);
1039 positiveSuffixPatterns = new ArrayList<>(size);
1040 negativeSuffixPatterns = new ArrayList<>(size);
1041 divisors = new ArrayList<>(size);
1042
1043 for (int index = 0; index < size; index++) {
1044 applyPattern(compactPatterns[index], index);
1045 }
1046 }
1047
1048 /**
1049 * Process a compact pattern at a specific {@code index}
1050 * @param pattern the compact pattern to be processed
1051 * @param index index in the array of compact patterns
1052 *
1053 */
1054 private void applyPattern(String pattern, int index) {
1055
1056 if (pattern == null) {
1057 throw new IllegalArgumentException("A null compact pattern" +
1058 " encountered at index: " + index);
1059 }
1060
1061 int start = 0;
1062 boolean gotNegative = false;
1063
1064 String positivePrefix = "";
1065 String positiveSuffix = "";
1066 String negativePrefix = "";
1067 String negativeSuffix = "";
1068 String zeros = "";
1069 for (int j = 1; j >= 0 && start < pattern.length(); --j) {
1070
1071 StringBuffer prefix = new StringBuffer();
1072 StringBuffer suffix = new StringBuffer();
1073 boolean inQuote = false;
1074 // The phase ranges from 0 to 2. Phase 0 is the prefix. Phase 1 is
1075 // the section of the pattern with digits. Phase 2 is the suffix.
1076 // The separation of the characters into phases is
1077 // strictly enforced; if phase 1 characters are to appear in the
1078 // suffix, for example, they must be quoted.
1079 int phase = 0;
|