diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/util/PathHeader.java b/tools/cldr-code/src/main/java/org/unicode/cldr/util/PathHeader.java index a64137d660f..f8587ad751d 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/util/PathHeader.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/util/PathHeader.java @@ -18,7 +18,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -232,6 +231,7 @@ public enum PageId { Length(SectionId.Units), Area(SectionId.Units), Volume_Metric(SectionId.Units, "Volume Metric"), + Volume_US(SectionId.Units, "Volume US"), Volume_Other(SectionId.Units, "Volume Other"), SpeedAcceleration(SectionId.Units, "Speed and Acceleration"), MassWeight(SectionId.Units, "Mass and Weight"), @@ -240,6 +240,9 @@ public enum PageId { Weather(SectionId.Units), Digital(SectionId.Units), Coordinates(SectionId.Units), + OtherUnitsMetric(SectionId.Units, "Other Units Metric"), + OtherUnitsMetricPer(SectionId.Units, "Other Units Metric Per"), + OtherUnitsUS(SectionId.Units, "Other Units US"), OtherUnits(SectionId.Units, "Other Units"), CompoundUnits(SectionId.Units, "Compound Units"), @@ -860,28 +863,7 @@ public static Relation getSectionIdsToPageIds() { return SectionIdToPageIds; } - /** - * Return the names for Sections and Pages that are defined, for display in menus. Both are - * ordered. - * - * @deprecated Use getSectionIdsToPageIds - */ - @Deprecated - public static LinkedHashMap> getSectionsToPages() { - LinkedHashMap> sectionsToPages = new LinkedHashMap<>(); - for (PageId pageId : PageId.values()) { - String sectionId2 = pageId.getSectionId().toString(); - Set pages = - sectionsToPages.computeIfAbsent(sectionId2, k -> new LinkedHashSet<>()); - pages.add(pageId.toString()); - } - return sectionsToPages; - } - - /** - * @deprecated, use the filterCldr with the section/page ids. - */ - public Iterable filterCldr(String section, String page, CLDRFile file) { + public Iterable filterCldr(SectionId section, PageId page, CLDRFile file) { return new FilteredIterable(section, page, file); } @@ -2241,6 +2223,8 @@ private static String fix(String input, int orderIn) { if (functionStart < 0) { if ("Volume".equals(input)) { return getVolumePageId(args.value[0] /* path */).toString(); + } else if ("Other Units".equals(input)) { + return getOtherUnitsPageId(args.value[0] /* path */).toString(); } return input; } @@ -2262,10 +2246,42 @@ private static String fix(String input, int orderIn) { } } - private static Set METRIC = + private static Set METRIC_UNITS = Set.of(UnitConverter.UnitSystem.metric, UnitConverter.UnitSystem.metric_adjacent); + private static Set US_UNITS = + Set.of(UnitConverter.UnitSystem.ussystem); + private static PageId getVolumePageId(String path) { + final String shortUnitId = getShortUnitId(path); + if (isSystemUnit(shortUnitId, METRIC_UNITS)) { + return PageId.Volume_Metric; + } else { + return isSystemUnit(shortUnitId, US_UNITS) ? PageId.Volume_US : PageId.Volume_Other; + } + } + + private static PageId getOtherUnitsPageId(String path) { + String shortUnitId = getShortUnitId(path); + if (isSystemUnit(shortUnitId, METRIC_UNITS)) { + return shortUnitId.contains("per") + ? PageId.OtherUnitsMetricPer + : PageId.OtherUnitsMetric; + } else { + return isSystemUnit(shortUnitId, US_UNITS) + ? PageId.OtherUnitsUS + : PageId.OtherUnits; + } + } + + private static boolean isSystemUnit( + String shortUnitId, Set system) { + final UnitConverter uc = supplementalDataInfo.getUnitConverter(); + final Set systems = uc.getSystemsEnum(shortUnitId); + return !Collections.disjoint(system, systems); + } + + private static String getShortUnitId(String path) { // Extract the unit from the path. For example, if path is // //ldml/units/unitLength[@type="narrow"]/unit[@type="volume-cubic-kilometer"]/displayName // then extract "volume-cubic-kilometer" which is the long unit id @@ -2276,12 +2292,7 @@ private static PageId getVolumePageId(String path) { } final UnitConverter uc = supplementalDataInfo.getUnitConverter(); // Convert, for example, "volume-cubic-kilometer" to "cubic-kilometer" - final String shortUnitId = uc.getShortId(longUnitId); - if (!Collections.disjoint(METRIC, uc.getSystemsEnum(shortUnitId))) { - return PageId.Volume_Metric; - } else { - return PageId.Volume_Other; - } + return uc.getShortId(longUnitId); } /** diff --git a/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestPathHeader.java b/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestPathHeader.java index e2f4ee43106..2ec0c5543de 100644 --- a/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestPathHeader.java +++ b/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestPathHeader.java @@ -1090,12 +1090,11 @@ public void TestZ() { for (String item : threeLevel) { logln(item); } - LinkedHashMap> sectionsToPages = - org.unicode.cldr.util.PathHeader.Factory.getSectionsToPages(); - logln("\nMenus:\t" + sectionsToPages.size()); - for (Entry> item : sectionsToPages.entrySet()) { - final String section = item.getKey(); - for (String page : item.getValue()) { + Relation s2p = PathHeader.Factory.getSectionIdsToPageIds(); + logln("\nMenus:\t" + s2p.size()); + for (Entry> sectionAndPages : s2p.keyValuesSet()) { + final SectionId section = sectionAndPages.getKey(); + for (PageId page : sectionAndPages.getValue()) { logln("\t" + section + "\t" + page); int count = 0; for (String path : pathHeaderFactory.filterCldr(section, page, english)) {