Home | History | Annotate | Download | only in util
      1 package org.unicode.cldr.util;
      2 
      3 import java.util.Comparator;
      4 import java.util.LinkedHashSet;
      5 import java.util.Set;
      6 import java.util.TreeSet;
      7 
      8 import org.unicode.cldr.util.DayPeriods.DayPeriod;
      9 
     10 import com.ibm.icu.util.ULocale;
     11 
     12 public class DayPeriodsCheck {
     13 
     14     private static final int HOUR = 60 * 60 * 1000;
     15     static boolean html = false;
     16 
     17     static final Comparator<Enum<?>> ENUM_COMPARATOR = new Comparator<Enum<?>>() {
     18         @Override
     19         public int compare(Enum<?> o1, Enum<?> o2) {
     20             return o1.ordinal() - o2.ordinal();
     21         }
     22     };
     23 
     24     // QUICK CHECK
     25     public static void main(String[] args) {
     26         Set<ULocale> localesToCheck = new LinkedHashSet<>();
     27         if (args.length == 0) {
     28             args = new String[] { "groups" };
     29         }
     30         html = false;
     31         for (String arg : args) {
     32             localesToCheck.clear();
     33             switch (arg) {
     34             case "groups":
     35                 StandardCodes sc = CLDRConfig.getInstance().getStandardCodes();
     36                 CLDRFile english = CLDRConfig.getInstance().getEnglish();
     37                 english.getName(LanguageGroup.uralic.iso);
     38 
     39                 for (LanguageGroup group : LanguageGroup.values()) {
     40                     System.out.println(group + "\t" + group.iso + "\t" + english.getName(group.iso));
     41                 }
     42 
     43                 for (LanguageGroup group : LanguageGroup.values()) {
     44                     for (ULocale loc : LanguageGroup.getLocales(group)) {
     45                         Level level = sc.getLocaleCoverageLevel("cldr", loc.toString());
     46                         System.out.println(group + "\t" + loc + "\t" + loc.getDisplayLanguage(ULocale.ENGLISH) + "\t" + level);
     47                     }
     48                 }
     49                 Set<ULocale> missing = new TreeSet<>();
     50                 for (String s : CLDRConfig.getInstance().getCldrFactory().getAvailableLanguages()) {
     51                     ULocale loc = new ULocale(s);
     52                     loc = loc.getScript().isEmpty() ? loc : new ULocale(loc.getLanguage());
     53                     if (LanguageGroup.getExplicit().contains(loc)) {
     54                         continue;
     55                     }
     56                     missing.add(loc);
     57                 }
     58                 for (ULocale loc : missing) {
     59                     Level level = sc.getLocaleCoverageLevel("cldr", loc.toString());
     60                     if (level == Level.UNDETERMINED) continue;
     61                     System.out.println("?\t" + loc + "\t" + loc.getDisplayLanguage(ULocale.ENGLISH) + "\t" + level);
     62                 }
     63                 for (ULocale loc : missing) {
     64                     Level level = sc.getLocaleCoverageLevel("cldr", loc.toString());
     65                     if (level != Level.UNDETERMINED || "root".equals(loc.toString())) continue;
     66                     System.out.println("?\t" + loc + "\t" + loc.getDisplayLanguage(ULocale.ENGLISH) + "\t" + level);
     67                 }
     68 
     69                 break;
     70             case "chart":
     71                 localesToCheck.addAll(DayPeriods.getAvailable());
     72 
     73                 // add a couple of strange locales for checking
     74                 localesToCheck.add(new ULocale("en-Arab"));
     75                 localesToCheck.add(new ULocale("und"));
     76                 for (ULocale locale : localesToCheck) {
     77                     DayPeriods dayPeriods = DayPeriods.getInstance(locale);
     78                     if (dayPeriods == null) {
     79                         System.out.println("No data for locale; not supported");
     80                         continue;
     81                     }
     82                     System.out.print(locale.getDisplayName(ULocale.ENGLISH));
     83                     for (int i = 0; i < 24 * HOUR; i += HOUR) {
     84                         DayPeriod dayPeriod = dayPeriods.get(i);
     85                         System.out.print("\t" + dayPeriod); //  + "\t" + dayPeriods.getSample(dayPeriod));
     86                     }
     87                     System.out.println();
     88                 }
     89                 break;
     90             case "diff":
     91                 for (ULocale locale : DayPeriods.getAvailable()) {
     92                     DayPeriods dayPeriods = DayPeriods.getInstance(locale);
     93                     DayPeriodsOld dayPeriodsOld = DayPeriodsOld.getInstance(locale);
     94                     System.out.println(locale.getDisplayName(ULocale.ENGLISH));
     95                     for (int i = 0; i < 24 * HOUR; i += HOUR) {
     96                         DayPeriod dayPeriod = dayPeriods.get(i);
     97                         DayPeriodsOld.DayPeriod dayPeriodOld = dayPeriodsOld.get(i);
     98                         String sampleOld = dayPeriodsOld.getSample(dayPeriodOld);
     99                         String sample = dayPeriods.getSample(dayPeriod);
    100                         boolean needLn = false;
    101                         if (!dayPeriodOld.toString().equals(dayPeriod.toString())) {
    102                             System.out.print(locale + "\t" + i / HOUR + "..\t" + dayPeriodOld + "  " + dayPeriod);
    103                             needLn = true;
    104                             if (!sampleOld.equals(sample)) {
    105                                 System.out.print("\t\t" + sampleOld + "\t\t" + sample);
    106                             } else {
    107                                 System.out.print("\t\t" + sample);
    108                             }
    109                         } else if (!sampleOld.equals(sample)) {
    110                             System.out.print(locale + "\t" + i / HOUR + "..\t" + dayPeriod + "\t\t" + sampleOld + "\t\t" + sample);
    111                             needLn = true;
    112                         }
    113                         if (needLn) {
    114                             System.out.println();
    115                         }
    116                     }
    117                 }
    118                 break;
    119             case "html":
    120                 html = true;
    121                 System.out
    122                     .println(
    123                         "<table border=\"1\" bordercolor=\"#888\" cellspacing=\"0\" style=\"border-collapse: collapse; border-color: rgb(136, 136, 136); border-width: 1px;\">\n"
    124                             + "<tbody>");
    125             case "list":
    126                 localesToCheck.addAll(DayPeriods.getAvailable());
    127                 for (ULocale locale : localesToCheck) {
    128                     DayPeriods dayPeriods = DayPeriods.getInstance(locale);
    129                     if (html) {
    130                         System.out.println("<tr><th>" + LanguageGroup.get(locale) +
    131                             "</th><th colSpan='2'><h4>" + locale.getDisplayName(ULocale.ENGLISH) + " (" + locale + ")</h4></tr></th></tr>");
    132                     } else {
    133                         System.out.println(LanguageGroup.get(locale) + "\t" + locale.getDisplayName(ULocale.ENGLISH) + "\t(" + locale + ")");
    134                     }
    135                     int start = 0;
    136                     DayPeriod lastDayPeriod = dayPeriods.get(start * HOUR);
    137                     //Set<DayPeriod> periods = dayPeriods.getDayPeriods();
    138                     for (int i = 1; i < 24; ++i) {
    139                         DayPeriod dayPeriod = dayPeriods.get(i * HOUR);
    140                         if (dayPeriod != lastDayPeriod) {
    141                             show(locale, start, i - 1, dayPeriods, lastDayPeriod);
    142                             lastDayPeriod = dayPeriod;
    143                             start = i;
    144                         }
    145                     }
    146                     show(locale, start, 23, dayPeriods, lastDayPeriod);
    147                 }
    148                 if (html) {
    149                     System.out.println("</tbody>\n</table>");
    150                 }
    151                 break;
    152             }
    153         }
    154     }
    155 
    156     private static void show(ULocale locale, int start, int limit, DayPeriods dayPeriods, DayPeriod dayPeriod) {
    157         if (html) {
    158             System.out.println("<tr><td>" + format(start) + ":00  " + format(limit) + ":59</td><td>" + dayPeriod + "</td><td>"
    159                 + dayPeriods.getSample(dayPeriod) + "</td></tr>");
    160         } else {
    161             System.out.println("||" + LanguageGroup.get(locale) + "||" + locale.getDisplayName(ULocale.ENGLISH) + "||" + locale + "||" + dayPeriod + "||"
    162                 + start + ":00  " + limit + ":59||" + dayPeriods.getSample(dayPeriod) + "||");
    163         }
    164     }
    165 
    166     private static String format(int start) {
    167         return start < 10 ? "0" + start : "" + start;
    168     }
    169 }
    170