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