Path: blob/master/test/jdk/sun/util/calendar/zi/Zone.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.BufferedReader;24import java.io.FileReader;25import java.io.FileNotFoundException;26import java.io.IOException;27import java.util.ArrayList;28import java.util.HashMap;29import java.util.HashSet;30import java.util.List;31import java.util.Map;32import java.util.Set;33import java.util.StringTokenizer;3435/**36* Zone holds information corresponding to a "Zone" part of a time37* zone definition file.38*39* @since 1.440*/41class Zone {42// zone name (e.g., "America/Los_Angeles")43private String name;4445// zone records46private List<ZoneRec> list;4748// target zone names for this compilation49private static Set<String> targetZones;5051/**52* Constructs a Zone with the specified zone name.53* @param name the zone name54*/55Zone(String name) {56this.name = name;57list = new ArrayList<ZoneRec>();58}5960/**61* Reads time zone names to be generated, called "target zone62* name", from the specified text file and creats an internal hash63* table to keep those names. It's assumed that one text line64* contains a zone name or comments if it starts with65* '#'. Comments can't follow a zone name in a single line.66* @param fileName the text file name67*/68static void readZoneNames(String fileName) {69if (fileName == null) {70return;71}72BufferedReader in = null;73try {74FileReader fr = new FileReader(fileName);75in = new BufferedReader(fr);76} catch (FileNotFoundException e) {77Main.panic("can't open file: " + fileName);78}79targetZones = new HashSet<String>();80String line;8182try {83while ((line = in.readLine()) != null) {84line = line.trim();85if (line.length() == 0 || line.charAt(0) == '#') {86continue;87}88if (!targetZones.add(line)) {89Main.warning("duplicated target zone name: " + line);90}91}92in.close();93} catch (IOException e) {94Main.panic("IO error: "+e.getMessage());95}96}9798/**99* Determines whether the specified zone is one of the target zones.100* If no target zones are specified, this method always returns101* true for any zone name.102* @param zoneName the zone name103* @return true if the specified name is a target zone.104*/105static boolean isTargetZone(String zoneName) {106if (targetZones == null) {107return true;108}109return targetZones.contains(zoneName);110}111112/**113* Forces to add "MET" to the target zone table. This is because114* there is a conflict between Java zone name "WET" and Olson zone115* name.116*/117static void addMET() {118if (targetZones != null) {119targetZones.add("MET");120}121}122123/**124* @return the zone name125*/126String getName() {127return name;128}129130/**131* Adds the specified zone record to the zone record list.132*/133void add(ZoneRec rec) {134list.add(rec);135}136137/**138* @param index the index at which the zone record in the list is returned.139* @return the zone record specified by the index.140*/141ZoneRec get(int index) {142return list.get(index);143}144145/**146* @return the size of the zone record list147*/148int size() {149return list.size();150}151152/**153* Resolves the reference to a rule in each zone record.154* @param zi the Zoneinfo object with which the rule reference is155* resolved.156*/157void resolve(Zoneinfo zi) {158for (int i = 0; i < list.size(); i++) {159ZoneRec rec = list.get(i);160rec.resolve(zi);161}162}163}164165166