1818 **/
1919package lucee .commons .i18n ;
2020
21- import java .lang .ref .SoftReference ;
2221import java .text .DateFormat ;
2322import java .text .ParsePosition ;
2423import java .text .SimpleDateFormat ;
4039import java .util .Locale ;
4140import java .util .Map ;
4241import java .util .TimeZone ;
43- import java .util .concurrent .ConcurrentHashMap ;
4442import java .util .concurrent .CopyOnWriteArrayList ;
4543
44+ import lucee .commons .collection .RefMap ;
45+ import lucee .commons .collection .RefMap .ReferenceType ;
4646import lucee .commons .date .DateTimeException ;
4747import lucee .commons .date .DateTimeUtil ;
4848import lucee .commons .io .SystemUtil ;
@@ -77,7 +77,7 @@ public final class FormatUtil {
7777 DEFAULT_MILLISECOND = DEFAULT_TIME .isSupported (ChronoField .MILLI_OF_SECOND ) ? DEFAULT_TIME .get (ChronoField .MILLI_OF_SECOND ) : 0 ;
7878 }
7979
80- private final static Map <String , SoftReference < List <FormatterWrapper >>> cfmlFormats = new ConcurrentHashMap <>();
80+ private final static Map <String , List <FormatterWrapper >> cfmlFormats = new RefMap <>(ReferenceType . SOFT );
8181 // "EEEE, MMMM d, yyyy, h:mm:ss a 'Coordinated Universal Time'"
8282 private final static Pattern [] strCfmlFormats = new Pattern [] {
8383
@@ -149,18 +149,17 @@ public final class FormatUtil {
149149
150150 };
151151
152- private static final Map <String , SoftReference < FormatterWrapper >> dateTimeFormatter = new ConcurrentHashMap <>();
152+ private static final Map <String , FormatterWrapper > dateTimeFormatter = new RefMap <>(ReferenceType . SOFT );
153153 public static final boolean debug = false ;
154154 private static final char NON_BREAKING_SPACE = '\u202F' ;
155155
156156 public static List <FormatterWrapper > getAllFormats (Locale locale , TimeZone timeZone , boolean lenient ) {
157157 String key = "all:" + locale .toString () + "-" + timeZone .getID () + ":" + lenient ;
158- SoftReference <List <FormatterWrapper >> sr = cfmlFormats .get (key );
159- List <FormatterWrapper > formatter = null ;
160- if (sr == null || (formatter = sr .get ()) == null ) {
158+ List <FormatterWrapper > formatter = cfmlFormats .get (key );
159+ if (formatter == null ) {
161160 synchronized (SystemUtil .createToken ("all" , key )) {
162- sr = cfmlFormats .get (key );
163- if (sr == null || ( formatter = sr . get ()) == null ) {
161+ formatter = cfmlFormats .get (key );
162+ if (formatter == null ) {
164163
165164 formatter = new CopyOnWriteArrayList <>();
166165 for (FormatterWrapper dtf : getCFMLFormats (locale , timeZone , lenient )) {
@@ -176,7 +175,7 @@ public static List<FormatterWrapper> getAllFormats(Locale locale, TimeZone timeZ
176175 formatter .add (dtf );
177176 }
178177
179- cfmlFormats .put (key , new SoftReference ( formatter ) );
178+ cfmlFormats .put (key , formatter );
180179 }
181180 }
182181 }
@@ -186,19 +185,18 @@ public static List<FormatterWrapper> getAllFormats(Locale locale, TimeZone timeZ
186185 public static List <FormatterWrapper > getCFMLFormats (Locale locale , TimeZone timeZone , boolean lenient ) {
187186 String key = "cfml:" + locale .toString () + "-" + timeZone .getID () + ":" + lenient ;
188187
189- SoftReference <List <FormatterWrapper >> sr = cfmlFormats .get (key );
190- List <FormatterWrapper > formatter = null ;
191- if (sr == null || (formatter = sr .get ()) == null ) {
188+ List <FormatterWrapper > formatter = cfmlFormats .get (key );
189+ if (formatter == null ) {
192190 synchronized (SystemUtil .createToken ("cfml" , key )) {
193- sr = cfmlFormats .get (key );
194- if (sr == null || ( formatter = sr . get ()) == null ) {
191+ formatter = cfmlFormats .get (key );
192+ if (formatter == null ) {
195193 ZoneId zone = timeZone .toZoneId ();
196194 formatter = new ArrayList <>();
197195 DateTimeFormatterBuilder builder ;
198196 for (Pattern p : strCfmlFormats ) {
199197 formatter .add (getFormatterWrapper (p .pattern , zone , locale , p .type , lenient ));
200198 }
201- cfmlFormats .put (key , new SoftReference <>( formatter ) );
199+ cfmlFormats .put (key , formatter );
202200 }
203201 }
204202 }
@@ -208,11 +206,10 @@ public static List<FormatterWrapper> getCFMLFormats(Locale locale, TimeZone time
208206 public static List <FormatterWrapper > getDateTimeFormats (Locale locale , TimeZone tz , boolean lenient ) {
209207
210208 String key = "dt-" + locale .toString () + "-" + tz .getID () + "-" + lenient ;
211- SoftReference <List <FormatterWrapper >> tmp = cfmlFormats .get (key );
212- List <FormatterWrapper > df = tmp == null ? null : tmp .get ();
209+ List <FormatterWrapper > df = cfmlFormats .get (key );
213210 if (df == null ) {
214211 synchronized (SystemUtil .createToken ("dt" , key )) {
215- df = tmp == null ? null : tmp .get ();
212+ df = cfmlFormats .get (key );
216213 if (df == null ) {
217214 ZoneId zone = tz .toZoneId ();
218215 df = new ArrayList <>();
@@ -263,7 +260,7 @@ public static List<FormatterWrapper> getDateTimeFormats(Locale locale, TimeZone
263260
264261 extractLegacyDateTimePatterns (df , locale , tz , lenient );
265262
266- cfmlFormats .put (key , new SoftReference < List < FormatterWrapper >>( df ) );
263+ cfmlFormats .put (key , df );
267264 }
268265 }
269266 }
@@ -307,11 +304,10 @@ public static FormatterWrapper fromFormatToFormatter(DateFormat format, short ty
307304
308305 public static List <FormatterWrapper > getDateFormats (Locale locale , TimeZone tz , boolean lenient ) {
309306 String key = "d-" + locale .toString () + "-" + tz .getID () + "-" + lenient ;
310- SoftReference <List <FormatterWrapper >> tmp = cfmlFormats .get (key );
311- List <FormatterWrapper > df = tmp == null ? null : tmp .get ();
307+ List <FormatterWrapper > df = cfmlFormats .get (key );
312308 if (df == null ) {
313309 synchronized (SystemUtil .createToken ("dt" , key )) {
314- df = tmp == null ? null : tmp .get ();
310+ df = cfmlFormats .get (key );
315311 if (df == null ) {
316312 ZoneId zone = tz .toZoneId ();
317313 df = new ArrayList <>();
@@ -323,7 +319,7 @@ public static List<FormatterWrapper> getDateFormats(Locale locale, TimeZone tz,
323319
324320 extractLegacyDatePatterns (df , locale , tz , lenient );
325321
326- cfmlFormats .put (key , new SoftReference < List < FormatterWrapper >>( df ) );
322+ cfmlFormats .put (key , df );
327323 }
328324 }
329325 }
@@ -455,11 +451,10 @@ private static boolean check(List<DateFormat> results, String orgPattern, Locale
455451 public static List <FormatterWrapper > getTimeFormats (Locale locale , TimeZone tz , boolean lenient ) {
456452
457453 String key = "t-" + locale .toString () + "-" + tz .getID () + "-" + lenient ;
458- SoftReference <List <FormatterWrapper >> tmp = cfmlFormats .get (key );
459- List <FormatterWrapper > df = tmp == null ? null : tmp .get ();
454+ List <FormatterWrapper > df = cfmlFormats .get (key );
460455 if (df == null ) {
461456 synchronized (SystemUtil .createToken ("dt" , key )) {
462- df = tmp == null ? null : tmp .get ();
457+ df = cfmlFormats .get (key );
463458 if (df == null ) {
464459 ZoneId zone = tz .toZoneId ();
465460 df = new ArrayList <>();
@@ -470,7 +465,7 @@ public static List<FormatterWrapper> getTimeFormats(Locale locale, TimeZone tz,
470465
471466 extractLegacyTimePatterns (df , locale , tz , lenient );
472467
473- cfmlFormats .put (key , new SoftReference < List < FormatterWrapper >>( df ) );
468+ cfmlFormats .put (key , df );
474469 }
475470 }
476471 }
@@ -502,12 +497,10 @@ public static FormatterWrapper getDateTimeFormatter(Locale locale, String mask,
502497
503498 public static FormatterWrapper getDateTimeFormatter (Locale locale , String mask , ZoneId zone ) {
504499 String key = locale + ":" + mask ;
505- SoftReference <FormatterWrapper > ref = dateTimeFormatter .get (key );
506- FormatterWrapper fw = ref == null ? null : ref .get ();
500+ FormatterWrapper fw = dateTimeFormatter .get (key );
507501 if (fw == null ) {
508502 synchronized (SystemUtil .createToken ("getDateTimeFormatter" , key )) {
509- ref = dateTimeFormatter .get (key );
510- fw = ref == null ? null : ref .get ();
503+ fw = dateTimeFormatter .get (key );
511504 if (fw == null ) {
512505 // TODO cache
513506 DateTimeFormatter formatter ;
@@ -525,7 +518,7 @@ else if (mask.equalsIgnoreCase("isoms") || mask.equalsIgnoreCase("isoMillis") ||
525518 if (locale != null ) formatter = formatter .withLocale (locale );
526519
527520 fw = new FormatterWrapper (formatter , mask , FORMAT_TYPE_DATE_TIME , zone );
528- dateTimeFormatter .put (key , new SoftReference < FormatterWrapper >( fw ) );
521+ dateTimeFormatter .put (key , fw );
529522 }
530523 }
531524 }
0 commit comments