< prev index next >

src/java.base/share/classes/java/util/Locale.java

Print this page
rev 52881 : 8214971: Replace use of string.equals("") with isEmpty()
Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad


2209      * @param stringList the list of strings to be formatted.
2210      * and formatting them into a list.
2211      * @param pattern should take 2 arguments for reduction
2212      * @return a string representing the list.
2213      */
2214     private static String formatList(String[] stringList, String pattern) {
2215         // If we have no list patterns, compose the list in a simple,
2216         // non-localized way.
2217         if (pattern == null) {
2218             return Arrays.stream(stringList).collect(Collectors.joining(","));
2219         }
2220 
2221         switch (stringList.length) {
2222             case 0:
2223                 return "";
2224             case 1:
2225                 return stringList[0];
2226             default:
2227                 return Arrays.stream(stringList).reduce("",
2228                     (s1, s2) -> {
2229                         if (s1.equals("")) {
2230                             return s2;
2231                         }
2232                         if (s2.equals("")) {
2233                             return s1;
2234                         }
2235                         return MessageFormat.format(pattern, s1, s2);
2236                     });
2237         }
2238     }
2239 
2240     // Duplicate of sun.util.locale.UnicodeLocaleExtension.isKey in order to
2241     // avoid its class loading.
2242     private static boolean isUnicodeExtensionKey(String s) {
2243         // 2alphanum
2244         return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s);
2245     }
2246 
2247     /**
2248      * @serialField language    String
2249      *      language subtag in lower case.
2250      *      (See <a href="java.base/java/util/Locale.html#getLanguage()">getLanguage()</a>)
2251      * @serialField country     String
2252      *      country subtag in upper case.


3052                 || range.endsWith("-")) {
3053                 isIllFormed = true;
3054             } else {
3055                 for (int i = 1; i < subtags.length; i++) {
3056                     if (isSubtagIllFormed(subtags[i], false)) {
3057                         isIllFormed = true;
3058                         break;
3059                     }
3060                 }
3061             }
3062             if (isIllFormed) {
3063                 throw new IllegalArgumentException("range=" + range);
3064             }
3065 
3066             this.range = range;
3067             this.weight = weight;
3068         }
3069 
3070         private static boolean isSubtagIllFormed(String subtag,
3071                                                  boolean isFirstSubtag) {
3072             if (subtag.equals("") || subtag.length() > 8) {
3073                 return true;
3074             } else if (subtag.equals("*")) {
3075                 return false;
3076             }
3077             char[] charArray = subtag.toCharArray();
3078             if (isFirstSubtag) { // ALPHA
3079                 for (char c : charArray) {
3080                     if (c < 'a' || c > 'z') {
3081                         return true;
3082                     }
3083                 }
3084             } else { // ALPHA / DIGIT
3085                 for (char c : charArray) {
3086                     if (c < '0' || (c > '9' && c < 'a') || c > 'z') {
3087                         return true;
3088                     }
3089                 }
3090             }
3091             return false;
3092         }




2209      * @param stringList the list of strings to be formatted.
2210      * and formatting them into a list.
2211      * @param pattern should take 2 arguments for reduction
2212      * @return a string representing the list.
2213      */
2214     private static String formatList(String[] stringList, String pattern) {
2215         // If we have no list patterns, compose the list in a simple,
2216         // non-localized way.
2217         if (pattern == null) {
2218             return Arrays.stream(stringList).collect(Collectors.joining(","));
2219         }
2220 
2221         switch (stringList.length) {
2222             case 0:
2223                 return "";
2224             case 1:
2225                 return stringList[0];
2226             default:
2227                 return Arrays.stream(stringList).reduce("",
2228                     (s1, s2) -> {
2229                         if (s1.isEmpty()) {
2230                             return s2;
2231                         }
2232                         if (s2.isEmpty()) {
2233                             return s1;
2234                         }
2235                         return MessageFormat.format(pattern, s1, s2);
2236                     });
2237         }
2238     }
2239 
2240     // Duplicate of sun.util.locale.UnicodeLocaleExtension.isKey in order to
2241     // avoid its class loading.
2242     private static boolean isUnicodeExtensionKey(String s) {
2243         // 2alphanum
2244         return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s);
2245     }
2246 
2247     /**
2248      * @serialField language    String
2249      *      language subtag in lower case.
2250      *      (See <a href="java.base/java/util/Locale.html#getLanguage()">getLanguage()</a>)
2251      * @serialField country     String
2252      *      country subtag in upper case.


3052                 || range.endsWith("-")) {
3053                 isIllFormed = true;
3054             } else {
3055                 for (int i = 1; i < subtags.length; i++) {
3056                     if (isSubtagIllFormed(subtags[i], false)) {
3057                         isIllFormed = true;
3058                         break;
3059                     }
3060                 }
3061             }
3062             if (isIllFormed) {
3063                 throw new IllegalArgumentException("range=" + range);
3064             }
3065 
3066             this.range = range;
3067             this.weight = weight;
3068         }
3069 
3070         private static boolean isSubtagIllFormed(String subtag,
3071                                                  boolean isFirstSubtag) {
3072             if (subtag.isEmpty() || subtag.length() > 8) {
3073                 return true;
3074             } else if (subtag.equals("*")) {
3075                 return false;
3076             }
3077             char[] charArray = subtag.toCharArray();
3078             if (isFirstSubtag) { // ALPHA
3079                 for (char c : charArray) {
3080                     if (c < 'a' || c > 'z') {
3081                         return true;
3082                     }
3083                 }
3084             } else { // ALPHA / DIGIT
3085                 for (char c : charArray) {
3086                     if (c < '0' || (c > '9' && c < 'a') || c > 'z') {
3087                         return true;
3088                     }
3089                 }
3090             }
3091             return false;
3092         }


< prev index next >