mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8287340: Refactor old code using StringTokenizer in locale related code
Reviewed-by: iris, joehw
This commit is contained in:
parent
ccec5d1e85
commit
26d24263c7
4 changed files with 115 additions and 171 deletions
|
@ -33,14 +33,12 @@ import java.text.spi.BreakIteratorProvider;
|
|||
import java.text.spi.CollatorProvider;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.spi.CalendarDataProvider;
|
||||
import java.util.spi.CalendarNameProvider;
|
||||
|
@ -63,13 +61,13 @@ public class CLDRLocaleProviderAdapter extends JRELocaleProviderAdapter {
|
|||
|
||||
// parent locales map
|
||||
private static volatile Map<Locale, Locale> parentLocalesMap;
|
||||
// language aliases map
|
||||
private static volatile Map<String,String> langAliasesMap;
|
||||
// cache to hold locale to locale mapping for language aliases.
|
||||
private static final Map<Locale, Locale> langAliasesCache;
|
||||
// cache the available locales
|
||||
private static volatile Locale[] AVAILABLE_LOCALES;
|
||||
|
||||
static {
|
||||
parentLocalesMap = new ConcurrentHashMap<>();
|
||||
langAliasesMap = new ConcurrentHashMap<>();
|
||||
langAliasesCache = new ConcurrentHashMap<>();
|
||||
// Assuming these locales do NOT have irregular parent locales.
|
||||
parentLocalesMap.put(Locale.ROOT, Locale.ROOT);
|
||||
|
@ -175,29 +173,19 @@ public class CLDRLocaleProviderAdapter extends JRELocaleProviderAdapter {
|
|||
|
||||
@Override
|
||||
public Locale[] getAvailableLocales() {
|
||||
Set<String> all = createLanguageTagSet("AvailableLocales");
|
||||
Locale[] locs = new Locale[all.size()];
|
||||
int index = 0;
|
||||
for (String tag : all) {
|
||||
locs[index++] = Locale.forLanguageTag(tag);
|
||||
if (AVAILABLE_LOCALES == null) {
|
||||
AVAILABLE_LOCALES = createLanguageTagSet("AvailableLocales").stream()
|
||||
.map(Locale::forLanguageTag)
|
||||
.toArray(Locale[]::new);
|
||||
}
|
||||
return locs;
|
||||
return AVAILABLE_LOCALES;
|
||||
}
|
||||
|
||||
private static Locale applyAliases(Locale loc) {
|
||||
if (langAliasesMap.isEmpty()) {
|
||||
langAliasesMap = baseMetaInfo.getLanguageAliasMap();
|
||||
}
|
||||
Locale locale = langAliasesCache.get(loc);
|
||||
if (locale == null) {
|
||||
String locTag = loc.toLanguageTag();
|
||||
Locale aliasLocale = langAliasesMap.containsKey(locTag)
|
||||
? Locale.forLanguageTag(langAliasesMap.get(locTag)) : loc;
|
||||
langAliasesCache.putIfAbsent(loc, aliasLocale);
|
||||
return aliasLocale;
|
||||
} else {
|
||||
return locale;
|
||||
}
|
||||
return langAliasesCache.computeIfAbsent(loc, l -> {
|
||||
var alias = baseMetaInfo.getLanguageAliasMap().get(l.toLanguageTag());
|
||||
return alias != null ? Locale.forLanguageTag(alias) : l;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -220,15 +208,9 @@ public class CLDRLocaleProviderAdapter extends JRELocaleProviderAdapter {
|
|||
supportedLocaleString = nonBaseTags;
|
||||
}
|
||||
}
|
||||
if (supportedLocaleString == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
StringTokenizer tokens = new StringTokenizer(supportedLocaleString);
|
||||
Set<String> tagset = new HashSet<>((tokens.countTokens() * 4 + 2) / 3);
|
||||
while (tokens.hasMoreTokens()) {
|
||||
tagset.add(tokens.nextToken());
|
||||
}
|
||||
return tagset;
|
||||
return supportedLocaleString != null ?
|
||||
Set.of(supportedLocaleString.split("\s+")) :
|
||||
Collections.emptySet();
|
||||
}
|
||||
|
||||
// Implementation of ResourceBundleBasedAdapter
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
package sun.util.locale.provider;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.AccessControlException;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.security.PrivilegedActionException;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
|
@ -36,15 +35,13 @@ import java.text.spi.DateFormatProvider;
|
|||
import java.text.spi.DateFormatSymbolsProvider;
|
||||
import java.text.spi.DecimalFormatSymbolsProvider;
|
||||
import java.text.spi.NumberFormatProvider;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.ServiceConfigurationError;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.spi.CalendarDataProvider;
|
||||
|
@ -437,7 +434,7 @@ public class JRELocaleProviderAdapter extends LocaleProviderAdapter implements R
|
|||
*/
|
||||
@Override
|
||||
public Locale[] getAvailableLocales() {
|
||||
return AvailableJRELocales.localeList.clone();
|
||||
return AvailableJRELocales.localeList;
|
||||
}
|
||||
|
||||
public Set<String> getLanguageTagSet(String category) {
|
||||
|
@ -454,16 +451,9 @@ public class JRELocaleProviderAdapter extends LocaleProviderAdapter implements R
|
|||
|
||||
protected Set<String> createLanguageTagSet(String category) {
|
||||
String supportedLocaleString = createSupportedLocaleString(category);
|
||||
if (supportedLocaleString == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
StringTokenizer tokens = new StringTokenizer(supportedLocaleString);
|
||||
Set<String> tagset = new HashSet<>((tokens.countTokens() * 4 + 2) / 3);
|
||||
while (tokens.hasMoreTokens()) {
|
||||
tagset.add(tokens.nextToken());
|
||||
}
|
||||
|
||||
return tagset;
|
||||
return supportedLocaleString != null ?
|
||||
Set.of(supportedLocaleString.split("\s+")) :
|
||||
Collections.emptySet();
|
||||
}
|
||||
|
||||
private static String createSupportedLocaleString(String category) {
|
||||
|
@ -520,28 +510,17 @@ public class JRELocaleProviderAdapter extends LocaleProviderAdapter implements R
|
|||
throw new InternalError("No available locales for JRE");
|
||||
}
|
||||
|
||||
StringTokenizer localeStringTokenizer = new StringTokenizer(supportedLocaleString);
|
||||
|
||||
int length = localeStringTokenizer.countTokens();
|
||||
Locale[] locales = new Locale[length + 1];
|
||||
locales[0] = Locale.ROOT;
|
||||
for (int i = 1; i <= length; i++) {
|
||||
String currentToken = localeStringTokenizer.nextToken();
|
||||
switch (currentToken) {
|
||||
case "ja-JP-JP":
|
||||
locales[i] = JRELocaleConstants.JA_JP_JP;
|
||||
break;
|
||||
case "no-NO-NY":
|
||||
locales[i] = JRELocaleConstants.NO_NO_NY;
|
||||
break;
|
||||
case "th-TH-TH":
|
||||
locales[i] = JRELocaleConstants.TH_TH_TH;
|
||||
break;
|
||||
default:
|
||||
locales[i] = Locale.forLanguageTag(currentToken);
|
||||
}
|
||||
}
|
||||
return locales;
|
||||
return Arrays.stream(supportedLocaleString.split("\s+"))
|
||||
.map(t -> {
|
||||
return switch (t) {
|
||||
case "ja-JP-JP" -> JRELocaleConstants.JA_JP_JP;
|
||||
case "no-NO-NY" -> JRELocaleConstants.NO_NO_NY;
|
||||
case "th-TH-TH" -> JRELocaleConstants.TH_TH_TH;
|
||||
default -> Locale.forLanguageTag(t);
|
||||
};
|
||||
})
|
||||
.distinct()
|
||||
.toArray(Locale[]::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -33,6 +33,7 @@ import java.text.spi.DateFormatSymbolsProvider;
|
|||
import java.text.spi.DecimalFormatSymbolsProvider;
|
||||
import java.text.spi.NumberFormatProvider;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
@ -308,23 +309,16 @@ public abstract class LocaleProviderAdapter {
|
|||
}
|
||||
|
||||
public static Locale[] toLocaleArray(Set<String> tags) {
|
||||
Locale[] locs = new Locale[tags.size() + 1];
|
||||
int index = 0;
|
||||
locs[index++] = Locale.ROOT;
|
||||
for (String tag : tags) {
|
||||
switch (tag) {
|
||||
case "ja-JP-JP":
|
||||
locs[index++] = JRELocaleConstants.JA_JP_JP;
|
||||
break;
|
||||
case "th-TH-TH":
|
||||
locs[index++] = JRELocaleConstants.TH_TH_TH;
|
||||
break;
|
||||
default:
|
||||
locs[index++] = Locale.forLanguageTag(tag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return locs;
|
||||
return tags.stream()
|
||||
.map(t -> {
|
||||
return switch (t) {
|
||||
case "ja-JP-JP" -> JRELocaleConstants.JA_JP_JP;
|
||||
case "no-NO-NY" -> JRELocaleConstants.NO_NO_NY;
|
||||
case "th-TH-TH" -> JRELocaleConstants.TH_TH_TH;
|
||||
default -> Locale.forLanguageTag(t);
|
||||
};
|
||||
})
|
||||
.toArray(Locale[]::new);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue