Path: blob/master/test/jdk/sun/util/calendar/zi/Mappings.java
51715 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;24import java.util.HashMap;25import java.util.LinkedList;26import java.util.List;27import java.util.Map;28import java.util.Set;29import java.util.TreeMap;30import java.util.TreeSet;3132/**33* <code>Mappings</code> generates two Maps and a List which are used by34* javazic BackEnd.35*36* @since 1.437*/38class Mappings {39// All aliases specified by Link statements. It's alias name to40// real name mappings.41private Map<String,String> aliases;4243private List<Integer> rawOffsetsIndex;4445private List<Set<String>> rawOffsetsIndexTable;4647// Zone names to be excluded from rawOffset table. Those have GMT48// offsets to change some future time.49private List<String> excludeList;5051/**52* Constructor creates some necessary instances.53*/54Mappings() {55aliases = new TreeMap<String,String>();56rawOffsetsIndex = new LinkedList<Integer>();57rawOffsetsIndexTable = new LinkedList<Set<String>>();58}5960/**61* Generates aliases and rawOffsets tables.62* @param zi a Zoneinfo containing Zones63*/64void add(Zoneinfo zi) {65Map<String,Zone> zones = zi.getZones();6667for (String zoneName : zones.keySet()) {68Zone zone = zones.get(zoneName);69String zonename = zone.getName();70int rawOffset = zone.get(zone.size()-1).getGmtOffset();7172// If the GMT offset of this Zone will change in some73// future time, this Zone is added to the exclude list.74boolean isExcluded = false;75for (int i = 0; i < zone.size(); i++) {76ZoneRec zrec = zone.get(i);77if ((zrec.getGmtOffset() != rawOffset)78&& (zrec.getUntilTime(0) > Time.getCurrentTime())) {79if (excludeList == null) {80excludeList = new ArrayList<String>();81}82excludeList.add(zone.getName());83isExcluded = true;84break;85}86}8788if (!rawOffsetsIndex.contains(new Integer(rawOffset))) {89// Find the index to insert this raw offset zones90int n = rawOffsetsIndex.size();91int i;92for (i = 0; i < n; i++) {93if (rawOffsetsIndex.get(i) > rawOffset) {94break;95}96}97rawOffsetsIndex.add(i, rawOffset);9899Set<String> perRawOffset = new TreeSet<String>();100if (!isExcluded) {101perRawOffset.add(zonename);102}103rawOffsetsIndexTable.add(i, perRawOffset);104} else if (!isExcluded) {105int i = rawOffsetsIndex.indexOf(new Integer(rawOffset));106Set<String> perRawOffset = rawOffsetsIndexTable.get(i);107perRawOffset.add(zonename);108}109}110111Map<String,String> a = zi.getAliases();112// If there are time zone names which refer to any of the113// excluded zones, add those names to the excluded list.114if (excludeList != null) {115for (String zoneName : a.keySet()) {116String realname = a.get(zoneName);117if (excludeList.contains(realname)) {118excludeList.add(zoneName);119}120}121}122aliases.putAll(a);123}124125/**126* Adds valid aliases to one of per-RawOffset table and removes127* invalid aliases from aliases List. Aliases referring to128* excluded zones are not added to a per-RawOffset table.129*/130void resolve() {131int index = rawOffsetsIndexTable.size();132List<String> toBeRemoved = new ArrayList<String>();133for (String key : aliases.keySet()) {134boolean validname = false;135for (int j = 0; j < index; j++) {136Set<String> perRO = rawOffsetsIndexTable.get(j);137boolean isExcluded = (excludeList == null) ?138false : excludeList.contains(key);139140if ((perRO.contains(aliases.get(key)) || isExcluded)141&& Zone.isTargetZone(key)) {142validname = true;143if (!isExcluded) {144perRO.add(key);145Main.info("Alias <"+key+"> added to the list.");146}147break;148}149}150151if (!validname) {152Main.info("Alias <"+key+"> removed from the list.");153toBeRemoved.add(key);154}155}156157// Remove zones, if any, from the list.158for (String key : toBeRemoved) {159aliases.remove(key);160}161// Eliminate any alias-to-alias mappings. For example, if162// there are A->B and B->C, A->B is changed to A->C.163Map<String, String> newMap = new HashMap<String, String>();164for (String key : aliases.keySet()) {165String realid = aliases.get(key);166String leaf = realid;167while (aliases.get(leaf) != null) {168leaf = aliases.get(leaf);169}170if (!realid.equals(leaf)) {171newMap.put(key, leaf);172}173}174aliases.putAll(newMap);175}176177Map<String,String> getAliases() {178return(aliases);179}180181List<Integer> getRawOffsetsIndex() {182return(rawOffsetsIndex);183}184185List<Set<String>> getRawOffsetsIndexTable() {186return(rawOffsetsIndexTable);187}188189List<String> getExcludeList() {190return excludeList;191}192}193194195