< prev index next >
src/java.base/share/classes/sun/util/locale/BaseLocale.java
Print this page
rev 54349 : 8221701: Archive constant BaseLocales
Reviewed-by: TBD
@@ -30,18 +30,26 @@
*******************************************************************************
*/
package sun.util.locale;
+import jdk.internal.misc.VM;
+import jdk.internal.vm.annotation.Stable;
+
import java.lang.ref.SoftReference;
+import java.util.Map;
import java.util.StringJoiner;
public final class BaseLocale {
- public static final String SEP = "_";
+ // Initialized by java.util.Locale
+ public static @Stable Map<String, Map<String, BaseLocale>> constantBaseLocales;
+ static {
+ VM.initializeFromArchive(BaseLocale.class);
+ }
- private static final Cache CACHE = new Cache();
+ public static final String SEP = "_";
private final String language;
private final String script;
private final String region;
private final String variant;
@@ -66,32 +74,54 @@
}
// Called for creating the Locale.* constants. No argument
// validation is performed.
public static BaseLocale createInstance(String language, String region) {
+ assert LocaleUtils.toLowerString(language).intern() == language
+ && LocaleUtils.toUpperString(region).intern() == region;
BaseLocale base = new BaseLocale(language, "", region, "", false);
- CACHE.put(new Key(base), base);
return base;
}
public static BaseLocale getInstance(String language, String script,
String region, String variant) {
- // JDK uses deprecated ISO639.1 language codes for he, yi and id
+
if (language != null) {
+ // JDK uses deprecated ISO639.1 language codes for he, yi and id
if (LocaleUtils.caseIgnoreMatch(language, "he")) {
language = "iw";
} else if (LocaleUtils.caseIgnoreMatch(language, "yi")) {
language = "ji";
} else if (LocaleUtils.caseIgnoreMatch(language, "id")) {
language = "in";
}
+ } else {
+ language = "";
+ }
+ if (script == null) {
+ script = "";
+ }
+ if (region == null) {
+ region = "";
+ }
+ if (variant == null) {
+ variant = "";
}
- Key key = new Key(language, script, region, variant, false);
- BaseLocale baseLocale = CACHE.get(key);
+ // Check for constant base locales first
+ if (script.isEmpty() && variant.isEmpty()) {
+ BaseLocale baseLocale = constantBaseLocales
+ .getOrDefault(LocaleUtils.toLowerString(language), Map.of())
+ .get(LocaleUtils.toUpperString(region));
+ if (baseLocale != null) {
return baseLocale;
}
+ }
+
+ Key key = new Key(language, script, region, variant, false);
+ return Cache.CACHE.get(key);
+ }
public String getLanguage() {
return language;
}
@@ -169,50 +199,12 @@
private final BaseLocale holder;
private final boolean normalized;
private final int hash;
- /**
- * Creates a Key. language and region must be normalized
- * (intern'ed in the proper case).
- */
- private Key(BaseLocale locale) {
- this.holder = locale;
- this.holderRef = null;
- this.normalized = true;
- String language = locale.getLanguage();
- String region = locale.getRegion();
- assert LocaleUtils.toLowerString(language).intern() == language
- && LocaleUtils.toUpperString(region).intern() == region
- && locale.getVariant() == ""
- && locale.getScript() == "";
-
- int h = language.hashCode();
- if (region != "") {
- int len = region.length();
- for (int i = 0; i < len; i++) {
- h = 31 * h + LocaleUtils.toLower(region.charAt(i));
- }
- }
- hash = h;
- }
-
private Key(String language, String script, String region,
String variant, boolean normalize) {
- if (language == null) {
- language = "";
- }
- if (script == null) {
- script = "";
- }
- if (region == null) {
- region = "";
- }
- if (variant == null) {
- variant = "";
- }
-
BaseLocale locale = new BaseLocale(language, script, region, variant, normalize);
this.normalized = normalize;
if (normalized) {
this.holderRef = new SoftReference<>(locale);
this.holder = null;
@@ -289,10 +281,12 @@
}
}
private static class Cache extends LocaleObjectCache<Key, BaseLocale> {
+ private static final Cache CACHE = new Cache();
+
public Cache() {
}
@Override
protected Key normalizeKey(Key key) {
< prev index next >