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 a89879a67d1371b86a7f1aa5f4210c49402289bc Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Mon Jun 9 08:32:16 2025 -0400 Clean caches between tests --- .../commons/lang3/time/AbstractFormatCache.java | 11 ++++-- .../apache/commons/lang3/time/FastDateFormat.java | 42 +++++++++++++--------- .../commons/lang3/time/FastDateParserTest.java | 17 ++++----- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/time/AbstractFormatCache.java b/src/main/java/org/apache/commons/lang3/time/AbstractFormatCache.java index 684f48b28..3d29a1167 100644 --- a/src/main/java/org/apache/commons/lang3/time/AbstractFormatCache.java +++ b/src/main/java/org/apache/commons/lang3/time/AbstractFormatCache.java @@ -128,7 +128,14 @@ static String getPatternForStyle(final Integer dateStyle, final Integer timeStyl }); } - private final ConcurrentMap<ArrayKey, F> cInstanceCache = new ConcurrentHashMap<>(7); + private final ConcurrentMap<ArrayKey, F> instanceCache = new ConcurrentHashMap<>(7); + + /** + * Clears the cache. + */ + void clearInstance() { + instanceCache.clear(); + } /** * Create a format instance using the specified pattern, time zone @@ -225,7 +232,7 @@ public F getInstance(final String pattern, final TimeZone timeZone, final Locale 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 -> createInstance(pattern, actualTimeZone, actualLocale)); + return instanceCache.computeIfAbsent(key, k -> createInstance(pattern, actualTimeZone, actualLocale)); } /** diff --git a/src/main/java/org/apache/commons/lang3/time/FastDateFormat.java b/src/main/java/org/apache/commons/lang3/time/FastDateFormat.java index 3955a2419..9b3df28b3 100644 --- a/src/main/java/org/apache/commons/lang3/time/FastDateFormat.java +++ b/src/main/java/org/apache/commons/lang3/time/FastDateFormat.java @@ -101,13 +101,21 @@ public class FastDateFormat extends Format implements DateParser, DatePrinter { */ public static final int SHORT = DateFormat.SHORT; - private static final AbstractFormatCache<FastDateFormat> cache = new AbstractFormatCache<FastDateFormat>() { + private static final AbstractFormatCache<FastDateFormat> CACHE = new AbstractFormatCache<FastDateFormat>() { @Override protected FastDateFormat createInstance(final String pattern, final TimeZone timeZone, final Locale locale) { return new FastDateFormat(pattern, timeZone, locale); } }; + /** + * Clears the cache. + */ + static void clear() { + AbstractFormatCache.clear(); + CACHE.clearInstance(); + } + /** * Gets a date formatter instance using the specified style in the * default time zone and locale. @@ -119,7 +127,7 @@ protected FastDateFormat createInstance(final String pattern, final TimeZone tim * @since 2.1 */ public static FastDateFormat getDateInstance(final int style) { - return cache.getDateInstance(style, null, null); + return CACHE.getDateInstance(style, null, null); } /** @@ -134,7 +142,7 @@ public static FastDateFormat getDateInstance(final int style) { * @since 2.1 */ public static FastDateFormat getDateInstance(final int style, final Locale locale) { - return cache.getDateInstance(style, null, locale); + return CACHE.getDateInstance(style, null, locale); } /** @@ -150,7 +158,7 @@ public static FastDateFormat getDateInstance(final int style, final Locale local * @since 2.1 */ public static FastDateFormat getDateInstance(final int style, final TimeZone timeZone) { - return cache.getDateInstance(style, timeZone, null); + return CACHE.getDateInstance(style, timeZone, null); } /** @@ -166,7 +174,7 @@ public static FastDateFormat getDateInstance(final int style, final TimeZone tim * pattern defined */ public static FastDateFormat getDateInstance(final int style, final TimeZone timeZone, final Locale locale) { - return cache.getDateInstance(style, timeZone, locale); + return CACHE.getDateInstance(style, timeZone, locale); } /** @@ -181,7 +189,7 @@ public static FastDateFormat getDateInstance(final int style, final TimeZone tim * @since 2.1 */ public static FastDateFormat getDateTimeInstance(final int dateStyle, final int timeStyle) { - return cache.getDateTimeInstance(dateStyle, timeStyle, null, null); + return CACHE.getDateTimeInstance(dateStyle, timeStyle, null, null); } /** @@ -197,7 +205,7 @@ public static FastDateFormat getDateTimeInstance(final int dateStyle, final int * @since 2.1 */ public static FastDateFormat getDateTimeInstance(final int dateStyle, final int timeStyle, final Locale locale) { - return cache.getDateTimeInstance(dateStyle, timeStyle, null, locale); + return CACHE.getDateTimeInstance(dateStyle, timeStyle, null, locale); } /** @@ -232,7 +240,7 @@ public static FastDateFormat getDateTimeInstance(final int dateStyle, final int */ public static FastDateFormat getDateTimeInstance( final int dateStyle, final int timeStyle, final TimeZone timeZone, final Locale locale) { - return cache.getDateTimeInstance(dateStyle, timeStyle, timeZone, locale); + return CACHE.getDateTimeInstance(dateStyle, timeStyle, timeZone, locale); } /** @@ -242,7 +250,7 @@ public static FastDateFormat getDateTimeInstance( * @return a date/time formatter */ public static FastDateFormat getInstance() { - return cache.getInstance(); + return CACHE.getInstance(); } /** @@ -255,7 +263,7 @@ public static FastDateFormat getInstance() { * @throws IllegalArgumentException if pattern is invalid */ public static FastDateFormat getInstance(final String pattern) { - return cache.getInstance(pattern, null, null); + return CACHE.getInstance(pattern, null, null); } /** @@ -269,7 +277,7 @@ public static FastDateFormat getInstance(final String pattern) { * @throws IllegalArgumentException if pattern is invalid */ public static FastDateFormat getInstance(final String pattern, final Locale locale) { - return cache.getInstance(pattern, null, locale); + return CACHE.getInstance(pattern, null, locale); } /** @@ -284,7 +292,7 @@ public static FastDateFormat getInstance(final String pattern, final Locale loca * @throws IllegalArgumentException if pattern is invalid */ public static FastDateFormat getInstance(final String pattern, final TimeZone timeZone) { - return cache.getInstance(pattern, timeZone, null); + return CACHE.getInstance(pattern, timeZone, null); } /** @@ -301,7 +309,7 @@ public static FastDateFormat getInstance(final String pattern, final TimeZone ti * or {@code null} */ public static FastDateFormat getInstance(final String pattern, final TimeZone timeZone, final Locale locale) { - return cache.getInstance(pattern, timeZone, locale); + return CACHE.getInstance(pattern, timeZone, locale); } /** @@ -315,7 +323,7 @@ public static FastDateFormat getInstance(final String pattern, final TimeZone ti * @since 2.1 */ public static FastDateFormat getTimeInstance(final int style) { - return cache.getTimeInstance(style, null, null); + return CACHE.getTimeInstance(style, null, null); } /** @@ -330,7 +338,7 @@ public static FastDateFormat getTimeInstance(final int style) { * @since 2.1 */ public static FastDateFormat getTimeInstance(final int style, final Locale locale) { - return cache.getTimeInstance(style, null, locale); + return CACHE.getTimeInstance(style, null, locale); } /** @@ -346,7 +354,7 @@ public static FastDateFormat getTimeInstance(final int style, final Locale local * @since 2.1 */ public static FastDateFormat getTimeInstance(final int style, final TimeZone timeZone) { - return cache.getTimeInstance(style, timeZone, null); + return CACHE.getTimeInstance(style, timeZone, null); } /** @@ -362,7 +370,7 @@ public static FastDateFormat getTimeInstance(final int style, final TimeZone tim * pattern defined */ public static FastDateFormat getTimeInstance(final int style, final TimeZone timeZone, final Locale locale) { - return cache.getTimeInstance(style, timeZone, locale); + return CACHE.getTimeInstance(style, timeZone, locale); } /** Our fast printer. */ diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java index c260e60ca..1098939f7 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java @@ -55,11 +55,14 @@ class FastDateParserTest extends AbstractLangTest { private enum Expected1806 { - India(INDIA, "+05", "+0530", "+05:30", true), Greenwich(TimeZones.GMT, "Z", "Z", "Z", false), + + // @formatter:off + India(INDIA, "+05", "+0530", "+05:30", true), + Greenwich(TimeZones.GMT, "Z", "Z", "Z", false), NewYork(NEW_YORK, "-05", "-0500", "-05:00", false); + // @formatter:on final TimeZone zone; - final String one; final String two; final String three; @@ -99,6 +102,7 @@ private enum Expected1806 { @AfterEach void clear() { AbstractFormatCache.clear(); + FastDateFormat.clear(); FastDateParser.clear(); FastDatePrinter.clear(); } @@ -147,18 +151,16 @@ private static Calendar initializeCalendar(final TimeZone timeZone) { return cal; } - private final TriFunction<String, TimeZone, Locale, DateParser> dateParserProvider = (format, timeZone, - locale) -> new FastDateParser(format, timeZone, locale, null); + private final TriFunction<String, TimeZone, Locale, DateParser> dateParserProvider = (format, timeZone, locale) -> new FastDateParser(format, timeZone, + locale, null); private DateParser getDateInstance(final int dateStyle, final Locale locale) { - return getInstance(null, AbstractFormatCache.getPatternForStyle(Integer.valueOf(dateStyle), null, locale), - TimeZone.getDefault(), Locale.getDefault()); + return getInstance(null, AbstractFormatCache.getPatternForStyle(Integer.valueOf(dateStyle), null, locale), TimeZone.getDefault(), Locale.getDefault()); } private Calendar getEraStart(int year, final TimeZone zone, final Locale locale) { final Calendar cal = Calendar.getInstance(zone, locale); cal.clear(); - // https://docs.oracle.com/javase/8/docs/technotes/guides/intl/calendar.doc.html if (locale.equals(FastDateParser.JAPANESE_IMPERIAL)) { if (year < 1868) { @@ -220,7 +222,6 @@ void test_Equality_Hash(final TriFunction<String, TimeZone, Locale, DateParser> for (final DateParser parser : parsers) { map.put(parser, Integer.valueOf(i++)); } - i = 0; for (final DateParser parser : parsers) { assertEquals(i++, map.get(parser).intValue());