From: Markus Scherer Date: Fri, 10 Jun 2016 22:51:24 +0000 (+0000) Subject: ICU-7434 switch DateFormatSymbols from SimpleCache to SoftCache X-Git-Tag: milestone-59-0-1~389 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ba843c9aa96c37f9030ec0a9241c441ef3001c0c;p=icu ICU-7434 switch DateFormatSymbols from SimpleCache to SoftCache X-SVN-Rev: 38827 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DateFormatSymbols.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DateFormatSymbols.java index 9b9b075457f..a6ebced745a 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DateFormatSymbols.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DateFormatSymbols.java @@ -13,9 +13,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; -import java.util.ListIterator; import java.util.Locale; import java.util.Map; import java.util.MissingResourceException; @@ -23,11 +21,11 @@ import java.util.ResourceBundle; import java.util.Set; import java.util.TreeMap; +import com.ibm.icu.impl.CacheBase; import com.ibm.icu.impl.CalendarUtil; -import com.ibm.icu.impl.ICUCache; import com.ibm.icu.impl.ICUData; import com.ibm.icu.impl.ICUResourceBundle; -import com.ibm.icu.impl.SimpleCache; +import com.ibm.icu.impl.SoftCache; import com.ibm.icu.impl.UResource; import com.ibm.icu.impl.Utility; import com.ibm.icu.text.TimeZoneNames.NameType; @@ -1495,8 +1493,23 @@ public class DateFormatSymbols implements Serializable, Cloneable { static final int millisPerHour = 60*60*1000; // DateFormatSymbols cache - private static ICUCache DFSCACHE = - new SimpleCache(); + private static CacheBase DFSCACHE = + new SoftCache() { + @Override + protected DateFormatSymbols createInstance(String key, ULocale locale) { + // Extract the type string from the key. + // Otherwise we would have to create a pair object that + // carries both the locale and the type. + int typeStart = key.indexOf('+') + 1; + int typeLimit = key.indexOf('+', typeStart); + if (typeLimit < 0) { + // no numbers keyword value + typeLimit = key.length(); + } + String type = key.substring(typeStart, typeLimit); + return new DateFormatSymbols(locale, null, type); + } + }; /** * Initializes format symbols for the locale and calendar type @@ -1508,23 +1521,13 @@ public class DateFormatSymbols implements Serializable, Cloneable { // We may need to deescalate this API to @internal. protected void initializeData(ULocale desiredLocale, String type) { - String key = desiredLocale.getBaseName() + "+" + type; + String key = desiredLocale.getBaseName() + '+' + type; String ns = desiredLocale.getKeywordValue("numbers"); if (ns != null && ns.length() > 0) { - key += "+" + ns; - } - DateFormatSymbols dfs = DFSCACHE.get(key); - if (dfs == null) { - // Initialize data from scratch put a clone of this instance into the cache - initializeData(desiredLocale, null, type); - // Do not cache subclass instances - if (this.getClass().getName().equals("com.ibm.icu.text.DateFormatSymbols")) { - dfs = (DateFormatSymbols)this.clone(); - DFSCACHE.put(key, dfs); - } - } else { - initializeData(dfs); + key += '+' + ns; } + DateFormatSymbols dfs = DFSCACHE.getInstance(key, desiredLocale); + initializeData(dfs); } /** @@ -1824,6 +1827,10 @@ public class DateFormatSymbols implements Serializable, Cloneable { } } + /** Private, for cache.getInstance(). */ + private DateFormatSymbols(ULocale desiredLocale, ICUResourceBundle b, String calendarType) { + initializeData(desiredLocale, b, calendarType); + } /** * Initializes format symbols for the locale and calendar type