ICU-7149 Add initial version, @internal for now.

X-SVN-Rev: 31455
This commit is contained in:
Mark Davis 2012-02-21 00:35:51 +00:00
parent 9fdb34c0d3
commit 7bc558601a
5 changed files with 331 additions and 0 deletions

3
.gitattributes vendored
View File

@ -248,6 +248,8 @@ icu4j/main/classes/core/manifest.stub -text
icu4j/main/classes/core/src/com/ibm/icu/text/CompactDecimalFormat.java -text
icu4j/main/classes/core/src/com/ibm/icu/text/CompactDecimalFormatData.java -text
icu4j/main/classes/core/src/com/ibm/icu/util/GenderInfo.java -text
icu4j/main/classes/core/src/com/ibm/icu/util/ListFormat.java -text
icu4j/main/classes/core/src/com/ibm/icu/util/ListFormatData.java -text
icu4j/main/classes/currdata/.externalToolBuilders/copy-data-currdata.launch -text
icu4j/main/classes/currdata/.settings/org.eclipse.core.resources.prefs -text
icu4j/main/classes/currdata/.settings/org.eclipse.jdt.core.prefs -text
@ -679,6 +681,7 @@ icu4j/main/tests/core/src/com/ibm/icu/dev/test/serializable/data/ICU_4.8/com.ibm
icu4j/main/tests/core/src/com/ibm/icu/dev/test/serializable/data/ICU_4.8/com.ibm.icu.util.UResourceTypeMismatchException.dat -text
icu4j/main/tests/core/src/com/ibm/icu/dev/test/serializable/data/ICU_4.8/com.ibm.icu.util.VTimeZone.dat -text
icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/GenderInfoTest.java -text
icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ListFormatTest.java -text
icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/Trie2Test.setRanges1.16.tri2 -text
icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/Trie2Test.setRanges1.32.tri2 -text
icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/Trie2Test.setRanges2.16.tri2 -text

View File

@ -0,0 +1,157 @@
/*
*******************************************************************************
* Copyright (C) 2012-2012, Google, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.util;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import com.ibm.icu.text.Transform;
import com.ibm.icu.util.ULocale;
/**
* Immutable class for formatting a list, using data from CLDR (or supplied
* separately). The class is not subclassable.
*
* @author markdavis
* @internal
*/
final public class ListFormat implements Transform<Collection<String>, String> {
private final String two;
private final String start;
private final String middle;
private final String end;
/**
* 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.
* @param start
* string for the start of a list items, containing {0} for the
* first, and {1} for the rest.
* @param middle
* string for the start of a list items, containing {0} for the
* first part of the list, and {1} for the rest of the list.
* @param end
* string for the end of a list items, containing {0} for the
* first part of the list, and {1} for the last item.
* @internal
*/
public ListFormat(String two, String start, String middle, String end) {
this.two = two;
this.start = start;
this.middle = middle;
this.end = end;
}
/**
* Create a list formatter that is appropriate for a locale.
*
* @param locale
* the locale in question.
* @return ListFormatter
* @internal
*/
public static ListFormat getInstance(ULocale locale) {
// These can be cached, since they are read-only
// poor-man's locale lookup, for hardcoded data
while (true) {
ListFormat 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);
}
}
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
* @internal
*/
public static ListFormat getInstance(Locale locale) {
return getInstance(ULocale.forLocale(locale));
}
/**
* Format a list of objects.
*
* @param items
* items to format. The toString() method is called on each.
* @return items formatted into a string
* @internal
*/
public String format(Object... items) {
return format(Arrays.asList(items));
}
/**
* Format a collation 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
* @internal
*/
public String format(Collection<Object> items) {
// TODO optimize this for the common case that the patterns are all of the
// form {0}<sometext>{1}.
// We avoid MessageFormat, because there is no "sub" formatting.
Iterator<Object> it = items.iterator();
int count = items.size();
switch (count) {
case 0:
return "";
case 1:
return it.next().toString();
case 2:
return format2(two, it.next(), it.next());
}
String result = it.next().toString();
result = format2(start, result, it.next());
for (count -= 3; count > 0; --count) {
result = format2(middle, result, it.next());
}
return format2(end, result, it.next());
}
private String format2(String pattern, Object a, Object b) {
// TODO: make slightly faster by using single pass.
return pattern.replace("{0}", a.toString()).replace("{1}", b.toString());
}
/**
* @internal
*/
public String transform(Collection<String> source) {
return format(source);
}
/** JUST FOR DEVELOPMENT */
// For use with the hard-coded data
// TODO Replace by use of RB
// Verify in building that all of the patterns contain {0}, {1}.
static Map<ULocale, ListFormat> localeToData = new HashMap<ULocale, ListFormat>();
static void add(String locale, String...data) {
localeToData.put(new ULocale(locale), new ListFormat(data[0], data[1], data[2], data[3]));
}
static {
ListFormatData.load();
}
}

View File

@ -0,0 +1,84 @@
/*
*******************************************************************************
* Copyright (C) 2012-2012, Google, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.util;
public class ListFormatData {
static void load() {
ListFormat.add("af", "{0} en {1}", "{0}, {1}", "{0}, {1}", "{0} en {1}");
ListFormat.add("am", "{0} እና {1}", "{0}, {1}", "{0}, {1}", "{0}, እና {1}");
ListFormat.add("ar", "{0} و {1}", "{0}، {1}", "{0}، {1}", "{0}، و {1}");
ListFormat.add("bg", "{0} и {1}", "{0}, {1}", "{0}, {1}", "{0} и {1}");
ListFormat.add("bn", "{0} এবং {1}", "{0}, {1}", "{0}, {1}", "{0}, এবং {1}");
ListFormat.add("bs", "{0} i {1}", "{0}, {1}", "{0}, {1}", "{0} i {1}");
ListFormat.add("ca", "{0} i {1}", "{0}, {1}", "{0}, {1}", "{0} i {1}");
ListFormat.add("cs", "{0} a {1}", "{0}, {1}", "{0}, {1}", "{0} a {1}");
ListFormat.add("da", "{0} og {1}", "{0}, {1}", "{0}, {1}", "{0} og {1}");
ListFormat.add("de", "{0} und {1}", "{0}, {1}", "{0}, {1}", "{0} und {1}");
ListFormat.add("ee", "{0} kple {1}", "{0}, {1}", "{0}, {1}", "{0}, kple {1}");
ListFormat.add("el", "{0} και {1}", "{0}, {1}", "{0}, {1}", "{0} και {1}");
ListFormat.add("en", "{0} and {1}", "{0}, {1}", "{0}, {1}", "{0}, and {1}");
ListFormat.add("es", "{0} y {1}", "{0}, {1}", "{0}, {1}", "{0} y {1}");
ListFormat.add("et", "{0} ja {1}", "{0}, {1}", "{0}, {1}", "{0} ja {1}");
ListFormat.add("eu", "{0} eta {1}", "{0}, {1}", "{0}, {1}", "{0} eta {1}");
ListFormat.add("fa", "{0} و {1}", "{0}،‏ {1}", "{0}،‏ {1}", "{0}، و {1}");
ListFormat.add("fi", "{0} ja {1}", "{0}, {1}", "{0}, {1}", "{0} ja {1}");
ListFormat.add("fil", "{0} at {1}", "{0}, {1}", "{0}, {1}", "{0} at {1}");
ListFormat.add("fo", "{0} og {1}", "{0}, {1}", "{0}, {1}", "{0} og {1}");
ListFormat.add("fr", "{0} et {1}", "{0}, {1}", "{0}, {1}", "{0} et {1}");
ListFormat.add("fur", "{0} e {1}", "{0}, {1}", "{0}, {1}", "{0} e {1}");
ListFormat.add("gd", "{0} agus {1}", "{0}, {1}", "{0}, {1}", "{0}, agus {1}");
ListFormat.add("gl", "{0} e {1}", "{0}, {1}", "{0}, {1}", "{0} e {1}");
ListFormat.add("gsw", "{0} und {1}", "{0}, {1}", "{0}, {1}", "{0} und {1}");
ListFormat.add("gu", "{0} અને {1}", "{0}, {1}", "{0}, {1}", "{0} અને {1}");
ListFormat.add("he", "{0} ו-{1}", "{0}, {1}", "{0}, {1}", "{0} ו-{1}");
ListFormat.add("hi", "{0} और {1}", "{0}, {1}", "{0}, {1}", "{0}, और {1}");
ListFormat.add("hr", "{0} i {1}", "{0}, {1}", "{0}, {1}", "{0} i {1}");
ListFormat.add("hu", "{0} és {1}", "{0}, {1}", "{0}, {1}", "{0} és {1}");
ListFormat.add("id", "{0} dan {1}", "{0}, {1}", "{0}, {1}", "{0}, dan {1}");
ListFormat.add("is", "{0} og {1}", "{0}, {1}", "{0}, {1}", "{0} og {1}");
ListFormat.add("it", "{0} e {1}", "{0}, {1}", "{0}, {1}", "{0}, e {1}");
ListFormat.add("ja", "{0}、{1}", "{0}、{1}", "{0}、{1}", "{0}、{1}");
ListFormat.add("ka", "{0} და {1}", "{0}, {1}", "{0}, {1}", "{0} და {1}");
ListFormat.add("kea", "{0} y {1}", "{0}, {1}", "{0}, {1}", "{0} y {1}");
ListFormat.add("kl", "{0} aamma {1}", "{0} aamma {1}", "{0}, {1}", "{0}, {1}");
ListFormat.add("kn", "{0} ಮತ್ತು {1}", "{0}, {1}", "{0}, {1}", "{0}, ಮತ್ತು {1}");
ListFormat.add("ko", "{0} 및 {1}", "{0}, {1}", "{0}, {1}", "{0} 및 {1}");
ListFormat.add("ksh", "{0} un {1}", "{0}, {1}", "{0}, {1}", "{0} un {1}");
ListFormat.add("lt", "{0} ir {1}", "{0}, {1}", "{0}, {1}", "{0} ir {1}");
ListFormat.add("lv", "{0} un {1}", "{0}, {1}", "{0}, {1}", "{0} un {1}");
ListFormat.add("ml", "{0} കൂടാതെ {1}", "{0}, {1}", "{0}, {1}", "{0}, {1} എന്നിവ");
ListFormat.add("mr", "{0} आणि {1}", "{0}, {1}", "{0}, {1}", "{0} आणि {1}");
ListFormat.add("ms", "{0} dan {1}", "{0}, {1}", "{0}, {1}", "{0}, dan {1}");
ListFormat.add("nb", "{0} og {1}", "{0}, {1}", "{0}, {1}", "{0} og {1}");
ListFormat.add("nl", "{0} en {1}", "{0}, {1}", "{0}, {1}", "{0} en {1}");
ListFormat.add("nn", "{0} og {1}", "{0}, {1}", "{0}, {1}", "{0} og {1}");
ListFormat.add("pl", "{0} i {1}", "{0}; {1}", "{0}; {1}", "{0} i {1}");
ListFormat.add("pt", "{0} e {1}", "{0}, {1}", "{0}, {1}", "{0} e {1}");
ListFormat.add("ro", "{0} şi {1}", "{0}, {1}", "{0}, {1}", "{0} şi {1}");
ListFormat.add("", "{0}, {1}", "{0}, {1}", "{0}, {1}", "{0}, {1}"); // root
ListFormat.add("ru", "{0} и {1}", "{0}, {1}", "{0}, {1}", "{0} и {1}");
ListFormat.add("se", "{0} ja {1}", "{0}, {1}", "{0}, {1}", "{0} ja {1}");
ListFormat.add("sk", "{0} a {1}", "{0}, {1}", "{0}, {1}", "{0} a {1}");
ListFormat.add("sl", "{0} in {1}", "{0}, {1}", "{0}, {1}", "{0} in {1}");
ListFormat.add("sr", "{0} и {1}", "{0}, {1}", "{0}, {1}", "{0} и {1}");
ListFormat.add("sr_Cyrl", "{0} и {1}", "{0}, {1}", "{0}, {1}", "{0} и {1}");
ListFormat.add("sr_Latn", "{0} i {1}", "{0}, {1}", "{0}, {1}", "{0} i {1}");
ListFormat.add("sv", "{0} och {1}", "{0}, {1}", "{0}, {1}", "{0} och {1}");
ListFormat.add("sw", "{0} na {1}", "{0}, {1}", "{0}, {1}", "{0}, na {1}");
ListFormat.add("ta", "{0} மற்றும் {1}", "{0}, {1}", "{0}, {1}", "{0} மற்றும் {1}");
ListFormat.add("te", "{0} మరియు {1}", "{0}, {1}", "{0}, {1}", "{0} మరియు {1}");
ListFormat.add("th", "{0}และ{1}", "{0} {1}", "{0} {1}", "{0} และ{1}");
ListFormat.add("tr", "{0} ve {1}", "{0}, {1}", "{0}, {1}", "{0} ve {1}");
ListFormat.add("uk", "{0} та {1}", "{0}, {1}", "{0}, {1}", "{0} та {1}");
ListFormat.add("ur", "{0} اور {1}", "{0}، {1}", "{0}، {1}", "{0}، اور {1}");
ListFormat.add("vi", "{0} và {1}", "{0}, {1}", "{0}, {1}", "{0} và {1}");
ListFormat.add("wae", "{0} und {1}", "{0}, {1}", "{0}, {1}", "{0} und {1}");
ListFormat.add("zh", "{0}和{1}", "{0}、{1}", "{0}、{1}", "{0}和{1}");
ListFormat.add("zh", "{0}和{1}", "{0}、{1}", "{0}、{1}", "{0}和{1}");
ListFormat.add("zu", "I-{0} ne-{1}", "{0}, {1}", "{0}, {1}", "{0}, no-{1}");
}
}

View File

@ -0,0 +1,86 @@
/*
*******************************************************************************
* Copyright (C) 2012-2012, Google, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.dev.test.util;
import java.util.Arrays;
import java.util.Collection;
import com.ibm.icu.dev.test.TestFmwk;
import com.ibm.icu.text.Transform;
import com.ibm.icu.util.ListFormat;
import com.ibm.icu.util.ULocale;
public class ListFormatTest extends TestFmwk {
public static void main(String[] args) {
new ListFormatTest().run(args);
}
String[] HardcodedTestData = {
"",
"A",
"A and B",
"A; B, and C",
"A; B, C, and D",
"A; B, C, D, and E"
};
public void TestBasic() {
ListFormat formatter = new ListFormat("{0} and {1}", "{0}; {1}", "{0}, {1}", "{0}, and {1}");
checkData(formatter, HardcodedTestData);
}
String[] EnglishTestData = {
"",
"A",
"A and B",
"A, B, and C",
"A, B, C, and D",
"A, B, C, D, and E"
};
public void TestEnglish() {
checkData(ListFormat.getInstance(ULocale.ENGLISH), EnglishTestData);
checkData(ListFormat.getInstance(ULocale.US), EnglishTestData);
}
String[] JapaneseTestData = {
"",
"A",
"A、B",
"A、B、C",
"A、B、C、D",
"A、B、C、D、E"
};
public void TestJapanese() {
checkData(ListFormat.getInstance(ULocale.JAPANESE), JapaneseTestData);
}
String[] RootTestData = {
"",
"A",
"A, B",
"A, B, C",
"A, B, C, D",
"A, B, C, D, E"
};
public void TestSpecial() {
checkData(ListFormat.getInstance(ULocale.ROOT), RootTestData);
checkData(ListFormat.getInstance(new ULocale("xxx")), RootTestData);
}
public void checkData(ListFormat listFormat, String[] strings) {
assertEquals("0", strings[0], listFormat.format());
assertEquals("1", strings[1], listFormat.format("A"));
assertEquals("2", strings[2], listFormat.format("A", "B"));
assertEquals("3", strings[3], listFormat.format("A", "B", "C"));
assertEquals("4", strings[4], listFormat.format("A", "B", "C", "D"));
assertEquals("5", strings[5], listFormat.format("A", "B", "C", "D", "E"));
}
}

View File

@ -31,6 +31,7 @@ public class TestAll extends TestGroup {
"Trie2Test",
"LocaleDataTest",
"GenderInfoTest",
"ListFormatTest",
"ULocaleTest",
"LocaleAliasTest",
"DebugUtilitiesTest",