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 89bcc5f80ebf0a6c85843f5fdb5d94a3a803267b
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Mar 20 14:06:23 2022 -0400

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

diff --git a/src/main/java/org/apache/commons/lang3/LocaleUtils.java 
b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
index 69f5eae..a568a2b 100644
--- a/src/main/java/org/apache/commons/lang3/LocaleUtils.java
+++ b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
@@ -100,22 +100,16 @@ public class LocaleUtils {
         if (languageCode == null) {
             return Collections.emptyList();
         }
-        List<Locale> countries = cCountriesByLanguage.get(languageCode);
-        if (countries == null) {
-            countries = new ArrayList<>();
+        return cCountriesByLanguage.computeIfAbsent(languageCode, lc -> {
+            List<Locale> countries = new ArrayList<>();
             final List<Locale> locales = availableLocaleList();
             for (final Locale locale : locales) {
-                if (languageCode.equals(locale.getLanguage()) &&
-                        !locale.getCountry().isEmpty() &&
-                    locale.getVariant().isEmpty()) {
+                if (languageCode.equals(locale.getLanguage()) && 
!locale.getCountry().isEmpty() && locale.getVariant().isEmpty()) {
                     countries.add(locale);
                 }
             }
-            countries = Collections.unmodifiableList(countries);
-            cCountriesByLanguage.putIfAbsent(languageCode, countries);
-            countries = cCountriesByLanguage.get(languageCode);
-        }
-        return countries;
+            return Collections.unmodifiableList(countries);
+        });
     }
 
     /**

Reply via email to