From 9dd85849635461835d629e3301b7808d4dfe35d6 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 20 Mar 2022 14:27:10 -0400 Subject: [PATCH] Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent(). --- .../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 697ad746c..c836eda42 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 cTimeZoneDisplayCache = new ConcurrentHashMap<>(7); + /** *

Gets the time zone display name, using a cache for performance.

* @@ -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)); } /**