< prev index next >

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

Print this page
rev 52979 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: TBD


1096         // in the fast-path case, we need to reinitialize fastPathData anyway.
1097         if (isFastPath) {
1098             // We need to instantiate fastPathData if not already done.
1099             if (fastPathData == null) {
1100                 fastPathData = new FastPathData();
1101             }
1102 
1103             // Sets up the locale specific constants used when formatting.
1104             // '0' is our default representation of zero.
1105             fastPathData.zeroDelta = symbols.getZeroDigit() - '0';
1106             fastPathData.groupingChar = symbols.getGroupingSeparator();
1107 
1108             // Sets up fractional constants related to currency/decimal pattern.
1109             fastPathData.fractionalMaxIntBound = (isCurrencyFormat)
1110                     ? 99 : 999;
1111             fastPathData.fractionalScaleFactor = (isCurrencyFormat)
1112                     ? 100.0d : 1000.0d;
1113 
1114             // Records the need for adding prefix or suffix
1115             fastPathData.positiveAffixesRequired
1116                     = (positivePrefix.length() != 0)
1117                         || (positiveSuffix.length() != 0);
1118             fastPathData.negativeAffixesRequired
1119                     = (negativePrefix.length() != 0)
1120                         || (negativeSuffix.length() != 0);
1121 
1122             // Creates a cached char container for result, with max possible size.
1123             int maxNbIntegralDigits = 10;
1124             int maxNbGroups = 3;
1125             int containerSize
1126                     = Math.max(positivePrefix.length(), negativePrefix.length())
1127                     + maxNbIntegralDigits + maxNbGroups + 1
1128                     + maximumFractionDigits
1129                     + Math.max(positiveSuffix.length(), negativeSuffix.length());
1130 
1131             fastPathData.fastPathContainer = new char[containerSize];
1132 
1133             // Sets up prefix and suffix char arrays constants.
1134             fastPathData.charsPositiveSuffix = positiveSuffix.toCharArray();
1135             fastPathData.charsNegativeSuffix = negativeSuffix.toCharArray();
1136             fastPathData.charsPositivePrefix = positivePrefix.toCharArray();
1137             fastPathData.charsNegativePrefix = negativePrefix.toCharArray();
1138 
1139             // Sets up fixed index positions for integral and fractional digits.
1140             // Sets up decimal point in cached result container.


2045 
2046     /**
2047      * Appends the String <code>string</code> to <code>result</code>.
2048      * <code>delegate</code> is notified of all  the
2049      * <code>FieldPosition</code>s in <code>positions</code>.
2050      * <p>
2051      * If one of the <code>FieldPosition</code>s in <code>positions</code>
2052      * identifies a <code>SIGN</code> attribute, it is mapped to
2053      * <code>signAttribute</code>. This is used
2054      * to map the <code>SIGN</code> attribute to the <code>EXPONENT</code>
2055      * attribute as necessary.
2056      * <p>
2057      * This is used by <code>subformat</code> to add the prefix/suffix.
2058      */
2059     private void append(StringBuffer result, String string,
2060                         FieldDelegate delegate,
2061                         FieldPosition[] positions,
2062                         Format.Field signAttribute) {
2063         int start = result.length();
2064 
2065         if (string.length() > 0) {
2066             result.append(string);
2067             for (int counter = 0, max = positions.length; counter < max;
2068                  counter++) {
2069                 FieldPosition fp = positions[counter];
2070                 Format.Field attribute = fp.getFieldAttribute();
2071 
2072                 if (attribute == Field.SIGN) {
2073                     attribute = signAttribute;
2074                 }
2075                 delegate.formatted(attribute, attribute,
2076                                    start + fp.getBeginIndex(),
2077                                    start + fp.getEndIndex(), result);
2078             }
2079         }
2080     }
2081 
2082     /**
2083      * Parses text from a string to produce a <code>Number</code>.
2084      * <p>
2085      * The method attempts to parse text starting at the index given by


3025      */
3026     private FieldPosition[] expandAffix(String pattern) {
3027         ArrayList<FieldPosition> positions = null;
3028         int stringIndex = 0;
3029         for (int i=0; i<pattern.length(); ) {
3030             char c = pattern.charAt(i++);
3031             if (c == QUOTE) {
3032                 int field = -1;
3033                 Format.Field fieldID = null;
3034                 c = pattern.charAt(i++);
3035                 switch (c) {
3036                 case CURRENCY_SIGN:
3037                     String string;
3038                     if (i<pattern.length() &&
3039                         pattern.charAt(i) == CURRENCY_SIGN) {
3040                         ++i;
3041                         string = symbols.getInternationalCurrencySymbol();
3042                     } else {
3043                         string = symbols.getCurrencySymbol();
3044                     }
3045                     if (string.length() > 0) {
3046                         if (positions == null) {
3047                             positions = new ArrayList<>(2);
3048                         }
3049                         FieldPosition fp = new FieldPosition(Field.CURRENCY);
3050                         fp.setBeginIndex(stringIndex);
3051                         fp.setEndIndex(stringIndex + string.length());
3052                         positions.add(fp);
3053                         stringIndex += string.length();
3054                     }
3055                     continue;
3056                 case PATTERN_PERCENT:
3057                     c = symbols.getPercent();
3058                     field = -1;
3059                     fieldID = Field.PERCENT;
3060                     break;
3061                 case PATTERN_PER_MILLE:
3062                     c = symbols.getPerMill();
3063                     field = -1;
3064                     fieldID = Field.PERMILLE;
3065                     break;


3596                 setMinimumIntegerDigits(effectiveDecimalPos - digitLeftCount);
3597                 setMaximumIntegerDigits(useExponentialNotation ?
3598                     digitLeftCount + getMinimumIntegerDigits() :
3599                     MAXIMUM_INTEGER_DIGITS);
3600                 setMaximumFractionDigits(decimalPos >= 0 ?
3601                     (digitTotalCount - decimalPos) : 0);
3602                 setMinimumFractionDigits(decimalPos >= 0 ?
3603                     (digitLeftCount + zeroDigitCount - decimalPos) : 0);
3604                 setGroupingUsed(groupingCount > 0);
3605                 this.groupingSize = (groupingCount > 0) ? groupingCount : 0;
3606                 this.multiplier = multiplier;
3607                 setDecimalSeparatorAlwaysShown(decimalPos == 0 ||
3608                     decimalPos == digitTotalCount);
3609             } else {
3610                 negPrefixPattern = prefix.toString();
3611                 negSuffixPattern = suffix.toString();
3612                 gotNegative = true;
3613             }
3614         }
3615 
3616         if (pattern.length() == 0) {
3617             posPrefixPattern = posSuffixPattern = "";
3618             setMinimumIntegerDigits(0);
3619             setMaximumIntegerDigits(MAXIMUM_INTEGER_DIGITS);
3620             setMinimumFractionDigits(0);
3621             setMaximumFractionDigits(MAXIMUM_FRACTION_DIGITS);
3622         }
3623 
3624         // If there was no negative pattern, or if the negative pattern is
3625         // identical to the positive pattern, then prepend the minus sign to
3626         // the positive pattern to form the negative pattern.
3627         if (!gotNegative ||
3628             (negPrefixPattern.equals(posPrefixPattern)
3629              && negSuffixPattern.equals(posSuffixPattern))) {
3630             negSuffixPattern = posSuffixPattern;
3631             negPrefixPattern = "'-" + posPrefixPattern;
3632         }
3633 
3634         expandAffixes();
3635     }
3636 




1096         // in the fast-path case, we need to reinitialize fastPathData anyway.
1097         if (isFastPath) {
1098             // We need to instantiate fastPathData if not already done.
1099             if (fastPathData == null) {
1100                 fastPathData = new FastPathData();
1101             }
1102 
1103             // Sets up the locale specific constants used when formatting.
1104             // '0' is our default representation of zero.
1105             fastPathData.zeroDelta = symbols.getZeroDigit() - '0';
1106             fastPathData.groupingChar = symbols.getGroupingSeparator();
1107 
1108             // Sets up fractional constants related to currency/decimal pattern.
1109             fastPathData.fractionalMaxIntBound = (isCurrencyFormat)
1110                     ? 99 : 999;
1111             fastPathData.fractionalScaleFactor = (isCurrencyFormat)
1112                     ? 100.0d : 1000.0d;
1113 
1114             // Records the need for adding prefix or suffix
1115             fastPathData.positiveAffixesRequired
1116                     = !positivePrefix.isEmpty() || !positiveSuffix.isEmpty();

1117             fastPathData.negativeAffixesRequired
1118                     = !negativePrefix.isEmpty() || !negativeSuffix.isEmpty();

1119 
1120             // Creates a cached char container for result, with max possible size.
1121             int maxNbIntegralDigits = 10;
1122             int maxNbGroups = 3;
1123             int containerSize
1124                     = Math.max(positivePrefix.length(), negativePrefix.length())
1125                     + maxNbIntegralDigits + maxNbGroups + 1
1126                     + maximumFractionDigits
1127                     + Math.max(positiveSuffix.length(), negativeSuffix.length());
1128 
1129             fastPathData.fastPathContainer = new char[containerSize];
1130 
1131             // Sets up prefix and suffix char arrays constants.
1132             fastPathData.charsPositiveSuffix = positiveSuffix.toCharArray();
1133             fastPathData.charsNegativeSuffix = negativeSuffix.toCharArray();
1134             fastPathData.charsPositivePrefix = positivePrefix.toCharArray();
1135             fastPathData.charsNegativePrefix = negativePrefix.toCharArray();
1136 
1137             // Sets up fixed index positions for integral and fractional digits.
1138             // Sets up decimal point in cached result container.


2043 
2044     /**
2045      * Appends the String <code>string</code> to <code>result</code>.
2046      * <code>delegate</code> is notified of all  the
2047      * <code>FieldPosition</code>s in <code>positions</code>.
2048      * <p>
2049      * If one of the <code>FieldPosition</code>s in <code>positions</code>
2050      * identifies a <code>SIGN</code> attribute, it is mapped to
2051      * <code>signAttribute</code>. This is used
2052      * to map the <code>SIGN</code> attribute to the <code>EXPONENT</code>
2053      * attribute as necessary.
2054      * <p>
2055      * This is used by <code>subformat</code> to add the prefix/suffix.
2056      */
2057     private void append(StringBuffer result, String string,
2058                         FieldDelegate delegate,
2059                         FieldPosition[] positions,
2060                         Format.Field signAttribute) {
2061         int start = result.length();
2062 
2063         if (!string.isEmpty()) {
2064             result.append(string);
2065             for (int counter = 0, max = positions.length; counter < max;
2066                  counter++) {
2067                 FieldPosition fp = positions[counter];
2068                 Format.Field attribute = fp.getFieldAttribute();
2069 
2070                 if (attribute == Field.SIGN) {
2071                     attribute = signAttribute;
2072                 }
2073                 delegate.formatted(attribute, attribute,
2074                                    start + fp.getBeginIndex(),
2075                                    start + fp.getEndIndex(), result);
2076             }
2077         }
2078     }
2079 
2080     /**
2081      * Parses text from a string to produce a <code>Number</code>.
2082      * <p>
2083      * The method attempts to parse text starting at the index given by


3023      */
3024     private FieldPosition[] expandAffix(String pattern) {
3025         ArrayList<FieldPosition> positions = null;
3026         int stringIndex = 0;
3027         for (int i=0; i<pattern.length(); ) {
3028             char c = pattern.charAt(i++);
3029             if (c == QUOTE) {
3030                 int field = -1;
3031                 Format.Field fieldID = null;
3032                 c = pattern.charAt(i++);
3033                 switch (c) {
3034                 case CURRENCY_SIGN:
3035                     String string;
3036                     if (i<pattern.length() &&
3037                         pattern.charAt(i) == CURRENCY_SIGN) {
3038                         ++i;
3039                         string = symbols.getInternationalCurrencySymbol();
3040                     } else {
3041                         string = symbols.getCurrencySymbol();
3042                     }
3043                     if (!string.isEmpty()) {
3044                         if (positions == null) {
3045                             positions = new ArrayList<>(2);
3046                         }
3047                         FieldPosition fp = new FieldPosition(Field.CURRENCY);
3048                         fp.setBeginIndex(stringIndex);
3049                         fp.setEndIndex(stringIndex + string.length());
3050                         positions.add(fp);
3051                         stringIndex += string.length();
3052                     }
3053                     continue;
3054                 case PATTERN_PERCENT:
3055                     c = symbols.getPercent();
3056                     field = -1;
3057                     fieldID = Field.PERCENT;
3058                     break;
3059                 case PATTERN_PER_MILLE:
3060                     c = symbols.getPerMill();
3061                     field = -1;
3062                     fieldID = Field.PERMILLE;
3063                     break;


3594                 setMinimumIntegerDigits(effectiveDecimalPos - digitLeftCount);
3595                 setMaximumIntegerDigits(useExponentialNotation ?
3596                     digitLeftCount + getMinimumIntegerDigits() :
3597                     MAXIMUM_INTEGER_DIGITS);
3598                 setMaximumFractionDigits(decimalPos >= 0 ?
3599                     (digitTotalCount - decimalPos) : 0);
3600                 setMinimumFractionDigits(decimalPos >= 0 ?
3601                     (digitLeftCount + zeroDigitCount - decimalPos) : 0);
3602                 setGroupingUsed(groupingCount > 0);
3603                 this.groupingSize = (groupingCount > 0) ? groupingCount : 0;
3604                 this.multiplier = multiplier;
3605                 setDecimalSeparatorAlwaysShown(decimalPos == 0 ||
3606                     decimalPos == digitTotalCount);
3607             } else {
3608                 negPrefixPattern = prefix.toString();
3609                 negSuffixPattern = suffix.toString();
3610                 gotNegative = true;
3611             }
3612         }
3613 
3614         if (pattern.isEmpty()) {
3615             posPrefixPattern = posSuffixPattern = "";
3616             setMinimumIntegerDigits(0);
3617             setMaximumIntegerDigits(MAXIMUM_INTEGER_DIGITS);
3618             setMinimumFractionDigits(0);
3619             setMaximumFractionDigits(MAXIMUM_FRACTION_DIGITS);
3620         }
3621 
3622         // If there was no negative pattern, or if the negative pattern is
3623         // identical to the positive pattern, then prepend the minus sign to
3624         // the positive pattern to form the negative pattern.
3625         if (!gotNegative ||
3626             (negPrefixPattern.equals(posPrefixPattern)
3627              && negSuffixPattern.equals(posSuffixPattern))) {
3628             negSuffixPattern = posSuffixPattern;
3629             negPrefixPattern = "'-" + posPrefixPattern;
3630         }
3631 
3632         expandAffixes();
3633     }
3634 


< prev index next >