Skip to content

Commit ef5de64

Browse files
committed
Clean caches between tests
1 parent d8f45fd commit ef5de64

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

src/main/java/org/apache/commons/lang3/time/AbstractFormatCache.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@ public int hashCode() {
8989
*/
9090
static final int NONE = -1;
9191

92-
private static final ConcurrentMap<ArrayKey, String> cDateTimeInstanceCache = new ConcurrentHashMap<>(7);
92+
private static final ConcurrentMap<ArrayKey, String> dateTimeInstanceCache = new ConcurrentHashMap<>(7);
93+
94+
/**
95+
* Clears the cache.
96+
*/
97+
static void clear() {
98+
dateTimeInstanceCache.clear();
99+
}
93100

94101
/**
95102
* Gets a date/time format for the specified styles and locale.
@@ -104,7 +111,7 @@ public int hashCode() {
104111
static String getPatternForStyle(final Integer dateStyle, final Integer timeStyle, final Locale locale) {
105112
final Locale safeLocale = LocaleUtils.toLocale(locale);
106113
final ArrayKey key = new ArrayKey(dateStyle, timeStyle, safeLocale);
107-
return cDateTimeInstanceCache.computeIfAbsent(key, k -> {
114+
return dateTimeInstanceCache.computeIfAbsent(key, k -> {
108115
try {
109116
final DateFormat formatter;
110117
if (dateStyle == null) {

src/main/java/org/apache/commons/lang3/time/FastDateParser.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.concurrent.ConcurrentMap;
4343
import java.util.regex.Matcher;
4444
import java.util.regex.Pattern;
45+
import java.util.stream.Stream;
4546

4647
import org.apache.commons.lang3.ArraySorter;
4748
import org.apache.commons.lang3.LocaleUtils;
@@ -630,7 +631,7 @@ public String toString() {
630631
// helper classes to parse the format string
631632

632633
@SuppressWarnings("unchecked") // OK because we are creating an array with no entries
633-
private static final ConcurrentMap<Locale, Strategy>[] caches = new ConcurrentMap[Calendar.FIELD_COUNT];
634+
private static final ConcurrentMap<Locale, Strategy>[] CACHES = new ConcurrentMap[Calendar.FIELD_COUNT];
634635

635636
private static final Strategy ABBREVIATED_YEAR_STRATEGY = new NumberStrategy(Calendar.YEAR) {
636637
/**
@@ -717,18 +718,25 @@ private static Map<String, Integer> appendDisplayNames(final Calendar calendar,
717718
return values;
718719
}
719720

721+
/**
722+
* Clears the cache.
723+
*/
724+
static void clear() {
725+
Stream.of(CACHES).filter(Objects::nonNull).forEach(ConcurrentMap::clear);
726+
}
727+
720728
/**
721729
* Gets a cache of Strategies for a particular field
722730
*
723731
* @param field The Calendar field
724732
* @return a cache of Locale to Strategy
725733
*/
726734
private static ConcurrentMap<Locale, Strategy> getCache(final int field) {
727-
synchronized (caches) {
728-
if (caches[field] == null) {
729-
caches[field] = new ConcurrentHashMap<>(3);
735+
synchronized (CACHES) {
736+
if (CACHES[field] == null) {
737+
CACHES[field] = new ConcurrentHashMap<>(3);
730738
}
731-
return caches[field];
739+
return CACHES[field];
732740
}
733741
}
734742

@@ -786,7 +794,6 @@ private static StringBuilder simpleQuote(final StringBuilder sb, final String va
786794

787795
/** Initialized from Calendar. */
788796
private transient List<StrategyAndWidth> patterns;
789-
790797
/**
791798
* Constructs a new FastDateParser.
792799
*

src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,7 @@ public int estimateLength() {
899899

900900
private static final int MAX_DIGITS = 10; // log10(Integer.MAX_VALUE) ~= 9.3
901901

902-
private static final ConcurrentMap<TimeZoneDisplayKey, String> cTimeZoneDisplayCache =
903-
new ConcurrentHashMap<>(7);
902+
private static final ConcurrentMap<TimeZoneDisplayKey, String> timeZoneDisplayCache = new ConcurrentHashMap<>(7);
904903

905904
/**
906905
* Appends two digits to the given buffer.
@@ -991,6 +990,10 @@ private static void appendFullDigits(final Appendable buffer, int value, int min
991990
}
992991
}
993992

993+
static void clear() {
994+
timeZoneDisplayCache.clear();
995+
}
996+
994997
/**
995998
* Gets the time zone display name, using a cache for performance.
996999
*
@@ -1003,7 +1006,7 @@ private static void appendFullDigits(final Appendable buffer, int value, int min
10031006
static String getTimeZoneDisplay(final TimeZone tz, final boolean daylight, final int style, final Locale locale) {
10041007
final TimeZoneDisplayKey key = new TimeZoneDisplayKey(tz, daylight, style, locale);
10051008
// This is a very slow call, so cache the results.
1006-
return cTimeZoneDisplayCache.computeIfAbsent(key, k -> tz.getDisplayName(daylight, style, locale));
1009+
return timeZoneDisplayCache.computeIfAbsent(key, k -> tz.getDisplayName(daylight, style, locale));
10071010
}
10081011

10091012
/**

src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import org.apache.commons.lang3.SerializationUtils;
4343
import org.apache.commons.lang3.SystemUtils;
4444
import org.apache.commons.lang3.function.TriFunction;
45+
import org.junit.jupiter.api.AfterEach;
46+
import org.junit.jupiter.api.BeforeEach;
4547
import org.junit.jupiter.api.Test;
4648
import org.junit.jupiter.params.ParameterizedTest;
4749
import org.junit.jupiter.params.provider.Arguments;
@@ -50,7 +52,7 @@
5052
/**
5153
* Tests {@link org.apache.commons.lang3.time.FastDateParser}.
5254
*/
53-
public class FastDateParserTest extends AbstractLangTest {
55+
class FastDateParserTest extends AbstractLangTest {
5456

5557
private enum Expected1806 {
5658
India(INDIA, "+05", "+0530", "+05:30", true), Greenwich(TimeZones.GMT, "Z", "Z", "Z", false),
@@ -93,6 +95,14 @@ private enum Expected1806 {
9395

9496
private static final Locale SWEDEN = new Locale("sv", "SE");
9597

98+
@BeforeEach
99+
@AfterEach
100+
void clear() {
101+
AbstractFormatCache.clear();
102+
FastDateParser.clear();
103+
FastDatePrinter.clear();
104+
}
105+
96106
static void checkParse(final Locale locale, final Calendar cal, final SimpleDateFormat simpleDateFormat,
97107
final DateParser dateParser) {
98108
final String formattedDate = simpleDateFormat.format(cal.getTime());

0 commit comments

Comments
 (0)