Skip to content

Commit a89879a

Browse files
committed
Clean caches between tests
1 parent c867eed commit a89879a

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
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
@@ -128,7 +128,14 @@ static String getPatternForStyle(final Integer dateStyle, final Integer timeStyl
128128
});
129129
}
130130

131-
private final ConcurrentMap<ArrayKey, F> cInstanceCache = new ConcurrentHashMap<>(7);
131+
private final ConcurrentMap<ArrayKey, F> instanceCache = new ConcurrentHashMap<>(7);
132+
133+
/**
134+
* Clears the cache.
135+
*/
136+
void clearInstance() {
137+
instanceCache.clear();
138+
}
132139

133140
/**
134141
* 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
225232
final TimeZone actualTimeZone = TimeZones.toTimeZone(timeZone);
226233
final Locale actualLocale = LocaleUtils.toLocale(locale);
227234
final ArrayKey key = new ArrayKey(pattern, actualTimeZone, actualLocale);
228-
return cInstanceCache.computeIfAbsent(key, k -> createInstance(pattern, actualTimeZone, actualLocale));
235+
return instanceCache.computeIfAbsent(key, k -> createInstance(pattern, actualTimeZone, actualLocale));
229236
}
230237

231238
/**

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,21 @@ public class FastDateFormat extends Format implements DateParser, DatePrinter {
101101
*/
102102
public static final int SHORT = DateFormat.SHORT;
103103

104-
private static final AbstractFormatCache<FastDateFormat> cache = new AbstractFormatCache<FastDateFormat>() {
104+
private static final AbstractFormatCache<FastDateFormat> CACHE = new AbstractFormatCache<FastDateFormat>() {
105105
@Override
106106
protected FastDateFormat createInstance(final String pattern, final TimeZone timeZone, final Locale locale) {
107107
return new FastDateFormat(pattern, timeZone, locale);
108108
}
109109
};
110110

111+
/**
112+
* Clears the cache.
113+
*/
114+
static void clear() {
115+
AbstractFormatCache.clear();
116+
CACHE.clearInstance();
117+
}
118+
111119
/**
112120
* Gets a date formatter instance using the specified style in the
113121
* default time zone and locale.
@@ -119,7 +127,7 @@ protected FastDateFormat createInstance(final String pattern, final TimeZone tim
119127
* @since 2.1
120128
*/
121129
public static FastDateFormat getDateInstance(final int style) {
122-
return cache.getDateInstance(style, null, null);
130+
return CACHE.getDateInstance(style, null, null);
123131
}
124132

125133
/**
@@ -134,7 +142,7 @@ public static FastDateFormat getDateInstance(final int style) {
134142
* @since 2.1
135143
*/
136144
public static FastDateFormat getDateInstance(final int style, final Locale locale) {
137-
return cache.getDateInstance(style, null, locale);
145+
return CACHE.getDateInstance(style, null, locale);
138146
}
139147

140148
/**
@@ -150,7 +158,7 @@ public static FastDateFormat getDateInstance(final int style, final Locale local
150158
* @since 2.1
151159
*/
152160
public static FastDateFormat getDateInstance(final int style, final TimeZone timeZone) {
153-
return cache.getDateInstance(style, timeZone, null);
161+
return CACHE.getDateInstance(style, timeZone, null);
154162
}
155163

156164
/**
@@ -166,7 +174,7 @@ public static FastDateFormat getDateInstance(final int style, final TimeZone tim
166174
* pattern defined
167175
*/
168176
public static FastDateFormat getDateInstance(final int style, final TimeZone timeZone, final Locale locale) {
169-
return cache.getDateInstance(style, timeZone, locale);
177+
return CACHE.getDateInstance(style, timeZone, locale);
170178
}
171179

172180
/**
@@ -181,7 +189,7 @@ public static FastDateFormat getDateInstance(final int style, final TimeZone tim
181189
* @since 2.1
182190
*/
183191
public static FastDateFormat getDateTimeInstance(final int dateStyle, final int timeStyle) {
184-
return cache.getDateTimeInstance(dateStyle, timeStyle, null, null);
192+
return CACHE.getDateTimeInstance(dateStyle, timeStyle, null, null);
185193
}
186194

187195
/**
@@ -197,7 +205,7 @@ public static FastDateFormat getDateTimeInstance(final int dateStyle, final int
197205
* @since 2.1
198206
*/
199207
public static FastDateFormat getDateTimeInstance(final int dateStyle, final int timeStyle, final Locale locale) {
200-
return cache.getDateTimeInstance(dateStyle, timeStyle, null, locale);
208+
return CACHE.getDateTimeInstance(dateStyle, timeStyle, null, locale);
201209
}
202210

203211
/**
@@ -232,7 +240,7 @@ public static FastDateFormat getDateTimeInstance(final int dateStyle, final int
232240
*/
233241
public static FastDateFormat getDateTimeInstance(
234242
final int dateStyle, final int timeStyle, final TimeZone timeZone, final Locale locale) {
235-
return cache.getDateTimeInstance(dateStyle, timeStyle, timeZone, locale);
243+
return CACHE.getDateTimeInstance(dateStyle, timeStyle, timeZone, locale);
236244
}
237245

238246
/**
@@ -242,7 +250,7 @@ public static FastDateFormat getDateTimeInstance(
242250
* @return a date/time formatter
243251
*/
244252
public static FastDateFormat getInstance() {
245-
return cache.getInstance();
253+
return CACHE.getInstance();
246254
}
247255

248256
/**
@@ -255,7 +263,7 @@ public static FastDateFormat getInstance() {
255263
* @throws IllegalArgumentException if pattern is invalid
256264
*/
257265
public static FastDateFormat getInstance(final String pattern) {
258-
return cache.getInstance(pattern, null, null);
266+
return CACHE.getInstance(pattern, null, null);
259267
}
260268

261269
/**
@@ -269,7 +277,7 @@ public static FastDateFormat getInstance(final String pattern) {
269277
* @throws IllegalArgumentException if pattern is invalid
270278
*/
271279
public static FastDateFormat getInstance(final String pattern, final Locale locale) {
272-
return cache.getInstance(pattern, null, locale);
280+
return CACHE.getInstance(pattern, null, locale);
273281
}
274282

275283
/**
@@ -284,7 +292,7 @@ public static FastDateFormat getInstance(final String pattern, final Locale loca
284292
* @throws IllegalArgumentException if pattern is invalid
285293
*/
286294
public static FastDateFormat getInstance(final String pattern, final TimeZone timeZone) {
287-
return cache.getInstance(pattern, timeZone, null);
295+
return CACHE.getInstance(pattern, timeZone, null);
288296
}
289297

290298
/**
@@ -301,7 +309,7 @@ public static FastDateFormat getInstance(final String pattern, final TimeZone ti
301309
* or {@code null}
302310
*/
303311
public static FastDateFormat getInstance(final String pattern, final TimeZone timeZone, final Locale locale) {
304-
return cache.getInstance(pattern, timeZone, locale);
312+
return CACHE.getInstance(pattern, timeZone, locale);
305313
}
306314

307315
/**
@@ -315,7 +323,7 @@ public static FastDateFormat getInstance(final String pattern, final TimeZone ti
315323
* @since 2.1
316324
*/
317325
public static FastDateFormat getTimeInstance(final int style) {
318-
return cache.getTimeInstance(style, null, null);
326+
return CACHE.getTimeInstance(style, null, null);
319327
}
320328

321329
/**
@@ -330,7 +338,7 @@ public static FastDateFormat getTimeInstance(final int style) {
330338
* @since 2.1
331339
*/
332340
public static FastDateFormat getTimeInstance(final int style, final Locale locale) {
333-
return cache.getTimeInstance(style, null, locale);
341+
return CACHE.getTimeInstance(style, null, locale);
334342
}
335343

336344
/**
@@ -346,7 +354,7 @@ public static FastDateFormat getTimeInstance(final int style, final Locale local
346354
* @since 2.1
347355
*/
348356
public static FastDateFormat getTimeInstance(final int style, final TimeZone timeZone) {
349-
return cache.getTimeInstance(style, timeZone, null);
357+
return CACHE.getTimeInstance(style, timeZone, null);
350358
}
351359

352360
/**
@@ -362,7 +370,7 @@ public static FastDateFormat getTimeInstance(final int style, final TimeZone tim
362370
* pattern defined
363371
*/
364372
public static FastDateFormat getTimeInstance(final int style, final TimeZone timeZone, final Locale locale) {
365-
return cache.getTimeInstance(style, timeZone, locale);
373+
return CACHE.getTimeInstance(style, timeZone, locale);
366374
}
367375

368376
/** Our fast printer. */

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@
5555
class FastDateParserTest extends AbstractLangTest {
5656

5757
private enum Expected1806 {
58-
India(INDIA, "+05", "+0530", "+05:30", true), Greenwich(TimeZones.GMT, "Z", "Z", "Z", false),
58+
59+
// @formatter:off
60+
India(INDIA, "+05", "+0530", "+05:30", true),
61+
Greenwich(TimeZones.GMT, "Z", "Z", "Z", false),
5962
NewYork(NEW_YORK, "-05", "-0500", "-05:00", false);
63+
// @formatter:on
6064

6165
final TimeZone zone;
62-
6366
final String one;
6467
final String two;
6568
final String three;
@@ -99,6 +102,7 @@ private enum Expected1806 {
99102
@AfterEach
100103
void clear() {
101104
AbstractFormatCache.clear();
105+
FastDateFormat.clear();
102106
FastDateParser.clear();
103107
FastDatePrinter.clear();
104108
}
@@ -147,18 +151,16 @@ private static Calendar initializeCalendar(final TimeZone timeZone) {
147151
return cal;
148152
}
149153

150-
private final TriFunction<String, TimeZone, Locale, DateParser> dateParserProvider = (format, timeZone,
151-
locale) -> new FastDateParser(format, timeZone, locale, null);
154+
private final TriFunction<String, TimeZone, Locale, DateParser> dateParserProvider = (format, timeZone, locale) -> new FastDateParser(format, timeZone,
155+
locale, null);
152156

153157
private DateParser getDateInstance(final int dateStyle, final Locale locale) {
154-
return getInstance(null, AbstractFormatCache.getPatternForStyle(Integer.valueOf(dateStyle), null, locale),
155-
TimeZone.getDefault(), Locale.getDefault());
158+
return getInstance(null, AbstractFormatCache.getPatternForStyle(Integer.valueOf(dateStyle), null, locale), TimeZone.getDefault(), Locale.getDefault());
156159
}
157160

158161
private Calendar getEraStart(int year, final TimeZone zone, final Locale locale) {
159162
final Calendar cal = Calendar.getInstance(zone, locale);
160163
cal.clear();
161-
162164
// https://docs.oracle.com/javase/8/docs/technotes/guides/intl/calendar.doc.html
163165
if (locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
164166
if (year < 1868) {
@@ -220,7 +222,6 @@ void test_Equality_Hash(final TriFunction<String, TimeZone, Locale, DateParser>
220222
for (final DateParser parser : parsers) {
221223
map.put(parser, Integer.valueOf(i++));
222224
}
223-
224225
i = 0;
225226
for (final DateParser parser : parsers) {
226227
assertEquals(i++, map.get(parser).intValue());

0 commit comments

Comments
 (0)