You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/03/20 18:27:55 UTC

[commons-lang] branch master updated (cb6676b -> 3952780)

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

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


    from cb6676b  Use Java 8 API ConcurrentMap#computeIfAbsent().
     new 89bcc5f  Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().
     new 5648bc4  Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().
     new 9dd8584  Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().
     new f518bb6  Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().
     new 30c9be6  Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().
     new 3952780  Adds TimeZones.toTimeZone().

The 6 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../java/org/apache/commons/lang3/LocaleUtils.java | 31 ++++++-----------
 .../apache/commons/lang3/time/FastDateParser.java  | 13 ++-----
 .../apache/commons/lang3/time/FastDatePrinter.java | 13 ++-----
 .../org/apache/commons/lang3/time/FormatCache.java | 40 ++++++----------------
 .../org/apache/commons/lang3/time/TimeZones.java   | 14 +++++++-
 5 files changed, 39 insertions(+), 72 deletions(-)

[commons-lang] 01/06: Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().

Posted by gg...@apache.org.
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 <ga...@gmail.com>
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);
+        });
     }
 
     /**

[commons-lang] 06/06: Adds TimeZones.toTimeZone().

Posted by gg...@apache.org.
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 3952780fd797b344e5752077fc61c0e0907ec158
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Mar 20 14:27:50 2022 -0400

    Adds TimeZones.toTimeZone().
---
 src/main/java/org/apache/commons/lang3/time/TimeZones.java | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/lang3/time/TimeZones.java b/src/main/java/org/apache/commons/lang3/time/TimeZones.java
index 4f7a009..770e1cc 100644
--- a/src/main/java/org/apache/commons/lang3/time/TimeZones.java
+++ b/src/main/java/org/apache/commons/lang3/time/TimeZones.java
@@ -26,7 +26,7 @@ import java.util.TimeZone;
  */
 public class TimeZones {
 
-    // Do not instantiate.
+    /** Do not instantiate. */
     private TimeZones() {
     }
 
@@ -41,4 +41,16 @@ public class TimeZones {
      * @since 3.13.0
      */
     public static final TimeZone GMT = TimeZone.getTimeZone(GMT_ID);
+
+    /**
+     * Returns the given TimeZone if non-{@code null}, otherwise {@link TimeZone#getDefault()}.
+     *
+     * @param timeZone a locale or {@code null}.
+     * @return the given locale if non-{@code null}, otherwise {@link TimeZone#getDefault()}.
+     * @since 3.13.0
+     */
+    public static TimeZone toTimeZone(final TimeZone timeZone) {
+        return timeZone != null ? timeZone : TimeZone.getDefault();
+    }
+
 }

[commons-lang] 03/06: Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().

Posted by gg...@apache.org.
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 9dd85849635461835d629e3301b7808d4dfe35d6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Mar 20 14:27:10 2022 -0400

    Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().
---
 .../java/org/apache/commons/lang3/time/FastDatePrinter.java | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java b/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
index 697ad74..c836eda 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
@@ -1299,6 +1299,7 @@ public class FastDatePrinter implements DatePrinter, Serializable {
 
     private static final ConcurrentMap<TimeZoneDisplayKey, String> cTimeZoneDisplayCache =
         new ConcurrentHashMap<>(7);
+
     /**
      * <p>Gets the time zone display name, using a cache for performance.</p>
      *
@@ -1310,16 +1311,8 @@ public class FastDatePrinter implements DatePrinter, Serializable {
      */
     static String getTimeZoneDisplay(final TimeZone tz, final boolean daylight, final int style, final Locale locale) {
         final TimeZoneDisplayKey key = new TimeZoneDisplayKey(tz, daylight, style, locale);
-        String value = cTimeZoneDisplayCache.get(key);
-        if (value == null) {
-            // This is a very slow call, so cache the results.
-            value = tz.getDisplayName(daylight, style, locale);
-            final String prior = cTimeZoneDisplayCache.putIfAbsent(key, value);
-            if (prior != null) {
-                value= prior;
-            }
-        }
-        return value;
+        // This is a very slow call, so cache the results.
+        return cTimeZoneDisplayCache.computeIfAbsent(key, k -> tz.getDisplayName(daylight, style, locale));
     }
 
     /**

[commons-lang] 04/06: Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().

Posted by gg...@apache.org.
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 f518bb6c6f5a6b87ad7fe04205b8ba611f711ff4
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Mar 20 14:27:15 2022 -0400

    Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().
---
 .../org/apache/commons/lang3/time/FormatCache.java | 40 ++++++----------------
 1 file changed, 10 insertions(+), 30 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/time/FormatCache.java b/src/main/java/org/apache/commons/lang3/time/FormatCache.java
index 4a568cd..265ef18 100644
--- a/src/main/java/org/apache/commons/lang3/time/FormatCache.java
+++ b/src/main/java/org/apache/commons/lang3/time/FormatCache.java
@@ -67,24 +67,14 @@ abstract class FormatCache<F extends Format> {
      * @throws NullPointerException if pattern is {@code null}
      * @throws IllegalArgumentException if pattern is invalid
      */
-    public F getInstance(final String pattern, TimeZone timeZone, Locale locale) {
+    public F getInstance(final String pattern, final TimeZone timeZone, final Locale locale) {
         Validate.notNull(pattern, "pattern");
-        if (timeZone == null) {
-            timeZone = TimeZone.getDefault();
-        }
-        locale = LocaleUtils.toLocale(locale);
-        final ArrayKey key = new ArrayKey(pattern, timeZone, locale);
-        F format = cInstanceCache.get(key);
-        if (format == null) {
-            format = createInstance(pattern, timeZone, locale);
-            final F previousValue = cInstanceCache.putIfAbsent(key, format);
-            if (previousValue != null) {
-                // another thread snuck in and did the same work
-                // we should return the instance that is in ConcurrentMap
-                format = previousValue;
-            }
-        }
-        return format;
+        final TimeZone actualTimeZone = TimeZones.toTimeZone(timeZone);
+        final Locale actualLocale = LocaleUtils.toLocale(locale);
+        final ArrayKey key = new ArrayKey(pattern, actualTimeZone, actualLocale);
+        return cInstanceCache.computeIfAbsent(key, k -> {
+            return createInstance(pattern, actualTimeZone, actualLocale);
+        });
     }
 
     /**
@@ -185,9 +175,7 @@ abstract class FormatCache<F extends Format> {
     static String getPatternForStyle(final Integer dateStyle, final Integer timeStyle, final Locale locale) {
         final Locale safeLocale = LocaleUtils.toLocale(locale);
         final ArrayKey key = new ArrayKey(dateStyle, timeStyle, safeLocale);
-
-        String pattern = cDateTimeInstanceCache.get(key);
-        if (pattern == null) {
+        return cDateTimeInstanceCache.computeIfAbsent(key, k -> {
             try {
                 final DateFormat formatter;
                 if (dateStyle == null) {
@@ -197,19 +185,11 @@ abstract class FormatCache<F extends Format> {
                 } else {
                     formatter = DateFormat.getDateTimeInstance(dateStyle.intValue(), timeStyle.intValue(), safeLocale);
                 }
-                pattern = ((SimpleDateFormat) formatter).toPattern();
-                final String previous = cDateTimeInstanceCache.putIfAbsent(key, pattern);
-                if (previous != null) {
-                    // even though it doesn't matter if another thread put the pattern
-                    // it's still good practice to return the String instance that is
-                    // actually in the ConcurrentMap
-                    pattern = previous;
-                }
+                return ((SimpleDateFormat) formatter).toPattern();
             } catch (final ClassCastException ex) {
                 throw new IllegalArgumentException("No date time pattern for locale: " + safeLocale);
             }
-        }
-        return pattern;
+        });
     }
 
     /**

[commons-lang] 05/06: Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().

Posted by gg...@apache.org.
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 <ga...@gmail.com>
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);
+        });
     }
 
     /**

[commons-lang] 02/06: Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().

Posted by gg...@apache.org.
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 5648bc42e2f618bd230e815eb36964d2fd62d303
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Mar 20 14:06:39 2022 -0400

    Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().
---
 .../java/org/apache/commons/lang3/time/FastDateParser.java  | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
index 049040b..fea3347 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
@@ -653,16 +653,9 @@ public class FastDateParser implements DateParser, Serializable {
      */
     private Strategy getLocaleSpecificStrategy(final int field, final Calendar definingCalendar) {
         final ConcurrentMap<Locale, Strategy> cache = getCache(field);
-        Strategy strategy = cache.get(locale);
-        if (strategy == null) {
-            strategy = field == Calendar.ZONE_OFFSET ? new TimeZoneStrategy(locale)
-                : new CaseInsensitiveTextStrategy(field, definingCalendar, locale);
-            final Strategy inCache = cache.putIfAbsent(locale, strategy);
-            if (inCache != null) {
-                return inCache;
-            }
-        }
-        return strategy;
+        return cache.computeIfAbsent(locale, k -> {
+            return field == Calendar.ZONE_OFFSET ? new TimeZoneStrategy(locale) : new CaseInsensitiveTextStrategy(field, definingCalendar, locale);
+        });
     }
 
     /**