Home | History | Annotate | Download | only in locale
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2017 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 package android.icu.impl.locale;
      5 
      6 import java.util.Collection;
      7 import java.util.Collections;
      8 import java.util.Enumeration;
      9 import java.util.HashMap;
     10 import java.util.Map;
     11 import java.util.Map.Entry;
     12 import java.util.Set;
     13 import java.util.TreeMap;
     14 
     15 import android.icu.impl.ICUData;
     16 import android.icu.impl.ICUResourceBundle;
     17 import android.icu.impl.Utility;
     18 import android.icu.impl.locale.XCldrStub.HashMultimap;
     19 import android.icu.impl.locale.XCldrStub.Multimap;
     20 import android.icu.impl.locale.XCldrStub.Multimaps;
     21 import android.icu.util.ICUException;
     22 import android.icu.util.ULocale;
     23 import android.icu.util.ULocale.Minimize;
     24 import android.icu.util.UResourceBundle;
     25 
     26 /**
     27  * @hide Only a subset of ICU is exposed in Android
     28  */
     29 public class XLikelySubtags {
     30 
     31     private static final XLikelySubtags DEFAULT = new XLikelySubtags();
     32 
     33     public static final XLikelySubtags getDefault() {
     34         return DEFAULT;
     35     }
     36 
     37     @SuppressWarnings("unchecked")
     38     static abstract class Maker {
     39         abstract <V> V make();
     40 
     41         public <K,V> V getSubtable(Map<K, V> langTable, final K language) {
     42             V scriptTable = langTable.get(language);
     43             if (scriptTable == null) {
     44                 langTable.put(language, scriptTable = (V) make());
     45             }
     46             return scriptTable;
     47         }
     48 
     49         static final Maker HASHMAP = new Maker() {
     50             @Override
     51             public Map<Object,Object> make() {
     52                 return new HashMap<Object,Object>();
     53             }
     54         };
     55 
     56         static final Maker TREEMAP = new Maker() {
     57             @Override
     58             public Map<Object,Object> make() {
     59                 return new TreeMap<Object,Object>();
     60             }
     61         };
     62     }
     63 
     64     public static class Aliases {
     65         final Map<String, String> toCanonical;
     66         final Multimap<String, String> toAliases;
     67         public String getCanonical(String alias) {
     68             String canonical = toCanonical.get(alias);
     69             return canonical == null ? alias : canonical;
     70         }
     71         public Set<String> getAliases(String canonical) {
     72             Set<String> aliases = toAliases.get(canonical);
     73             return aliases == null ? Collections.singleton(canonical) : aliases;
     74         }
     75         public Aliases(String key) {
     76             UResourceBundle metadata = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME,"metadata",ICUResourceBundle.ICU_DATA_CLASS_LOADER);
     77             UResourceBundle metadataAlias = metadata.get("alias");
     78             UResourceBundle territoryAlias = metadataAlias.get(key);
     79             Map<String, String> toCanonical1 = new HashMap<String, String>();
     80             for ( int i = 0 ; i < territoryAlias.getSize(); i++ ) {
     81                 UResourceBundle res = territoryAlias.get(i);
     82                 String aliasFrom = res.getKey();
     83                 if (aliasFrom.contains("_")) {
     84                     continue; // only simple aliasing
     85                 }
     86                 String aliasReason = res.get("reason").getString();
     87                 if (aliasReason.equals("overlong")) {
     88                     continue;
     89                 }
     90                 String aliasTo = res.get("replacement").getString();
     91                 int spacePos = aliasTo.indexOf(' ');
     92                 String aliasFirst = spacePos < 0 ? aliasTo : aliasTo.substring(0, spacePos);
     93                 if (aliasFirst.contains("_")) {
     94                     continue; // only simple aliasing
     95                 }
     96                 toCanonical1.put(aliasFrom, aliasFirst);
     97             }
     98             if (key.equals("language")) {
     99                 toCanonical1.put("mo", "ro"); // special case
    100             }
    101             toCanonical = Collections.unmodifiableMap(toCanonical1);
    102             toAliases = Multimaps.invertFrom(toCanonical1, HashMultimap.<String,String>create());
    103         }
    104     }
    105 
    106     public static class LSR {
    107         public final String language;
    108         public final String script;
    109         public final String region;
    110 
    111         public static Aliases LANGUAGE_ALIASES = new Aliases("language");
    112         public static Aliases REGION_ALIASES = new Aliases("territory");
    113 
    114         public static LSR from(String language, String script, String region) {
    115             return new LSR(language, script, region);
    116         }
    117 
    118         // from http://unicode.org/reports/tr35/#Unicode_language_identifier
    119         // but simplified to requiring language subtag, and nothing beyond region
    120         // #1 is language
    121         // #2 is script
    122         // #3 is region
    123         //        static final String pat =
    124         //                "language_id = (unicode_language_subtag)"
    125         //                        + "(?:sep(unicode_script_subtag))?"
    126         //                        + "(?:sep(unicode_region_subtag))?;\n"
    127         //                        + "unicode_language_subtag = alpha{2,3}|alpha{5,8};\n"
    128         //                        + "unicode_script_subtag = alpha{4};\n"
    129         //                        + "unicode_region_subtag  = alpha{2}|digit{3};\n"
    130         //                        + "sep    = [-_];\n"
    131         //                        + "digit  = [0-9];\n"
    132         //                        + "alpha   = [A-Za-z];\n"
    133         //                        ;
    134         //        static {
    135         //            System.out.println(pat);
    136         //            System.out.println(new UnicodeRegex().compileBnf(pat));
    137         //        }
    138         //        static final Pattern LANGUAGE_PATTERN = Pattern.compile(
    139         //                "([a-zA-Z0-9]+)" // (?:[-_]([a-zA-Z0-9]+))?(?:[-_]([a-zA-Z0-9]+))?"
    140         //                //new UnicodeRegex().compileBnf(pat)
    141         //                );
    142         //
    143         // TODO: fix this to check for format. Not required, since this is only called internally, but safer for the future.
    144         static LSR from(String languageIdentifier) {
    145             String[] parts = languageIdentifier.split("[-_]");
    146             if (parts.length < 1 || parts.length > 3) {
    147                 throw new ICUException("too many subtags");
    148             }
    149             String lang = parts[0].toLowerCase();
    150             String p2 = parts.length < 2 ? "": parts[1];
    151             String p3 = parts.length < 3 ? "": parts[2];
    152             return p2.length() < 4 ? new LSR(lang, "", p2) : new LSR(lang, p2, p3);
    153 
    154             //            Matcher matcher = LANGUAGE_PATTERN.matcher(languageIdentifier);
    155             //            if (!matcher.matches()) {
    156             //                return new LSR(matcher.group(1), matcher.group(2), matcher.group(3));
    157             //            }
    158             //            System.out.println(RegexUtilities.showMismatch(matcher, languageIdentifier));
    159             //            throw new ICUException("invalid language id");
    160         }
    161 
    162         public static LSR from(ULocale locale) {
    163             return new LSR(locale.getLanguage(), locale.getScript(), locale.getCountry());
    164         }
    165 
    166         public static LSR fromMaximalized(ULocale locale) {
    167             return fromMaximalized(locale.getLanguage(), locale.getScript(), locale.getCountry());
    168         }
    169 
    170         public static LSR fromMaximalized(String language, String script, String region) {
    171             String canonicalLanguage = LANGUAGE_ALIASES.getCanonical(language);
    172             // script is ok
    173             String canonicalRegion = REGION_ALIASES.getCanonical(region); // getCanonical(REGION_ALIASES.get(region));
    174 
    175             return DEFAULT.maximize(canonicalLanguage, script, canonicalRegion);
    176         }
    177 
    178         public LSR(String language, String script, String region) {
    179             this.language = language;
    180             this.script = script;
    181             this.region = region;
    182         }
    183 
    184         @Override
    185         public String toString() {
    186             StringBuilder result = new StringBuilder(language);
    187             if (!script.isEmpty()) {
    188                 result.append('-').append(script);
    189             }
    190             if (!region.isEmpty()) {
    191                 result.append('-').append(region);
    192             }
    193             return result.toString();
    194         }
    195         public LSR replace(String language2, String script2, String region2) {
    196             if (language2 == null && script2 == null && region2 == null) return this;
    197             return new LSR(
    198                     language2 == null ? language: language2,
    199                             script2 == null ? script : script2,
    200                                     region2 == null ? region : region2);
    201         }
    202         @Override
    203         public boolean equals(Object obj) {
    204             LSR other;
    205             return this == obj ||
    206                     (obj != null
    207                     && obj.getClass() == this.getClass()
    208                     && language.equals((other = (LSR) obj).language)
    209                     && script.equals(other.script)
    210                     && region.equals(other.region));
    211         }
    212         @Override
    213         public int hashCode() {
    214             return Utility.hash(language, script, region);
    215         }
    216     }
    217 
    218     final Map<String, Map<String, Map<String, LSR>>> langTable;
    219 
    220     public XLikelySubtags() {
    221         this(getDefaultRawData(), true);
    222     }
    223 
    224     private static Map<String, String> getDefaultRawData() {
    225         Map<String, String> rawData = new TreeMap<String, String>();
    226         UResourceBundle bundle = UResourceBundle.getBundleInstance( ICUData.ICU_BASE_NAME, "likelySubtags");
    227         for (Enumeration<String> enumer = bundle.getKeys(); enumer.hasMoreElements();) {
    228             String key = enumer.nextElement();
    229             rawData.put(key, bundle.getString(key));
    230         }
    231         return rawData;
    232     }
    233 
    234     public XLikelySubtags(Map<String, String> rawData, boolean skipNoncanonical) {
    235         this.langTable = init(rawData, skipNoncanonical);
    236     }
    237 
    238     private Map<String, Map<String, Map<String, LSR>>> init(final Map<String, String> rawData, boolean skipNoncanonical) {
    239         // prepare alias info. We want a mapping from the canonical form to all aliases
    240 
    241         //Multimap<String,String> canonicalToAliasLanguage = HashMultimap.create();
    242         //        getAliasInfo(LANGUAGE_ALIASES, canonicalToAliasLanguage);
    243 
    244         // Don't bother with script; there are none
    245 
    246         //Multimap<String,String> canonicalToAliasRegion = HashMultimap.create();
    247         //        getAliasInfo(REGION_ALIASES, canonicalToAliasRegion);
    248 
    249         Maker maker = Maker.TREEMAP;
    250         Map<String, Map<String, Map<String, LSR>>> result = maker.make();
    251         //        Splitter bar = Splitter.on('_');
    252         //        int last = -1;
    253         // set the base data
    254         Map<LSR,LSR> internCache = new HashMap<LSR,LSR>();
    255         for (Entry<String, String> sourceTarget : rawData.entrySet()) {
    256             LSR ltp = LSR.from(sourceTarget.getKey());
    257             final String language = ltp.language;
    258             final String script = ltp.script;
    259             final String region = ltp.region;
    260 
    261             ltp = LSR.from(sourceTarget.getValue());
    262             String languageTarget = ltp.language;
    263             final String scriptTarget = ltp.script;
    264             final String regionTarget = ltp.region;
    265 
    266             set(result, language, script, region, languageTarget, scriptTarget, regionTarget, internCache);
    267             // now add aliases
    268             Collection<String> languageAliases = LSR.LANGUAGE_ALIASES.getAliases(language);
    269             //            if (languageAliases.isEmpty()) {
    270             //                languageAliases = Collections.singleton(language);
    271             //            }
    272             Collection<String> regionAliases = LSR.REGION_ALIASES.getAliases(region);
    273             //            if (regionAliases.isEmpty()) {
    274             //                regionAliases = Collections.singleton(region);
    275             //            }
    276             for (String languageAlias : languageAliases) {
    277                 for (String regionAlias : regionAliases) {
    278                     if (languageAlias.equals(language) && regionAlias.equals(region)) {
    279                         continue;
    280                     }
    281                     set(result, languageAlias, script, regionAlias, languageTarget, scriptTarget, regionTarget, internCache);
    282                 }
    283             }
    284         }
    285         // hack
    286         set(result, "und", "Latn", "", "en", "Latn", "US", internCache);
    287 
    288         // hack, ensure that if und-YY => und-Xxxx-YY, then we add Xxxx=>YY to the table
    289         // <likelySubtag from="und_GH" to="ak_Latn_GH"/>
    290 
    291         // so und-Latn-GH   =>  ak-Latn-GH
    292         Map<String, Map<String, LSR>> undScriptMap = result.get("und");
    293         Map<String, LSR> undEmptyRegionMap = undScriptMap.get("");
    294         for (Entry<String, LSR> regionEntry : undEmptyRegionMap.entrySet()) {
    295             final LSR value = regionEntry.getValue();
    296             set(result, "und", value.script, value.region, value);
    297         }
    298         //
    299         // check that every level has "" (or "und")
    300         if (!result.containsKey("und")) {
    301             throw new IllegalArgumentException("failure: base");
    302         }
    303         for (Entry<String, Map<String, Map<String, LSR>>> langEntry : result.entrySet()) {
    304             String lang = langEntry.getKey();
    305             final Map<String, Map<String, LSR>> scriptMap = langEntry.getValue();
    306             if (!scriptMap.containsKey("")) {
    307                 throw new IllegalArgumentException("failure: " + lang);
    308             }
    309             for (Entry<String, Map<String, LSR>> scriptEntry : scriptMap.entrySet()) {
    310                 String script = scriptEntry.getKey();
    311                 final Map<String, LSR> regionMap = scriptEntry.getValue();
    312                 if (!regionMap.containsKey("")) {
    313                     throw new IllegalArgumentException("failure: " + lang + "-" + script);
    314                 }
    315                 //                for (Entry<String, LSR> regionEntry : regionMap.entrySet()) {
    316                 //                    String region = regionEntry.getKey();
    317                 //                    LSR value = regionEntry.getValue();
    318                 //                }
    319             }
    320         }
    321         return result;
    322     }
    323 
    324     //    private void getAliasInfo(Map<String, R2<List<String>, String>> aliasInfo, Multimap<String, String> canonicalToAlias) {
    325     //        for (Entry<String, R2<List<String>, String>> e : aliasInfo.entrySet()) {
    326     //            final String alias = e.getKey();
    327     //            if (alias.contains("_")) {
    328     //                continue; // only do simple aliasing
    329     //            }
    330     //            String canonical = getCanonical(e.getValue());
    331     //            canonicalToAlias.put(canonical, alias);
    332     //        }
    333     //    }
    334 
    335     //    private static String getCanonical(R2<List<String>, String> aliasAndReason) {
    336     //        if (aliasAndReason == null) {
    337     //            return null;
    338     //        }
    339     //        if (aliasAndReason.get1().equals("overlong")) {
    340     //            return null;
    341     //        }
    342     //        List<String> value = aliasAndReason.get0();
    343     //        if (value.size() != 1) {
    344     //            return null;
    345     //        }
    346     //        final String canonical = value.iterator().next();
    347     //        if (canonical.contains("_")) {
    348     //            return null; // only do simple aliasing
    349     //        }
    350     //        return canonical;
    351     //    }
    352 
    353     private void set(Map<String, Map<String, Map<String, LSR>>> langTable, final String language, final String script, final String region,
    354             final String languageTarget, final String scriptTarget, final String regionTarget, Map<LSR, LSR> internCache) {
    355         LSR newValue = new LSR(languageTarget, scriptTarget, regionTarget);
    356         LSR oldValue = internCache.get(newValue);
    357         if (oldValue == null) {
    358             internCache.put(newValue, newValue);
    359             oldValue = newValue;
    360         }
    361         set(langTable, language, script, region, oldValue);
    362     }
    363 
    364     private void set(Map<String, Map<String, Map<String, LSR>>> langTable, final String language, final String script, final String region, LSR newValue) {
    365         Map<String, Map<String, LSR>> scriptTable = Maker.TREEMAP.getSubtable(langTable, language);
    366         Map<String, LSR> regionTable = Maker.TREEMAP.getSubtable(scriptTable, script);
    367         //        LSR oldValue = regionTable.get(region);
    368         //        if (oldValue != null) {
    369         //            int debug = 0;
    370         //        }
    371         regionTable.put(region, newValue);
    372     }
    373 
    374     /**
    375      * Convenience methods
    376      */
    377     public LSR maximize(String source) {
    378         return maximize(ULocale.forLanguageTag(source));
    379     }
    380 
    381     public LSR maximize(ULocale source) {
    382         return maximize(source.getLanguage(), source.getScript(), source.getCountry());
    383     }
    384 
    385     public LSR maximize(LSR source) {
    386         return maximize(source.language, source.script, source.region);
    387     }
    388 
    389     //    public static ULocale addLikelySubtags(ULocale loc) {
    390     //
    391     //    }
    392 
    393     /**
    394      * Raw access to addLikelySubtags. Input must be in canonical format, eg "en", not "eng" or "EN".
    395      */
    396     public LSR maximize(String language, String script, String region) {
    397         int retainOldMask = 0;
    398         Map<String, Map<String, LSR>> scriptTable = langTable.get(language);
    399         if (scriptTable == null) { // cannot happen if language == "und"
    400             retainOldMask |= 4;
    401             scriptTable = langTable.get("und");
    402         } else if (!language.equals("und")) {
    403             retainOldMask |= 4;
    404         }
    405 
    406         if (script.equals("Zzzz")) {
    407             script = "";
    408         }
    409         Map<String, LSR> regionTable = scriptTable.get(script);
    410         if (regionTable == null) { // cannot happen if script == ""
    411             retainOldMask |= 2;
    412             regionTable = scriptTable.get("");
    413         } else if (!script.isEmpty()) {
    414             retainOldMask |= 2;
    415         }
    416 
    417         if (region.equals("ZZ")) {
    418             region = "";
    419         }
    420         LSR result = regionTable.get(region);
    421         if (result == null) { // cannot happen if region == ""
    422             retainOldMask |= 1;
    423             result = regionTable.get("");
    424             if (result == null) {
    425                 return null;
    426             }
    427         } else if (!region.isEmpty()) {
    428             retainOldMask |= 1;
    429         }
    430 
    431         switch (retainOldMask) {
    432         default:
    433         case 0: return result;
    434         case 1: return result.replace(null, null, region);
    435         case 2: return result.replace(null, script, null);
    436         case 3: return result.replace(null, script, region);
    437         case 4: return result.replace(language, null, null);
    438         case 5: return result.replace(language, null, region);
    439         case 6: return result.replace(language, script, null);
    440         case 7: return result.replace(language, script, region);
    441         }
    442     }
    443 
    444     @SuppressWarnings("unused")
    445     private LSR minimizeSubtags(String languageIn, String scriptIn, String regionIn, Minimize fieldToFavor) {
    446         LSR result = maximize(languageIn, scriptIn, regionIn);
    447 
    448         // We could try just a series of checks, like:
    449         // LSR result2 = addLikelySubtags(languageIn, "", "");
    450         // if result.equals(result2) return result2;
    451         // However, we can optimize 2 of the cases:
    452         //   (languageIn, "", "")
    453         //   (languageIn, "", regionIn)
    454 
    455         Map<String, Map<String, LSR>> scriptTable = langTable.get(result.language);
    456 
    457         Map<String, LSR> regionTable0 = scriptTable.get("");
    458         LSR value00 = regionTable0.get("");
    459         boolean favorRegionOk = false;
    460         if (result.script.equals(value00.script)) { //script is default
    461             if (result.region.equals(value00.region)) {
    462                 return result.replace(null, "", "");
    463             } else if (fieldToFavor == Minimize.FAVOR_REGION) {
    464                 return result.replace(null, "", null);
    465             } else {
    466                 favorRegionOk = true;
    467             }
    468         }
    469 
    470         // The last case is not as easy to optimize.
    471         // Maybe do later, but for now use the straightforward code.
    472         LSR result2 = maximize(languageIn, scriptIn, "");
    473         if (result2.equals(result)) {
    474             return result.replace(null, null, "");
    475         } else if (favorRegionOk) {
    476             return result.replace(null, "", null);
    477         }
    478         return result;
    479     }
    480 
    481     private static StringBuilder show(Map<?,?> map, String indent, StringBuilder output) {
    482         String first = indent.isEmpty() ? "" : "\t";
    483         for (Entry<?,?> e : map.entrySet()) {
    484             String key = e.getKey().toString();
    485             Object value = e.getValue();
    486             output.append(first + (key.isEmpty() ? "" : key));
    487             if (value instanceof Map) {
    488                 show((Map<?,?>)value, indent+"\t", output);
    489             } else {
    490                 output.append("\t" + Utility.toString(value)).append("\n");
    491             }
    492             first = indent;
    493         }
    494         return output;
    495     }
    496 
    497     @Override
    498     public String toString() {
    499         return show(langTable, "", new StringBuilder()).toString();
    500     }
    501 
    502     //    public static void main(String[] args) {
    503     //        System.out.println(LSR.fromMaximalized(ULocale.ENGLISH));
    504     //
    505     //        final Map<String, String> rawData = sdi.getLikelySubtags();
    506     //        XLikelySubtags ls = XLikelySubtags.getDefault();
    507     //        System.out.println(ls);
    508     //        ls.maximize(new ULocale("iw"));
    509     //        if (true) return;
    510     //
    511     //        LanguageTagParser ltp = new LanguageTagParser();
    512     //
    513     //        // get all the languages, scripts, and regions
    514     //        Set<String> languages = new TreeSet<String>();
    515     //        Set<String> scripts = new TreeSet<String>();
    516     //        Set<String> regions = new TreeSet<String>();
    517     //        Counter<String> languageCounter = new Counter<String>();
    518     //        Counter<String> scriptCounter = new Counter<String>();
    519     //        Counter<String> regionCounter = new Counter<String>();
    520     //
    521     //        for (Entry<String, String> sourceTarget : rawData.entrySet()) {
    522     //            final String source = sourceTarget.getKey();
    523     //            ltp.set(source);
    524     //            languages.add(ltp.getLanguage());
    525     //            scripts.add(ltp.getScript());
    526     //            regions.add(ltp.getRegion());
    527     //            final String target = sourceTarget.getValue();
    528     //            ltp.set(target);
    529     //            add(target, languageCounter, ltp.getLanguage(), 1);
    530     //            add(target, scriptCounter, ltp.getScript(), 1);
    531     //            add(target, regionCounter, ltp.getRegion(), 1);
    532     //        }
    533     //        ltp.set("und-Zzzz-ZZ");
    534     //        languageCounter.add(ltp.getLanguage(), 1);
    535     //        scriptCounter.add(ltp.getScript(), 1);
    536     //        regionCounter.add(ltp.getRegion(), 1);
    537     //
    538     //        if (SHORT) {
    539     //            removeSingletons(languages, languageCounter);
    540     //            removeSingletons(scripts, scriptCounter);
    541     //            removeSingletons(regions, regionCounter);
    542     //        }
    543     //
    544     //        System.out.println("languages: " + languages.size() + "\n\t" + languages + "\n\t" + languageCounter);
    545     //        System.out.println("scripts: " + scripts.size() + "\n\t" + scripts + "\n\t" + scriptCounter);
    546     //        System.out.println("regions: " + regions.size() + "\n\t" + regions + "\n\t" + regionCounter);
    547     //
    548     //        int maxCount = Integer.MAX_VALUE;
    549     //
    550     //        int counter = maxCount;
    551     //        long tempTime = System.nanoTime();
    552     //        newMax:
    553     //            for (String language : languages) {
    554     //                for (String script : scripts) {
    555     //                    for (String region : regions) {
    556     //                        if (--counter < 0) break newMax;
    557     //                        LSR result = ls.maximize(language, script, region);
    558     //                    }
    559     //                }
    560     //            }
    561     //        long newMaxTime = System.nanoTime() - tempTime;
    562     //        System.out.println("newMaxTime: " + newMaxTime);
    563     //
    564     //        counter = maxCount;
    565     //        tempTime = System.nanoTime();
    566     //        newMin:
    567     //            for (String language : languages) {
    568     //                for (String script : scripts) {
    569     //                    for (String region : regions) {
    570     //                        if (--counter < 0) break newMin;
    571     //                        LSR minNewS = ls.minimizeSubtags(language, script, region, Minimize.FAVOR_SCRIPT);
    572     //                    }
    573     //                }
    574     //            }
    575     //        long newMinTime = System.nanoTime() - tempTime;
    576     //        System.out.println("newMinTime: " + newMinTime);
    577     //
    578     //        // *****
    579     //
    580     //        tempTime = System.nanoTime();
    581     //        counter = maxCount;
    582     //        oldMax:
    583     //            for (String language : languages) {
    584     //                for (String script : scripts) {
    585     //                    for (String region : regions) {
    586     //                        if (--counter < 0) break oldMax;
    587     //                        ULocale tempLocale = new ULocale(language, script, region);
    588     //                        ULocale max = ULocale.addLikelySubtags(tempLocale);
    589     //                    }
    590     //                }
    591     //            }
    592     //        long oldMaxTime = System.nanoTime() - tempTime;
    593     //        System.out.println("oldMaxTime: " + oldMaxTime + "\t" + oldMaxTime/newMaxTime + "x");
    594     //
    595     //        counter = maxCount;
    596     //        tempTime = System.nanoTime();
    597     //        oldMin:
    598     //            for (String language : languages) {
    599     //                for (String script : scripts) {
    600     //                    for (String region : regions) {
    601     //                        if (--counter < 0) break oldMin;
    602     //                        ULocale tempLocale = new ULocale(language, script, region);
    603     //                        ULocale minOldS = ULocale.minimizeSubtags(tempLocale, Minimize.FAVOR_SCRIPT);
    604     //                    }
    605     //                }
    606     //            }
    607     //        long oldMinTime = System.nanoTime() - tempTime;
    608     //        System.out.println("oldMinTime: " + oldMinTime + "\t" + oldMinTime/newMinTime + "x");
    609     //
    610     //        counter = maxCount;
    611     //        testMain:
    612     //            for (String language : languages) {
    613     //                System.out.println(language);
    614     //                int tests = 0;
    615     //                for (String script : scripts) {
    616     //                    for (String region : regions) {
    617     //                        ++tests;
    618     //                        if (--counter < 0) break testMain;
    619     //                        LSR maxNew = ls.maximize(language, script, region);
    620     //                        LSR minNewS = ls.minimizeSubtags(language, script, region, Minimize.FAVOR_SCRIPT);
    621     //                        LSR minNewR = ls.minimizeSubtags(language, script, region, Minimize.FAVOR_REGION);
    622     //
    623     //                        ULocale tempLocale = new ULocale(language, script, region);
    624     //                        ULocale maxOld = ULocale.addLikelySubtags(tempLocale);
    625     //                        ULocale minOldS = ULocale.minimizeSubtags(tempLocale, Minimize.FAVOR_SCRIPT);
    626     //                        ULocale minOldR = ULocale.minimizeSubtags(tempLocale, Minimize.FAVOR_REGION);
    627     //
    628     //                        // check values
    629     //                        final String maxNewS = String.valueOf(maxNew);
    630     //                        final String maxOldS = maxOld.toLanguageTag();
    631     //                        boolean sameMax = maxOldS.equals(maxNewS);
    632     //
    633     //                        final String minNewSS = String.valueOf(minNewS);
    634     //                        final String minOldSS = minOldS.toLanguageTag();
    635     //                        boolean sameMinS = minNewSS.equals(minOldSS);
    636     //
    637     //                        final String minNewRS = String.valueOf(minNewR);
    638     //                        final String minOldRS = minOldS.toLanguageTag();
    639     //                        boolean sameMinR = minNewRS.equals(minOldRS);
    640     //
    641     //                        if (sameMax && sameMinS && sameMinR) continue;
    642     //                        System.out.println(new LSR(language, script, region)
    643     //                                + "\tmax: " + maxNew
    644     //                                + (sameMax ? "" : "" + maxOldS)
    645     //                                + "\tminS: " + minNewS
    646     //                                + (sameMinS ? "" : "" + minOldS)
    647     //                                + "\tminR: " + minNewR
    648     //                                + (sameMinR ? "" : "" + minOldR)
    649     //                                );
    650     //                    }
    651     //                }
    652     //                System.out.println(language + ": " + tests);
    653     //            }
    654     //    }
    655     //
    656     //    private static void add(String target, Counter<String> languageCounter, String language, int count) {
    657     //        if (language.equals("aa")) {
    658     //            int debug = 0;
    659     //        }
    660     //        languageCounter.add(language, count);
    661     //    }
    662     //
    663     //    private static void removeSingletons(Set<String> languages, Counter<String> languageCounter) {
    664     //        for (String s : languageCounter) {
    665     //            final long count = languageCounter.get(s);
    666     //            if (count <= 1) {
    667     //                languages.remove(s);
    668     //            }
    669     //        }
    670     //    }
    671 }
    672