This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit 30c9be6f6d302fe82348a27fb18c1e4b4fc0fe23
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Mar 20 14:27:28 2022 -0400

    Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().
---
 src/main/java/org/apache/commons/lang3/LocaleUtils.java | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/LocaleUtils.java 
b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
index a568a2b..eadb15e 100644
--- a/src/main/java/org/apache/commons/lang3/LocaleUtils.java
+++ b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
@@ -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);
+        });
     }
 
     /**

Reply via email to