Path: blob/master/test/jdk/sun/util/calendar/zi/Simple.java
41153 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.io.BufferedWriter;24import java.io.File;25import java.io.FileWriter;26import java.io.IOException;27import java.util.HashMap;28import java.util.List;29import java.util.Map;30import java.util.Set;31import java.util.SortedMap;32import java.util.TreeMap;33import java.util.TreeSet;3435/**36* <code>Simple</code> generates TimeZoneData, which had been used as internal37* data of TimeZone before J2SDK1.3.38* Since J2SDK1.4 doesn't need TimeZoneData, this class is for maintenance39* of old JDK release.40*/41class Simple extends BackEnd {4243/**44* Zone records which are applied for given year.45*/46private static Map<String,ZoneRec> lastZoneRecs = new HashMap<>();4748/**49* Rule records which are applied for given year.50*/51private static Map<String,List<RuleRec>> lastRules = new TreeMap<>();5253/**54* zone IDs sorted by their GMT offsets. If zone's GMT55* offset will change in the future, its last known offset is56* used.57*/58private SortedMap<Integer, Set<String>> zonesByOffset = new TreeMap<>();5960/**61* Sets last Rule records and Zone records for given timezone to62* each Map.63*64* @param tz Timezone object for each zone65* @return always 066*/67int processZoneinfo(Timezone tz) {68String zonename = tz.getName();6970lastRules.put(zonename, tz.getLastRules());71lastZoneRecs.put(zonename, tz.getLastZoneRec());7273// Populate zonesByOffset. (Zones that will change their74// GMT offsets are also added to zonesByOffset here.)75int lastKnownOffset = tz.getRawOffset();76Set<String> set = zonesByOffset.get(lastKnownOffset);77if (set == null) {78set = new TreeSet<>();79zonesByOffset.put(lastKnownOffset, set);80}81set.add(zonename);8283return 0;84}8586/**87* Generates TimeZoneData to output SimpleTimeZone data.88* @param map Mappings object which is generated by {@link Main#compile}.89* @return 0 if no error occurred, otherwise 1.90*/91int generateSrc(Mappings map) {92try {93File outD = new File(Main.getOutputDir());94outD.mkdirs();9596FileWriter fw =97new FileWriter(new File(outD, "TimeZoneData.java"), false);98BufferedWriter out = new BufferedWriter(fw);99100out.write("import java.util.SimpleTimeZone;\n\n");101out.write(" static SimpleTimeZone zones[] = {\n");102103Map<String,String> a = map.getAliases();104List<Integer> roi = map.getRawOffsetsIndex();105List<Set<String>> roit = map.getRawOffsetsIndexTable();106107int index = 0;108for (int offset : zonesByOffset.keySet()) {109int o = roi.get(index);110Set<String> set = zonesByOffset.get(offset);111if (offset == o) {112// Merge aliases into zonesByOffset113set.addAll(roit.get(index));114}115index++;116117for (String key : set) {118ZoneRec zrec;119String realname;120List<RuleRec> stz;121if ((realname = a.get(key)) != null) {122// if this alias is not targeted, ignore it.123if (!Zone.isTargetZone(key)) {124continue;125}126stz = lastRules.get(realname);127zrec = lastZoneRecs.get(realname);128} else {129stz = lastRules.get(key);130zrec = lastZoneRecs.get(key);131}132133out.write("\t//--------------------------------------------------------------------\n");134String s = Time.toFormedString(offset);135out.write("\tnew SimpleTimeZone(" +136Time.toFormedString(offset) + ", \"" + key + "\"");137if (realname != null) {138out.write(" /* " + realname + " */");139}140141if (stz == null) {142out.write("),\n");143} else {144RuleRec rr0 = stz.get(0);145RuleRec rr1 = stz.get(1);146147out.write(",\n\t " + Month.toString(rr0.getMonthNum()) +148", " + rr0.getDay().getDayForSimpleTimeZone() + ", " +149rr0.getDay().getDayOfWeekForSimpleTimeZone() + ", " +150Time.toFormedString((int)rr0.getTime().getTime()) + ", " +151rr0.getTime().getTypeForSimpleTimeZone() + ",\n" +152153"\t " + Month.toString(rr1.getMonthNum()) + ", " +154rr1.getDay().getDayForSimpleTimeZone() + ", " +155rr1.getDay().getDayOfWeekForSimpleTimeZone() + ", " +156Time.toFormedString((int)rr1.getTime().getTime())+ ", " +157rr1.getTime().getTypeForSimpleTimeZone() + ",\n" +158159"\t " + Time.toFormedString(rr0.getSave()) + "),\n");160161out.write("\t// " + rr0.getLine() + "\n");162out.write("\t// " + rr1.getLine() + "\n");163}164165String zline = zrec.getLine();166if (zline.indexOf("Zone") == -1) {167zline = "Zone " + key + "\t" + zline.trim();168}169out.write("\t// " + zline + "\n");170}171}172out.write(" };\n");173174out.close();175fw.close();176} catch(IOException e) {177Main.panic("IO error: "+e.getMessage());178return 1;179}180181return 0;182}183}184185186