ICU-9615 Change GenderInfo.java to use CLDR data

X-SVN-Rev: 32466
This commit is contained in:
Travis Keep 2012-09-28 19:01:28 +00:00
parent 946d320413
commit 9620ed5954

View File

@ -11,6 +11,11 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.MissingResourceException;
import com.ibm.icu.impl.ICUCache;
import com.ibm.icu.impl.ICUResourceBundle;
import com.ibm.icu.impl.SimpleCache;
/** /**
* Provide information about gender in locales based on data in CLDR. Currently supplies gender of lists. * Provide information about gender in locales based on data in CLDR. Currently supplies gender of lists.
@ -33,18 +38,7 @@ public class GenderInfo {
* @internal * @internal
*/ */
public static GenderInfo getInstance(ULocale uLocale) { public static GenderInfo getInstance(ULocale uLocale) {
// These can be cached, since they are read-only return genderInfoCache.get(uLocale);
// poor-man's locale lookup, for hardcoded data
while (true) {
GenderInfo data = localeToListGender.get(uLocale);
if (data != null) {
return data;
}
uLocale = uLocale.getFallback();
if (uLocale == null) {
return neutral;
}
}
} }
/** /**
@ -74,17 +68,24 @@ public class GenderInfo {
* gender(all female) = female, otherwise gender(list) = male. * gender(all female) = female, otherwise gender(list) = male.
* In particular, any 'other' value makes the overall gender be 'male'. * In particular, any 'other' value makes the overall gender be 'male'.
*/ */
MALE_TAINTS MALE_TAINTS;
}
/** private static Map<String, ListGenderStyle> fromNameMap =
* Reset the data used for mapping locales to styles. Only for use in CLDR and in testing! new HashMap<String, ListGenderStyle>(3);
* @param newULocaleToListGender replacement data, copied internally for safety.
* @internal static {
*/ fromNameMap.put("neutral", NEUTRAL);
public static void setLocaleMapping(Map<ULocale,GenderInfo> newULocaleToListGender) { fromNameMap.put("maleTaints", MALE_TAINTS);
localeToListGender.clear(); fromNameMap.put("mixedNeutral", MIXED_NEUTRAL);
localeToListGender.putAll(newULocaleToListGender); }
public static ListGenderStyle fromName(String name) {
ListGenderStyle result = fromNameMap.get(name);
if (result == null) {
throw new IllegalArgumentException("Unknown gender style name: " + name);
}
return result;
}
} }
/** /**
@ -157,18 +158,41 @@ public class GenderInfo {
private static GenderInfo neutral = new GenderInfo(ListGenderStyle.NEUTRAL); private static GenderInfo neutral = new GenderInfo(ListGenderStyle.NEUTRAL);
// TODO Get this data from a resource bundle generated from CLDR. private static class Cache {
// For now, hard coded. private final ICUCache<ULocale, GenderInfo> cache =
new SimpleCache<ULocale, GenderInfo>();
private static Map<ULocale,GenderInfo> localeToListGender = new HashMap<ULocale,GenderInfo>(); public GenderInfo get(ULocale locale) {
static { GenderInfo result = cache.get(locale);
GenderInfo taints = new GenderInfo(ListGenderStyle.MALE_TAINTS); if (result == null) {
for (String locale : Arrays.asList("ar", "ca", "cs", "hr", "es", "fr", "he", "hi", "it", "lt", "lv", "mr", "nl", "pl", "pt", "ro", "ru", "sk", "sl", "sr", "uk", "ur", "zh")) { result = load(locale);
localeToListGender.put(new ULocale(locale), taints); if (result == null) {
ULocale fallback = locale.getFallback();
// We call get() recursively so that we can leverage the cache
// for all fallback locales too. If we get to the root locale,
// and find no resource assume that list gender style is NEUTRAL.
result = fallback == null ? neutral : get(fallback);
}
cache.put(locale, result);
}
return result;
} }
GenderInfo mixed = new GenderInfo(ListGenderStyle.MIXED_NEUTRAL);
for (String locale : Arrays.asList("el", "is")) { private static GenderInfo load(ULocale ulocale) {
localeToListGender.put(new ULocale(locale), mixed); UResourceBundle rb = UResourceBundle.getBundleInstance(
ICUResourceBundle.ICU_BASE_NAME,
"genderList",
ICUResourceBundle.ICU_DATA_CLASS_LOADER, true);
UResourceBundle genderList = rb.get("genderList");
try {
return new GenderInfo(
ListGenderStyle.fromName(genderList.getString(ulocale.toString())));
} catch (MissingResourceException mre) {
return null;
}
} }
} }
private static Cache genderInfoCache = new Cache();
} }