< prev index next >

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

Print this page
rev 54198 : [mq]: 8220224


 819 
 820     /**
 821      * Expands an affix {@code pattern} into a string of literals.
 822      * All characters in the pattern are literals unless prefixed by QUOTE.
 823      * The character prefixed by QUOTE is replaced with its respective
 824      * localized literal.
 825      * @param pattern a compact number pattern affix
 826      * @return an expanded affix
 827      */
 828     private String expandAffix(String pattern) {
 829         // Return if no quoted character exists
 830         if (pattern.indexOf(QUOTE) < 0) {
 831             return pattern;
 832         }
 833         StringBuilder sb = new StringBuilder();
 834         for (int index = 0; index < pattern.length();) {
 835             char ch = pattern.charAt(index++);
 836             if (ch == QUOTE) {
 837                 ch = pattern.charAt(index++);
 838                 if (ch == MINUS_SIGN) {
 839                     ch = symbols.getMinusSign();

 840                 }
 841             }
 842             sb.append(ch);
 843         }
 844         return sb.toString();
 845     }
 846 
 847     /**
 848      * Returns a list of {@code FieldPostion} in the given {@code pattern}.
 849      * @param pattern the pattern to be parsed for {@code FieldPosition}
 850      * @param field whether a PREFIX or SUFFIX field
 851      * @return a list of {@code FieldPostion}
 852      */
 853     private List<FieldPosition> getFieldPositions(String pattern, Field field) {
 854         List<FieldPosition> positions = new ArrayList<>();
 855         StringBuilder affix = new StringBuilder();
 856         int stringIndex = 0;
 857         for (int index = 0; index < pattern.length();) {
 858             char ch = pattern.charAt(index++);
 859             if (ch == QUOTE) {
 860                 ch = pattern.charAt(index++);
 861                 if (ch == MINUS_SIGN) {
 862                     ch = symbols.getMinusSign();
 863                     FieldPosition fp = new FieldPosition(NumberFormat.Field.SIGN);
 864                     fp.setBeginIndex(stringIndex);
 865                     fp.setEndIndex(stringIndex + 1);
 866                     positions.add(fp);



 867                 }
 868             }
 869             stringIndex++;
 870             affix.append(ch);
 871         }
 872         if (affix.length() != 0) {
 873             FieldPosition fp = new FieldPosition(field);
 874             fp.setBeginIndex(0);
 875             fp.setEndIndex(affix.length());
 876             positions.add(fp);
 877         }
 878         return positions;
 879     }
 880 
 881     /**
 882      * Select the index of the matched compact number pattern for
 883      * the given {@code long} {@code number}.
 884      *
 885      * @param number number to be formatted
 886      * @return index of matched compact pattern;




 819 
 820     /**
 821      * Expands an affix {@code pattern} into a string of literals.
 822      * All characters in the pattern are literals unless prefixed by QUOTE.
 823      * The character prefixed by QUOTE is replaced with its respective
 824      * localized literal.
 825      * @param pattern a compact number pattern affix
 826      * @return an expanded affix
 827      */
 828     private String expandAffix(String pattern) {
 829         // Return if no quoted character exists
 830         if (pattern.indexOf(QUOTE) < 0) {
 831             return pattern;
 832         }
 833         StringBuilder sb = new StringBuilder();
 834         for (int index = 0; index < pattern.length();) {
 835             char ch = pattern.charAt(index++);
 836             if (ch == QUOTE) {
 837                 ch = pattern.charAt(index++);
 838                 if (ch == MINUS_SIGN) {
 839                     sb.append(symbols.getMinusSignText());
 840                     continue;
 841                 }
 842             }
 843             sb.append(ch);
 844         }
 845         return sb.toString();
 846     }
 847 
 848     /**
 849      * Returns a list of {@code FieldPostion} in the given {@code pattern}.
 850      * @param pattern the pattern to be parsed for {@code FieldPosition}
 851      * @param field whether a PREFIX or SUFFIX field
 852      * @return a list of {@code FieldPostion}
 853      */
 854     private List<FieldPosition> getFieldPositions(String pattern, Field field) {
 855         List<FieldPosition> positions = new ArrayList<>();
 856         StringBuilder affix = new StringBuilder();
 857         int stringIndex = 0;
 858         for (int index = 0; index < pattern.length();) {
 859             char ch = pattern.charAt(index++);
 860             if (ch == QUOTE) {
 861                 ch = pattern.charAt(index++);
 862                 if (ch == MINUS_SIGN) {
 863                     String minusText = symbols.getMinusSignText();
 864                     FieldPosition fp = new FieldPosition(NumberFormat.Field.SIGN);
 865                     fp.setBeginIndex(stringIndex);
 866                     fp.setEndIndex(stringIndex + minusText.length());
 867                     positions.add(fp);
 868                     stringIndex += minusText.length();
 869                     affix.append(minusText);
 870                     continue;
 871                 }
 872             }
 873             stringIndex++;
 874             affix.append(ch);
 875         }
 876         if (affix.length() != 0) {
 877             FieldPosition fp = new FieldPosition(field);
 878             fp.setBeginIndex(0);
 879             fp.setEndIndex(affix.length());
 880             positions.add(fp);
 881         }
 882         return positions;
 883     }
 884 
 885     /**
 886      * Select the index of the matched compact number pattern for
 887      * the given {@code long} {@code number}.
 888      *
 889      * @param number number to be formatted
 890      * @return index of matched compact pattern;


< prev index next >