• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5  ********************************************************************************
6  * Copyright (C) 2003-2016, Google, International Business Machines Corporation
7  * and others. All Rights Reserved.
8  ********************************************************************************
9 */
10 package ohos.global.icu.util;
11 
12 import java.util.Arrays;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Locale;
16 import java.util.Map;
17 import java.util.MissingResourceException;
18 
19 import ohos.global.icu.impl.ICUCache;
20 import ohos.global.icu.impl.ICUData;
21 import ohos.global.icu.impl.ICUResourceBundle;
22 import ohos.global.icu.impl.SimpleCache;
23 
24 /**
25  * Provide information about gender in locales based on data in CLDR. Currently supplies gender of lists.
26  * @author markdavis
27  * @deprecated This API is ICU internal only.
28  * @hide exposed on OHOS
29  * @hide draft / provisional / internal are hidden on OHOS
30  */
31 @Deprecated
32 public class GenderInfo {
33 
34     private final ListGenderStyle style; // set based on locale
35 
36     /**
37      * Gender: OTHER means either the information is unavailable, or the person has declined to state MALE or FEMALE.
38      * @deprecated This API is ICU internal only.
39      * @hide exposed on OHOS
40      * @hide draft / provisional / internal are hidden on OHOS
41      */
42     @Deprecated
43     public enum Gender {
44         /**
45          * @deprecated This API is ICU internal only.
46          * @hide draft / provisional / internal are hidden on OHOS
47          */
48         @Deprecated
49         MALE,
50         /**
51          * @deprecated This API is ICU internal only.
52          * @hide draft / provisional / internal are hidden on OHOS
53          */
54         @Deprecated
55         FEMALE,
56         /**
57          * @deprecated This API is ICU internal only.
58          * @hide draft / provisional / internal are hidden on OHOS
59          */
60         @Deprecated
61         OTHER
62     }
63 
64     /**
65      * Create GenderInfo from a ULocale.
66      * @param uLocale desired locale
67      * @deprecated This API is ICU internal only.
68      * @hide draft / provisional / internal are hidden on OHOS
69      */
70     @Deprecated
getInstance(ULocale uLocale)71     public static GenderInfo getInstance(ULocale uLocale) {
72         return genderInfoCache.get(uLocale);
73     }
74 
75     /**
76      * Create GenderInfo from a Locale.
77      * @param locale desired locale
78      * @deprecated This API is ICU internal only.
79      * @hide draft / provisional / internal are hidden on OHOS
80      */
81     @Deprecated
getInstance(Locale locale)82     public static GenderInfo getInstance(Locale locale) {
83         return getInstance(ULocale.forLocale(locale));
84     }
85 
86     /**
87      * Enum only meant for use in CLDR and in testing. Indicates the category for the locale.
88      * This only affects gender for lists more than one. For lists of 1 item, the gender
89      * of the list always equals the gender of that sole item.
90      * @deprecated This API is ICU internal only.
91      * @hide exposed on OHOS
92      * @hide draft / provisional / internal are hidden on OHOS
93      */
94     @Deprecated
95     public enum ListGenderStyle {
96         /**
97          * For an empty list, returns OTHER;
98          * For a single item, returns its gender;
99          * Otherwise always OTHER.
100          * @deprecated This API is ICU internal only.
101          * @hide draft / provisional / internal are hidden on OHOS
102          */
103         @Deprecated
104         NEUTRAL,
105         /**
106          * For an empty list, returns OTHER;
107          * For a single item, returns its gender;
108          * Otherwise gender(all male) = male, gender(all female) = female, otherwise gender(list) = other.
109          * So any 'other' value makes the overall gender be 'other'.
110          * @deprecated This API is ICU internal only.
111          * @hide draft / provisional / internal are hidden on OHOS
112          */
113         @Deprecated
114         MIXED_NEUTRAL,
115         /**
116          * For an empty list, returns OTHER;
117          * For a single item, returns its gender;
118          * Otherwise, gender(all female) = female, otherwise gender(list) = male.
119          * So for more than one item, any 'other' value makes the overall gender be 'male'.
120          * @deprecated This API is ICU internal only.
121          * @hide draft / provisional / internal are hidden on OHOS
122          */
123         @Deprecated
124         MALE_TAINTS;
125 
126         private static Map<String, ListGenderStyle> fromNameMap =
127             new HashMap<String, ListGenderStyle>(3);
128 
129         static {
130             fromNameMap.put("neutral", NEUTRAL);
131             fromNameMap.put("maleTaints", MALE_TAINTS);
132             fromNameMap.put("mixedNeutral", MIXED_NEUTRAL);
133         }
134 
135         /**
136          * @deprecated This API is ICU internal only.
137          * @hide draft / provisional / internal are hidden on OHOS
138          */
139         @Deprecated
fromName(String name)140         public static ListGenderStyle fromName(String name) {
141             ListGenderStyle result = fromNameMap.get(name);
142             if (result == null) {
143                 throw new IllegalArgumentException("Unknown gender style name: " + name);
144             }
145             return result;
146         }
147     }
148 
149     /**
150      * Get the gender of a list, based on locale usage.
151      * @param genders a list of genders.
152      * @return the gender of the list.
153      * @deprecated This API is ICU internal only.
154      * @hide draft / provisional / internal are hidden on OHOS
155      */
156     @Deprecated
getListGender(Gender... genders)157     public Gender getListGender(Gender... genders) {
158         return getListGender(Arrays.asList(genders));
159     }
160 
161     /**
162      * Get the gender of a list, based on locale usage.
163      * @param genders a list of genders.
164      * @return the gender of the list.
165      * @deprecated This API is ICU internal only.
166      * @hide draft / provisional / internal are hidden on OHOS
167      */
168     @Deprecated
getListGender(List<Gender> genders)169     public Gender getListGender(List<Gender> genders) {
170         if (genders.size() == 0) {
171             return Gender.OTHER; // degenerate case
172         }
173         if (genders.size() == 1) {
174             return genders.get(0); // degenerate case
175         }
176         switch(style) {
177         case NEUTRAL:
178             return Gender.OTHER;
179         case MIXED_NEUTRAL:
180             boolean hasFemale = false;
181             boolean hasMale = false;
182             for (Gender gender : genders) {
183                 switch (gender) {
184                 case FEMALE:
185                     if (hasMale) {
186                         return Gender.OTHER;
187                     }
188                     hasFemale = true;
189                     break;
190                 case MALE:
191                     if (hasFemale) {
192                         return Gender.OTHER;
193                     }
194                     hasMale = true;
195                     break;
196                 case OTHER:
197                     return Gender.OTHER;
198                 }
199             }
200             return hasMale ? Gender.MALE : Gender.FEMALE;
201             // Note: any OTHER would have caused a return in the loop, which always happens.
202         case MALE_TAINTS:
203             for (Gender gender : genders) {
204                 if (gender != Gender.FEMALE) {
205                     return Gender.MALE;
206                 }
207             }
208             return Gender.FEMALE;
209         default:
210             return Gender.OTHER;
211         }
212     }
213 
214     /**
215      * Only for testing and use with CLDR.
216      * @param genderStyle gender style
217      * @deprecated This API is ICU internal only.
218      * @hide draft / provisional / internal are hidden on OHOS
219      */
220     @Deprecated
GenderInfo(ListGenderStyle genderStyle)221     public GenderInfo(ListGenderStyle genderStyle) {
222         style = genderStyle;
223     }
224 
225     private static GenderInfo neutral = new GenderInfo(ListGenderStyle.NEUTRAL);
226 
227     private static class Cache {
228         private final ICUCache<ULocale, GenderInfo> cache =
229             new SimpleCache<ULocale, GenderInfo>();
230 
get(ULocale locale)231         public GenderInfo get(ULocale locale) {
232             GenderInfo result = cache.get(locale);
233             if (result == null) {
234                 result = load(locale);
235                 if (result == null) {
236                     ULocale fallback = locale.getFallback();
237 
238                     // We call get() recursively so that we can leverage the cache
239                     // for all fallback locales too. If we get to the root locale,
240                     // and find no resource assume that list gender style is NEUTRAL.
241                     result = fallback == null ? neutral : get(fallback);
242                 }
243                 cache.put(locale, result);
244             }
245             return result;
246         }
247 
load(ULocale ulocale)248         private static GenderInfo load(ULocale ulocale) {
249             UResourceBundle rb = UResourceBundle.getBundleInstance(
250                     ICUData.ICU_BASE_NAME,
251                     "genderList",
252                     ICUResourceBundle.ICU_DATA_CLASS_LOADER, true);
253             UResourceBundle genderList = rb.get("genderList");
254             try {
255                 return new GenderInfo(
256                         ListGenderStyle.fromName(genderList.getString(ulocale.toString())));
257             } catch (MissingResourceException mre) {
258                 return null;
259             }
260         }
261     }
262 
263     private static Cache genderInfoCache = new Cache();
264 }
265