Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().

This commit is contained in:
Gary Gregory 2022-03-20 14:27:28 -04:00
parent f518bb6c6f
commit 30c9be6f6d
1 changed files with 5 additions and 10 deletions

View File

@ -165,21 +165,16 @@ public class LocaleUtils {
if (countryCode == null) {
return Collections.emptyList();
}
List<Locale> langs = cLanguagesByCountry.get(countryCode);
if (langs == null) {
langs = new ArrayList<>();
return cLanguagesByCountry.computeIfAbsent(countryCode, k -> {
final List<Locale> locales = availableLocaleList();
List<Locale> langs = new ArrayList<>();
for (final Locale locale : locales) {
if (countryCode.equals(locale.getCountry()) &&
locale.getVariant().isEmpty()) {
if (countryCode.equals(locale.getCountry()) && locale.getVariant().isEmpty()) {
langs.add(locale);
}
}
langs = Collections.unmodifiableList(langs);
cLanguagesByCountry.putIfAbsent(countryCode, langs);
langs = cLanguagesByCountry.get(countryCode);
}
return langs;
return Collections.unmodifiableList(langs);
});
}
/**