ICU-9125 Add initial version, @internal for now.
X-SVN-Rev: 31454
This commit is contained in:
parent
8638e4b63f
commit
9fdb34c0d3
2
.gitattributes
vendored
2
.gitattributes
vendored
@ -247,6 +247,7 @@ icu4j/main/classes/core/.settings/org.eclipse.jdt.core.prefs -text
|
||||
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/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
|
||||
@ -677,6 +678,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.ULocale.dat -text
|
||||
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/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
|
||||
|
154
icu4j/main/classes/core/src/com/ibm/icu/util/GenderInfo.java
Normal file
154
icu4j/main/classes/core/src/com/ibm/icu/util/GenderInfo.java
Normal file
@ -0,0 +1,154 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 2003-2012, Google, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*/
|
||||
package com.ibm.icu.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.ibm.icu.util.ULocale;
|
||||
|
||||
/**
|
||||
* Provide information about gender in locales based on data in CLDR. Currently supplies gender of lists.
|
||||
* @author markdavis
|
||||
* @internal
|
||||
*/
|
||||
public class GenderInfo {
|
||||
|
||||
private final ListGenderStyle style; // set based on locale
|
||||
|
||||
/**
|
||||
* Gender: OTHER means either the information is unavailable, or the person has declined to state MALE or FEMALE.
|
||||
* @internal
|
||||
*/
|
||||
public enum Gender {MALE, FEMALE, OTHER}
|
||||
|
||||
/**
|
||||
* Create GenderInfo from a ULocale.
|
||||
* @param uLocale desired locale
|
||||
* @internal
|
||||
*/
|
||||
public GenderInfo(ULocale uLocale) {
|
||||
ULocale language = new ULocale(uLocale.getLanguage()); // in the hard coded data, the language is sufficient. Will change with RB.
|
||||
ListGenderStyle tempStyle = localeToListGender.get(language);
|
||||
style = tempStyle == null ? ListGenderStyle.NEUTRAL : tempStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create GenderInfo from a Locale.
|
||||
* @param locale desired locale
|
||||
* @internal
|
||||
*/
|
||||
public GenderInfo(Locale locale) {
|
||||
this(ULocale.forLocale(locale));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enum only meant for use in CLDR and in testing. Indicates the category for the locale.
|
||||
* @internal
|
||||
*/
|
||||
public enum ListGenderStyle {
|
||||
/**
|
||||
* Always OTHER (if more than one)
|
||||
*/
|
||||
NEUTRAL,
|
||||
/**
|
||||
* gender(all male) = male, gender(all female) = female, otherwise gender(list) = other
|
||||
*/
|
||||
MIXED_NEUTRAL,
|
||||
/**
|
||||
* gender(all female) = female, otherwise gender(list) = male
|
||||
*/
|
||||
MALE_TAINTS
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the data used for mapping locales to styles. Only for use in CLDR and in testing!
|
||||
* @param newULocaleToListGender replacement data, copied internally for safety.
|
||||
* @internal
|
||||
*/
|
||||
public static void setLocaleMapping(Map<ULocale,ListGenderStyle> newULocaleToListGender) {
|
||||
localeToListGender.clear();
|
||||
for (Entry<ULocale, ListGenderStyle> entry : newULocaleToListGender.entrySet()) {
|
||||
localeToListGender.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the gender of a list, based on locale usage.
|
||||
* @param genders a list of genders.
|
||||
* @return the gender of the list.
|
||||
* @internal
|
||||
*/
|
||||
public Gender getListGender(Gender... genders) {
|
||||
return getListGender(Arrays.asList(genders));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the gender of a list, based on locale usage.
|
||||
* @param genders a list of genders.
|
||||
* @return the gender of the list.
|
||||
* @internal
|
||||
*/
|
||||
public Gender getListGender(List<Gender> genders) {
|
||||
if (genders.size() == 0 || style == ListGenderStyle.NEUTRAL) {
|
||||
return Gender.OTHER; // degenerate case
|
||||
}
|
||||
if (genders.size() == 1) {
|
||||
return genders.get(0); // degenerate case
|
||||
}
|
||||
switch(style) {
|
||||
case MIXED_NEUTRAL: // gender(all male) = male, gender(all female) = female, otherwise gender(list) = other
|
||||
boolean hasFemale = false;
|
||||
boolean hasMale = false;
|
||||
for (Gender gender : genders) {
|
||||
switch (gender) {
|
||||
case FEMALE:
|
||||
if (hasMale) {
|
||||
return Gender.OTHER;
|
||||
}
|
||||
hasFemale = true;
|
||||
break;
|
||||
case MALE:
|
||||
if (hasFemale) {
|
||||
return Gender.OTHER;
|
||||
}
|
||||
hasMale = true;
|
||||
break;
|
||||
case OTHER:
|
||||
return Gender.OTHER;
|
||||
}
|
||||
}
|
||||
return hasMale ? Gender.MALE : hasFemale ? Gender.FEMALE : Gender.OTHER;
|
||||
case MALE_TAINTS: // gender(all female) = female, otherwise gender(list) = male
|
||||
for (Gender gender : genders) {
|
||||
if (gender != Gender.FEMALE) {
|
||||
return Gender.MALE;
|
||||
}
|
||||
}
|
||||
return Gender.FEMALE;
|
||||
default:
|
||||
return Gender.OTHER;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Get this data from a resource bundle generated from CLDR.
|
||||
// For now, hard coded.
|
||||
|
||||
private static Map<ULocale,ListGenderStyle> localeToListGender = new HashMap<ULocale,ListGenderStyle>();
|
||||
static {
|
||||
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")) {
|
||||
localeToListGender.put(new ULocale(locale), ListGenderStyle.MALE_TAINTS);
|
||||
}
|
||||
for (String locale : Arrays.asList("el", "is")) {
|
||||
localeToListGender.put(new ULocale(locale), ListGenderStyle.MIXED_NEUTRAL);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 2003-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.List;
|
||||
|
||||
import com.ibm.icu.dev.test.TestFmwk;
|
||||
import com.ibm.icu.util.GenderInfo;
|
||||
import com.ibm.icu.util.GenderInfo.Gender;
|
||||
import com.ibm.icu.util.ULocale;
|
||||
|
||||
public class GenderInfoTest extends TestFmwk {
|
||||
public static void main(String[] args) {
|
||||
new GenderInfoTest().run(args);
|
||||
}
|
||||
|
||||
public static GenderInfo NEUTRAL_LOCALE = new GenderInfo(ULocale.ENGLISH);
|
||||
public static GenderInfo MIXED_NEUTRAL_LOCALE = new GenderInfo(new ULocale("is"));
|
||||
public static GenderInfo MALE_TAINTS_LOCALE = new GenderInfo(ULocale.FRANCE);
|
||||
|
||||
public void TestEmpty() {
|
||||
check(Gender.OTHER, Gender.OTHER);
|
||||
}
|
||||
|
||||
public void TestOne() {
|
||||
for (Gender g : Gender.values()) {
|
||||
check(g, g, g);
|
||||
}
|
||||
}
|
||||
|
||||
public void TestOther() {
|
||||
check(Gender.OTHER, Gender.MALE, Gender.MALE, Gender.FEMALE);
|
||||
check(Gender.OTHER, Gender.MALE, Gender.FEMALE, Gender.MALE);
|
||||
check(Gender.MALE, Gender.MALE, Gender.MALE, Gender.MALE);
|
||||
check(Gender.FEMALE, Gender.FEMALE, Gender.FEMALE, Gender.FEMALE);
|
||||
check(Gender.OTHER, Gender.MALE, Gender.FEMALE, Gender.OTHER);
|
||||
}
|
||||
|
||||
public void check(Gender mixed, Gender taints, Gender... genders) {
|
||||
List<Gender> mixed0 = Arrays.asList(genders);
|
||||
assertEquals("neutral " + mixed0, Gender.OTHER, NEUTRAL_LOCALE.getListGender(mixed0));
|
||||
assertEquals("mixed neutral " + mixed0, mixed, MIXED_NEUTRAL_LOCALE.getListGender(mixed0));
|
||||
assertEquals("male taints " + mixed0, taints, MALE_TAINTS_LOCALE.getListGender(mixed0));
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 1996-2011, International Business Machines Corporation and *
|
||||
* Copyright (C) 1996-2012, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*/
|
||||
@ -30,6 +30,7 @@ public class TestAll extends TestGroup {
|
||||
"TrieTest",
|
||||
"Trie2Test",
|
||||
"LocaleDataTest",
|
||||
"GenderInfoTest",
|
||||
"ULocaleTest",
|
||||
"LocaleAliasTest",
|
||||
"DebugUtilitiesTest",
|
||||
|
Loading…
Reference in New Issue
Block a user