From 8131a32512ea3d742125993278bd7b630ab81b32 Mon Sep 17 00:00:00 2001 From: Travis Keep Date: Mon, 27 Aug 2012 19:51:13 +0000 Subject: [PATCH] ICU-9506 re-integrate changes to ListFormatter from icu4j/branches/tkeep/cdf X-SVN-Rev: 32246 --- .../src/com/ibm/icu/text/ListFormatter.java | 57 ++++++++++++------- .../dev/test/format/ListFormatterTest.java | 23 +++++--- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java b/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java index a91a6a3b8a6..559171bd09e 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java @@ -13,12 +13,16 @@ import java.util.Iterator; import java.util.Locale; import java.util.Map; +import com.ibm.icu.impl.ICUCache; +import com.ibm.icu.impl.ICUResourceBundle; +import com.ibm.icu.impl.SimpleCache; import com.ibm.icu.util.ULocale; +import com.ibm.icu.util.UResourceBundle; /** * Immutable class for formatting a list, using data from CLDR (or supplied * separately). The class is not subclassable. - * + * * @author Mark Davis * @draft ICU 50 * @provisional This API might change or be removed in a future release. @@ -32,7 +36,7 @@ final public class ListFormatter { /** * Internal: Create a ListFormatter from component strings, * with definitions as in LDML. - * + * * @param two * string for two items, containing {0} for the first, and {1} * for the second. @@ -56,7 +60,7 @@ final public class ListFormatter { /** * Create a list formatter that is appropriate for a locale. - * + * * @param locale * the locale in question. * @return ListFormatter @@ -64,22 +68,14 @@ final public class ListFormatter { * @provisional This API might change or be removed in a future release. */ public static ListFormatter getInstance(ULocale locale) { - // These can be cached, since they are read-only - // poor-man's locale lookup, for hardcoded data - while (true) { - ListFormatter data = localeToData.get(locale); - if (data != null) { - return data; - } - locale = locale.equals(zhTW) ? ULocale.TRADITIONAL_CHINESE : locale.getFallback(); - if (locale == null) return localeToData.get(ULocale.ROOT); - } + return cache.get(locale); } + private static ULocale zhTW = new ULocale("zh_TW"); /** * Create a list formatter that is appropriate for a locale. - * + * * @param locale * the locale in question. * @return ListFormatter @@ -92,7 +88,7 @@ final public class ListFormatter { /** * Create a list formatter that is appropriate for the default FORMAT locale. - * + * * @return ListFormatter * @draft ICU 50 * @provisional This API might change or be removed in a future release. @@ -103,7 +99,7 @@ final public class ListFormatter { /** * Format a list of objects. - * + * * @param items * items to format. The toString() method is called on each. * @return items formatted into a string @@ -116,7 +112,7 @@ final public class ListFormatter { /** * Format a collection of objects. The toString() method is called on each. - * + * * @param items * items to format. The toString() method is called on each. * @return items formatted into a string @@ -165,7 +161,30 @@ final public class ListFormatter { static void add(String locale, String...data) { localeToData.put(new ULocale(locale), new ListFormatter(data[0], data[1], data[2], data[3])); } - static { - ListFormatterData.load(); + + private static class Cache { + private final ICUCache cache = + new SimpleCache(); + + public ListFormatter get(ULocale locale) { + ListFormatter result = cache.get(locale); + if (result == null) { + result = load(locale); + cache.put(locale, result); + } + return result; + } + + private static ListFormatter load(ULocale ulocale) { + ICUResourceBundle r = (ICUResourceBundle)UResourceBundle. + getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, ulocale); + return new ListFormatter( + r.getWithFallback("listPattern/standard/2").getString(), + r.getWithFallback("listPattern/standard/start").getString(), + r.getWithFallback("listPattern/standard/middle").getString(), + r.getWithFallback("listPattern/standard/end").getString()); + } } + + static Cache cache = new Cache(); } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ListFormatterTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ListFormatterTest.java index eaf77ffeef3..1c84cb1d3cd 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ListFormatterTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ListFormatterTest.java @@ -16,7 +16,7 @@ public class ListFormatterTest extends TestFmwk { public static void main(String[] args) { new ListFormatterTest().run(args); } - + String[] HardcodedTestData = { "", "A", @@ -30,7 +30,7 @@ public class ListFormatterTest extends TestFmwk { ListFormatter formatter = new ListFormatter("{0} and {1}", "{0}; {1}", "{0}, {1}", "{0}, and {1}"); checkData(formatter, HardcodedTestData); } - + String[] EnglishTestData = { "", "A", @@ -39,18 +39,17 @@ public class ListFormatterTest extends TestFmwk { "A, B, C, and D", "A, B, C, D, and E" }; - + public void TestEnglish() { checkData(ListFormatter.getInstance(ULocale.ENGLISH), EnglishTestData); checkData(ListFormatter.getInstance(ULocale.US), EnglishTestData); // Redundant tests for code coverage. checkData(ListFormatter.getInstance(Locale.ENGLISH), EnglishTestData); - ULocale defaultLocale = ULocale.getDefault(ULocale.Category.FORMAT); - if (defaultLocale.equals(ULocale.ENGLISH) || defaultLocale.equals(ULocale.US)) { + if (isDefaultLocaleEnglishLike()) { checkData(ListFormatter.getInstance(), EnglishTestData); } } - + String[] JapaneseTestData = { "", "A", @@ -63,7 +62,7 @@ public class ListFormatterTest extends TestFmwk { public void TestJapanese() { checkData(ListFormatter.getInstance(ULocale.JAPANESE), JapaneseTestData); } - + String[] RootTestData = { "", "A", @@ -75,10 +74,11 @@ public class ListFormatterTest extends TestFmwk { public void TestSpecial() { checkData(ListFormatter.getInstance(ULocale.ROOT), RootTestData); - checkData(ListFormatter.getInstance(new ULocale("xxx")), RootTestData); + if (isDefaultLocaleEnglishLike()) { + checkData(ListFormatter.getInstance(new ULocale("xxx")), EnglishTestData); + } } - public void checkData(ListFormatter listFormat, String[] strings) { assertEquals("0", strings[0], listFormat.format()); assertEquals("1", strings[1], listFormat.format("A")); @@ -87,4 +87,9 @@ public class ListFormatterTest extends TestFmwk { assertEquals("4", strings[4], listFormat.format("A", "B", "C", "D")); assertEquals("5", strings[5], listFormat.format("A", "B", "C", "D", "E")); } + + private boolean isDefaultLocaleEnglishLike() { + ULocale defaultLocale = ULocale.getDefault(ULocale.Category.FORMAT); + return defaultLocale.equals(ULocale.ENGLISH) || defaultLocale.equals(ULocale.US); + } }