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 <[email protected]> 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; + }); } /**
