• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.tool;
2 
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Locale;
6 import java.util.Map;
7 import java.util.Map.Entry;
8 import java.util.Set;
9 import java.util.TreeMap;
10 import java.util.TreeSet;
11 
12 import org.unicode.cldr.draft.FileUtilities;
13 import org.unicode.cldr.util.ChainedMap;
14 import org.unicode.cldr.util.ChainedMap.M5;
15 import org.unicode.cldr.util.CldrUtility;
16 import org.unicode.cldr.util.StandardCodes;
17 import org.unicode.cldr.util.StandardCodes.LstrField;
18 import org.unicode.cldr.util.StandardCodes.LstrType;
19 import org.unicode.cldr.util.StringRange;
20 import org.unicode.cldr.util.StringRange.Adder;
21 import org.unicode.cldr.util.SupplementalDataInfo;
22 
23 import com.google.common.base.Joiner;
24 import com.google.common.base.Splitter;
25 import com.ibm.icu.lang.CharSequences;
26 
27 @SuppressWarnings("deprecation")
28 public class CompareIso3166_1Status {
29 
30     private static final Joiner SPACE_JOINER = Joiner.on(" ");
31 
32     public enum Iso3166Status {
33         officially_assigned, private_use, exceptionally_reserved, indeterminately_reserved, transitionally_reserved, formerly_used, out_of_scope
34     }
35 
36     public enum CldrStatus {
37         region, macroregion, deprecated, privateUse, unused,
38     }
39 
main(String[] args)40     public static void main(String[] args) {
41 
42         Map<String, Iso3166Status> isoStatus = new TreeMap<>();
43         Map<String, String> isoDescription = new TreeMap<>();
44         Splitter semi = Splitter.on(';').trimResults();
45         for (String line : FileUtilities.in(StandardCodes.class, "data/external/iso_3166_status.txt")) {
46             if (line.startsWith("#")) continue;
47             List<String> parts = semi.splitToList(line);
48             // AC ; Exceptionally reserved  ; Refers to the United Nations and reserved by the ISO 3166 Maintenance Agency.
49             final String regionCode = parts.get(0);
50             isoStatus.put(regionCode, Iso3166Status.valueOf(parts.get(1).toLowerCase(Locale.ROOT).replace(' ', '_')));
51             isoDescription.put(regionCode, parts.get(1));
52         }
53 
54         Map<String, Map<LstrField, String>> lstregRegions = StandardCodes.getEnumLstreg().get(LstrType.region);
55         Map<String, Map<LstrField, String>> lstregRegionsRaw = StandardCodes.getLstregEnumRaw().get(LstrType.region);
56 
57         Set<String> seen = new HashSet<>();
58         M5<CldrStatus, CldrStatus, Iso3166Status, String, Boolean> ordered = ChainedMap.of(
59             new TreeMap<CldrStatus, Object>(),
60             new TreeMap<CldrStatus, Object>(),
61             new TreeMap<Iso3166Status, Object>(),
62             new TreeMap<String, Object>(),
63             Boolean.class);
64 
65         Map<String, CldrStatus> cldrStatus = new TreeMap<>();
66         Map<String, CldrStatus> bcp47Status = new TreeMap<>();
67         for (Entry<String, Map<LstrField, String>> entry : lstregRegions.entrySet()) {
68             String regionCode = entry.getKey();
69             final Map<LstrField, String> cldrData = entry.getValue();
70             String description = setStatus(regionCode, cldrData, cldrStatus, true);
71 
72             final Map<LstrField, String> bcp47Data = lstregRegionsRaw.get(regionCode);
73             setStatus(regionCode, bcp47Data, bcp47Status, false);
74 
75             final Iso3166Status isoStatus2 = CldrUtility.ifNull(isoStatus.get(regionCode), Iso3166Status.out_of_scope);
76             ordered.put(cldrStatus.get(regionCode), bcp47Status.get(regionCode), isoStatus2, regionCode, Boolean.TRUE);
77             System.out.println(regionCode
78                 + "\t" + cldrStatus.get(regionCode)
79                 + "\t" + bcp47Status.get(regionCode)
80                 + "\t" + isoStatus2
81                 //+ "\t" + description
82                 + "\t" + bcp47Data);
83             seen.add(regionCode);
84         }
85 
86         for (Entry<String, Map<LstrField, String>> entry : lstregRegionsRaw.entrySet()) {
87             String regionCode = entry.getKey();
88             final Map<LstrField, String> bcp47Data = entry.getValue();
89             setStatus(regionCode, bcp47Data, bcp47Status, false);
90         }
91 
92         Set<String> missing = new TreeSet<>(isoStatus.keySet());
93         missing.removeAll(seen);
94         for (String regionCode : missing) {
95             Iso3166Status isoStatus2 = isoStatus.get(regionCode);
96             cldrStatus.put(regionCode, CldrStatus.unused);
97             bcp47Status.put(regionCode, CldrStatus.unused);
98             ordered.put(cldrStatus.get(regionCode), bcp47Status.get(regionCode), isoStatus2, regionCode, Boolean.TRUE);
99             System.out.println(regionCode
100                 + "\t" + cldrStatus.get(regionCode)
101                 + "\t" + bcp47Status.get(regionCode)
102                 + "\t" + isoStatus2
103                 + "\t" + isoDescription.get(regionCode));
104         }
105         System.out.println();
106 
107         for (Entry<CldrStatus, Map<CldrStatus, Map<Iso3166Status, Map<String, Boolean>>>> entry : ordered) {
108             CldrStatus cldrStatus2 = entry.getKey();
109             for (Entry<CldrStatus, Map<Iso3166Status, Map<String, Boolean>>> entry2 : entry.getValue().entrySet()) {
110                 CldrStatus bcp47Status2 = entry2.getKey();
111                 for (Entry<Iso3166Status, Map<String, Boolean>> entry3 : entry2.getValue().entrySet()) {
112                     Iso3166Status isoStatus2 = entry3.getKey();
113                     Set<String> codes = entry3.getValue().keySet();
114                     System.out.println("||\t" + cldrStatus2
115                         + "\t||\t" + bcp47Status2
116                         + "\t||\t" + isoStatus2
117                         + "\t||\t" + codes.size()
118                         + "\t||\t" + compactDisplay(codes)
119                         + "\t||");
120                 }
121             }
122         }
123     }
124 
compactDisplay(Set<String> codes)125     private static String compactDisplay(Set<String> codes) {
126         final StringBuilder b = new StringBuilder();
127         Adder myAdder = new Adder() { // for testing: doesn't do quoting, etc
128             @Override
129             public void add(String start, String end) {
130                 if (b.length() != 0) {
131                     b.append(' ');
132                 }
133                 b.append(start);
134                 if (end != null) {
135                     b.append('-').append(end);
136                 }
137             }
138         };
139         StringRange.compact(codes, myAdder, false);
140         return b.toString();
141     }
142 
143     // guaranteed ascii!
toNumber(String code)144     private static int toNumber(String code) {
145         int num = 0;
146         for (int cp : CharSequences.codePoints(code)) {
147             num <<= 7;
148             num += cp;
149         }
150         return num;
151     }
152 
fromNumber(int cp)153     private static String fromNumber(int cp) {
154         StringBuilder b = new StringBuilder();
155         while (cp != 0) {
156             int part = cp & 0x7F;
157             b.insert(0, (char) part);
158             cp >>= 7;
159         }
160         return b.toString();
161     }
162 
163     static SupplementalDataInfo SDI = SupplementalDataInfo.getInstance();
164 
setStatus(String regionCode, final Map<LstrField, String> cldrData, Map<String, CldrStatus> cldrStatus, boolean showMacro)165     private static String setStatus(String regionCode, final Map<LstrField, String> cldrData, Map<String, CldrStatus> cldrStatus, boolean showMacro) {
166         String description = cldrData.get(LstrField.Description);
167 
168         if (cldrData.containsKey(LstrField.Deprecated)) {
169             cldrStatus.put(regionCode, CldrStatus.deprecated);
170         } else if (description.equalsIgnoreCase("Private use")) {
171             cldrStatus.put(regionCode, CldrStatus.privateUse);
172         } else if (showMacro && SDI.getContained(regionCode) != null) {
173             cldrStatus.put(regionCode, CldrStatus.macroregion);
174         } else {
175             cldrStatus.put(regionCode, CldrStatus.region);
176         }
177         return description;
178     }
179 }
180