< prev index next >

src/java.base/share/classes/java/text/CompactNumberFormat.java

Print this page


   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


 389      *                         {@link NumberFormat#INTEGER_FIELD}, the begin
 390      *                         index and end index of {@code fieldPosition}
 391      *                         will be set to 0 and 3, respectively for the
 392      *                         output string {@code 123M}. Similarly, positions
 393      *                         of the prefix and the suffix fields can be
 394      *                         obtained using {@link NumberFormat.Field#PREFIX}
 395      *                         and {@link NumberFormat.Field#SUFFIX} respectively.
 396      * @return           the {@code StringBuffer} passed in as {@code toAppendTo}
 397      * @throws           IllegalArgumentException if {@code number} is
 398      *                   {@code null} or not an instance of {@code Number}
 399      * @throws           NullPointerException if {@code toAppendTo} or
 400      *                   {@code fieldPosition} is {@code null}
 401      * @throws           ArithmeticException if rounding is needed with rounding
 402      *                   mode being set to {@code RoundingMode.UNNECESSARY}
 403      * @see              FieldPosition
 404      */
 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 


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


 389      *                         {@link NumberFormat#INTEGER_FIELD}, the begin
 390      *                         index and end index of {@code fieldPosition}
 391      *                         will be set to 0 and 3, respectively for the
 392      *                         output string {@code 123M}. Similarly, positions
 393      *                         of the prefix and the suffix fields can be
 394      *                         obtained using {@link NumberFormat.Field#PREFIX}
 395      *                         and {@link NumberFormat.Field#SUFFIX} respectively.
 396      * @return           the {@code StringBuffer} passed in as {@code toAppendTo}
 397      * @throws           IllegalArgumentException if {@code number} is
 398      *                   {@code null} or not an instance of {@code Number}
 399      * @throws           NullPointerException if {@code toAppendTo} or
 400      *                   {@code fieldPosition} is {@code null}
 401      * @throws           ArithmeticException if rounding is needed with rounding
 402      *                   mode being set to {@code RoundingMode.UNNECESSARY}
 403      * @see              FieldPosition
 404      */
 405     @Override
 406     public final StringBuffer format(Object number,
 407             StringBuffer toAppendTo,
 408             FieldPosition fieldPosition) {
 409 
 410         if (number == null) {
 411             throw new IllegalArgumentException("Cannot format null as a number");
 412         }
 413 
 414         if (number instanceof Long || number instanceof Integer
 415                 || number instanceof Short || number instanceof Byte
 416                 || number instanceof AtomicInteger
 417                 || number instanceof AtomicLong
 418                 || (number instanceof BigInteger
 419                 && ((BigInteger) number).bitLength() < 64)) {
 420             return format(((Number) number).longValue(), toAppendTo,
 421                     fieldPosition);
 422         } else if (number instanceof BigDecimal) {
 423             return format((BigDecimal) number, toAppendTo, fieldPosition);
 424         } else if (number instanceof BigInteger) {
 425             return format((BigInteger) number, toAppendTo, fieldPosition);
 426         } else if (number instanceof Number) {
 427             return format(((Number) number).doubleValue(), toAppendTo, fieldPosition);
 428         } else {
 429             throw new IllegalArgumentException("Cannot format "
 430                     + number.getClass().getName() + " as a number");
 431         }
 432     }
 433 


1040     private void processCompactPatterns() {
1041         int size = compactPatterns.length;
1042         positivePrefixPatterns = new ArrayList<>(size);
1043         negativePrefixPatterns = new ArrayList<>(size);
1044         positiveSuffixPatterns = new ArrayList<>(size);
1045         negativeSuffixPatterns = new ArrayList<>(size);
1046         divisors = new ArrayList<>(size);
1047 
1048         for (int index = 0; index < size; index++) {
1049             applyPattern(compactPatterns[index], index);
1050         }
1051     }
1052 
1053     /**
1054      * Process a compact pattern at a specific {@code index}
1055      * @param pattern the compact pattern to be processed
1056      * @param index index in the array of compact patterns
1057      *
1058      */
1059     private void applyPattern(String pattern, int index) {
1060 
1061         if (pattern == null) {
1062             throw new IllegalArgumentException("A null compact pattern" +
1063                     " encountered at index: " + index);
1064         }
1065 
1066         int start = 0;
1067         boolean gotNegative = false;
1068 
1069         String positivePrefix = "";
1070         String positiveSuffix = "";
1071         String negativePrefix = "";
1072         String negativeSuffix = "";
1073         String zeros = "";
1074         for (int j = 1; j >= 0 && start < pattern.length(); --j) {
1075 
1076             StringBuffer prefix = new StringBuffer();
1077             StringBuffer suffix = new StringBuffer();
1078             boolean inQuote = false;
1079             // The phase ranges from 0 to 2.  Phase 0 is the prefix.  Phase 1 is
1080             // the section of the pattern with digits. Phase 2 is the suffix.
1081             // The separation of the characters into phases is
1082             // strictly enforced; if phase 1 characters are to appear in the
1083             // suffix, for example, they must be quoted.
1084             int phase = 0;


< prev index next >