1
0

GenerateOverview.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import com.grack.nanojson.JsonAppendableWriter;
  2. import com.grack.nanojson.JsonObject;
  3. import com.grack.nanojson.JsonParser;
  4. import com.grack.nanojson.JsonWriter;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.util.*;
  9. public class GenerateOverview {
  10. public static void main(String[] args) throws Exception {
  11. Map<String, Map<String, Collection<String>>> outMap = new TreeMap<>(new Comparator<String>() {
  12. @Override
  13. public int compare(String o1, String o2) {
  14. return o1.compareToIgnoreCase(o2);
  15. }
  16. });
  17. for (String unitName : Arrays.asList("seconds", "minutes", "hours", "days", "weeks", "months", "years")) {
  18. JsonObject object = JsonParser.object().from(new FileInputStream(new File("timeago-parser/raw/times/" + unitName + ".json")));
  19. for (Map.Entry<String, Object> timeKeyValue : object.entrySet()) {
  20. JsonObject timeObject = (JsonObject) timeKeyValue.getValue();
  21. for (Map.Entry<String, Object> langsKeyValue : timeObject.entrySet()) {
  22. String langKey = langsKeyValue.getKey();
  23. String langValue = langsKeyValue.getValue().toString();
  24. Map<String, Collection<String>> langUnitsMap;
  25. if (outMap.containsKey(langKey)) {
  26. langUnitsMap = outMap.get(langKey);
  27. } else {
  28. langUnitsMap = new TreeMap<>(Utils.compareByUnitName());
  29. outMap.put(langKey, langUnitsMap);
  30. }
  31. Collection<String> langUnitListValues;
  32. if (langUnitsMap.containsKey(unitName)) {
  33. langUnitListValues = langUnitsMap.get(unitName);
  34. } else {
  35. langUnitListValues = new TreeSet<>(Utils.compareByNumber());
  36. langUnitsMap.put(unitName, langUnitListValues);
  37. }
  38. langUnitListValues.add(langValue);
  39. }
  40. }
  41. }
  42. writeMapTo(outMap, JsonWriter.indent(" ").on(new FileOutputStream(new File("timeago-parser/raw/overview.json"))));
  43. }
  44. public static void writeMapTo(Map<String, Map<String, Collection<String>>> outMap, JsonAppendableWriter out) {
  45. out.object();
  46. for (Map.Entry<String, Map<String, Collection<String>>> langMapEntry : outMap.entrySet()) {
  47. final String langName = langMapEntry.getKey();
  48. out.object(langName);
  49. for (Map.Entry<String, Collection<String>> langValuesEntry : langMapEntry.getValue().entrySet()) {
  50. final String unitName = langValuesEntry.getKey();
  51. out.array(unitName);
  52. for (String timeValue : langValuesEntry.getValue()) out.value(timeValue);
  53. out.end();
  54. }
  55. out.end();
  56. }
  57. out.end().done();
  58. }
  59. }