1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.time.format;
  26 
  27 import java.util.HashMap;
  28 import java.util.Locale;
  29 import java.util.Map;
  30 
  31 /**
  32  * A helper class to map a zone name to metazone and back to the
  33  * appropriate zone id for the particular locale.
  34  * <p>
  35  * The zid<->metazone mappings are based on CLDR metaZones.xml.
  36  * The alias mappings are based on Link entries in tzdb data files and
  37  * CLDR's supplementalMetadata.xml.
  38  */
  39 class ZoneName {
  40 
  41     public static String toZid(String zid, Locale locale) {
  42         String mzone = zidToMzone.get(zid);
  43         if (mzone == null && aliases.containsKey(zid)) {
  44             zid = aliases.get(zid);
  45             mzone = zidToMzone.get(zid);
  46         }
  47         if (mzone != null) {
  48             Map<String, String> map = mzoneToZidL.get(mzone);
  49             if (map != null && map.containsKey(locale.getCountry())) {
  50                 zid = map.get(locale.getCountry());
  51             } else {
  52                 zid = mzoneToZid.get(mzone);
  53             }
  54         }
  55         return toZid(zid);
  56     }
  57 
  58     public static String toZid(String zid) {
  59         if (aliases.containsKey(zid)) {
  60             return aliases.get(zid);
  61         }
  62         return zid;
  63     }
  64 
  65     private static final String[] zidMap = new String[] {
  66         // From metaZones.xml
  67 %%%%ZIDMAP%%%%
  68 
  69         // From tzdb
  70         "Africa/Khartoum", "Africa_Central", "Africa/Maputo", // tzdata2017c
  71         "Africa/Windhoek", "Africa_Central", "Africa/Maputo", // tzdata2017c
  72         "Africa/Sao_Tome", "Africa_Western", "Africa/Lagos",  // tzdata2018c
  73     };
  74     private static final String[] mzoneMap = new String[] {
  75         // From metaZones.xml
  76 %%%%MZONEMAP%%%%
  77 
  78         // From tzdb
  79         "Africa_Western", "ST", "Africa/Sao_Tome", // tzdata2018c
  80     };
  81     private static final String[] aliasMap = new String[] {
  82         // From supplementalMetadata.xml
  83 %%%%DEPRECATED%%%%
  84 
  85         // From tzdb
  86 %%%%TZDATALINK%%%%
  87     };
  88 
  89     private static final Map<String, String> zidToMzone = new HashMap<>();
  90     private static final Map<String, String> mzoneToZid = new HashMap<>();
  91     private static final Map<String, Map<String, String>> mzoneToZidL = new HashMap<>();
  92     private static final Map<String, String> aliases = new HashMap<>();
  93 
  94     static {
  95         for (int i = 0; i < zidMap.length; i += 3) {
  96             zidToMzone.put(zidMap[i], zidMap[i + 1]);
  97             mzoneToZid.put(zidMap[i + 1], zidMap[i + 2]);
  98         }
  99 
 100         for (int i = 0; i < mzoneMap.length; i += 3) {
 101             String mzone = mzoneMap[i];
 102             Map<String, String> map = mzoneToZidL.get(mzone);
 103             if (map == null) {
 104                 map = new HashMap<>();
 105                 mzoneToZidL.put(mzone, map);
 106             }
 107             map.put(mzoneMap[i + 1], mzoneMap[i + 2]);
 108         }
 109 
 110         for (int i = 0; i < aliasMap.length; i += 2) {
 111             aliases.put(aliasMap[i], aliasMap[i + 1]);
 112         }
 113     }
 114 }