1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.libcore.timezone.tzlookup.zonetree;
17 
18 import com.android.libcore.timezone.countryzones.proto.CountryZonesFile.Country;
19 import com.android.libcore.timezone.countryzones.proto.CountryZonesFile.CountryZones;
20 import com.android.libcore.timezone.tzlookup.CountryZonesFileSupport;
21 import com.android.libcore.timezone.tzlookup.TzLookupGenerator;
22 
23 import java.io.IOException;
24 import java.time.Instant;
25 import java.util.Collections;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.function.Function;
29 import java.util.stream.Collectors;
30 
31 import static java.util.stream.Collectors.toSet;
32 
33 /**
34  * A tool for building and visualizing one or more {@link CountryZoneTree} instances.
35  */
36 public final class UniqueZonesVisualizer {
37 
main(String[] args)38     public static void main(String[] args) throws Exception {
39         if (args.length < 3) {
40             System.err.println("Usage:");
41             System.err.println(UniqueZonesVisualizer.class
42                     + " <countryzones.txt file> [country code|_] <output dir> [nocompress]");
43             System.err.println("_ means \"all countries\"");
44             System.exit(1);
45         }
46         String countryZonesTextFile = args[0];
47         String countryCodeArg = args[1];
48         String outputDir = args[2];
49         boolean compressTree = args.length == 3 || !"nocompress".equals(args[3]);
50 
51         CountryZones countryZones =
52                 CountryZonesFileSupport.parseCountryZonesTextFile(countryZonesTextFile);
53         UniqueZonesVisualizer zonesCalculator = new UniqueZonesVisualizer(countryZones);
54 
55         Set<String> countryCodes;
56         if (countryCodeArg.equals("_")) {
57             countryCodes = countryZones.getCountriesList().stream()
58                     .map(Country::getIsoCode)
59                     .collect(toSet());
60         } else {
61             countryCodes = Collections.singleton(countryCodeArg);
62         }
63 
64         Instant startInclusive = TzLookupGenerator.ZONE_USAGE_CALCS_START;
65         Instant endExclusive = TzLookupGenerator.ZONE_USAGE_CALCS_END;
66         for (String countryCode : countryCodes) {
67             zonesCalculator.createGraphvizFile(countryCode, startInclusive,
68                     endExclusive, compressTree, outputDir + "/" + countryCode + ".gv");
69         }
70     }
71 
72     private final Map<String, Country> countryMap;
73 
UniqueZonesVisualizer(CountryZones countryZones)74     private UniqueZonesVisualizer(CountryZones countryZones) {
75         this.countryMap = countryZones.getCountriesList().stream()
76                 .collect(Collectors.toMap(Country::getIsoCode, Function.identity()));
77     }
78 
createGraphvizFile(String countryIso, Instant startInclusive, Instant endExclusive, boolean compress, String outputFile)79     private void createGraphvizFile(String countryIso, Instant startInclusive, Instant endExclusive,
80             boolean compress, String outputFile) throws IOException {
81         Country country = countryMap.get(countryIso);
82         CountryZoneTree tree =
83                 CountryZoneTree.create(country, startInclusive, endExclusive, compress);
84         tree.createGraphvizFile(outputFile);
85     }
86 }
87 
88