diff --git a/src/java.base/share/classes/java/util/Locale.java b/src/java.base/share/classes/java/util/Locale.java index ae6c4af5ec5..73132b81c09 100644 --- a/src/java.base/share/classes/java/util/Locale.java +++ b/src/java.base/share/classes/java/util/Locale.java @@ -988,17 +988,23 @@ public final class Locale implements Cloneable, Serializable { if (locale != null) { return locale; } - return LOCALE_CACHE.computeIfAbsent(baseloc, LOCALE_CREATOR); + return LocaleCache.cache(baseloc); } else { LocaleKey key = new LocaleKey(baseloc, extensions); - return LOCALE_CACHE.computeIfAbsent(key, LOCALE_CREATOR); + return LocaleCache.cache(key); } } - private static final ReferencedKeyMap LOCALE_CACHE - = ReferencedKeyMap.create(true, ReferencedKeyMap.concurrentHashMapSupplier()); + private static final class LocaleCache implements Function { + private static final ReferencedKeyMap LOCALE_CACHE + = ReferencedKeyMap.create(true, ReferencedKeyMap.concurrentHashMapSupplier()); + + private static final Function LOCALE_CREATOR = new LocaleCache(); + + public static Locale cache(Object key) { + return LOCALE_CACHE.computeIfAbsent(key, LOCALE_CREATOR); + } - private static final Function LOCALE_CREATOR = new Function<>() { @Override public Locale apply(Object key) { if (key instanceof BaseLocale base) { @@ -1007,7 +1013,7 @@ public final class Locale implements Cloneable, Serializable { LocaleKey lk = (LocaleKey)key; return new Locale(lk.base, lk.exts); } - }; + } private static final class LocaleKey { diff --git a/src/java.base/share/classes/sun/util/locale/BaseLocale.java b/src/java.base/share/classes/sun/util/locale/BaseLocale.java index ec2a6a49183..a744b6ce39f 100644 --- a/src/java.base/share/classes/sun/util/locale/BaseLocale.java +++ b/src/java.base/share/classes/sun/util/locale/BaseLocale.java @@ -38,7 +38,6 @@ import jdk.internal.util.StaticProperty; import jdk.internal.vm.annotation.Stable; import java.util.StringJoiner; -import java.util.function.UnaryOperator; public final class BaseLocale { @@ -91,10 +90,6 @@ public final class BaseLocale { } } - // Interned BaseLocale cache - private static final ReferencedKeySet CACHE = - ReferencedKeySet.create(true, ReferencedKeySet.concurrentHashMapSupplier()); - public static final String SEP = "_"; private final String language; @@ -163,21 +158,16 @@ public final class BaseLocale { // Obtain the "interned" BaseLocale from the cache. The returned // "interned" instance can subsequently be used by the Locale // instance which guarantees the locale components are properly cased/interned. - return CACHE.intern(new BaseLocale(language, script, region, variant), - // Avoid lambdas since this may be on the bootstrap path in many locales - INTERNER); - } - - public static final UnaryOperator INTERNER = new UnaryOperator<>() { - @Override - public BaseLocale apply(BaseLocale b) { - return new BaseLocale( - LocaleUtils.toLowerString(b.language).intern(), - LocaleUtils.toTitleString(b.script).intern(), - LocaleUtils.toUpperString(b.region).intern(), - b.variant.intern()); + class InterningCache { // TODO: StableValue + private static final ReferencedKeySet CACHE = + ReferencedKeySet.create(true, ReferencedKeySet.concurrentHashMapSupplier()); } - }; + return InterningCache.CACHE.intern(new BaseLocale( + language.intern(), // guaranteed to be lower-case + LocaleUtils.toTitleString(script).intern(), + region.intern(), // guaranteed to be upper-case + variant.intern())); + } public static String convertOldISOCodes(String language) { return switch (language) {