001: /*
002: * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.javazic;
027:
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.HashSet;
031: import java.util.Iterator;
032: import java.util.LinkedList;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.Set;
036: import java.util.TreeMap;
037: import java.util.TreeSet;
038:
039: /**
040: * <code>Mappings</code> generates two Maps and a List which are used by
041: * javazic BackEnd.
042: *
043: * @since 1.4
044: */
045: class Mappings {
046: // All aliases specified by Link statements. It's alias name to
047: // real name mappings.
048: private Map<String, String> aliases;
049:
050: private List<Integer> rawOffsetsIndex;
051:
052: private List<Set<String>> rawOffsetsIndexTable;
053:
054: // Zone names to be excluded from rawOffset table. Those have GMT
055: // offsets to change some future time.
056: private List<String> excludeList;
057:
058: /**
059: * Constructor creates some necessary instances.
060: */
061: Mappings() {
062: aliases = new TreeMap<String, String>();
063: rawOffsetsIndex = new LinkedList<Integer>();
064: rawOffsetsIndexTable = new LinkedList<Set<String>>();
065: }
066:
067: /**
068: * Generates aliases and rawOffsets tables.
069: * @param zi a Zoneinfo containing Zones
070: */
071: void add(Zoneinfo zi) {
072: Map<String, Zone> zones = zi.getZones();
073:
074: for (String zoneName : zones.keySet()) {
075: Zone zone = zones.get(zoneName);
076: String zonename = zone.getName();
077: int rawOffset = zone.get(zone.size() - 1).getGmtOffset();
078: // If the GMT offset of this Zone will change in some
079: // future time, this Zone is added to the exclude list.
080: if (zone.size() > 1) {
081: ZoneRec zrec = zone.get(zone.size() - 2);
082: if ((zrec.getGmtOffset() != rawOffset)
083: && (zrec.getUntilTime(0) > Time
084: .getCurrentTime())) {
085: if (excludeList == null) {
086: excludeList = new ArrayList<String>();
087: }
088: excludeList.add(zone.getName());
089: continue;
090: }
091: }
092:
093: if (!rawOffsetsIndex.contains(new Integer(rawOffset))) {
094: // Find the index to insert this raw offset zones
095: int n = rawOffsetsIndex.size();
096: int i;
097: for (i = 0; i < n; i++) {
098: if (rawOffsetsIndex.get(i) > rawOffset) {
099: break;
100: }
101: }
102: rawOffsetsIndex.add(i, rawOffset);
103:
104: Set<String> perRawOffset = new TreeSet<String>();
105: perRawOffset.add(zonename);
106: rawOffsetsIndexTable.add(i, perRawOffset);
107: } else {
108: int i = rawOffsetsIndex.indexOf(new Integer(rawOffset));
109: Set<String> perRawOffset = rawOffsetsIndexTable.get(i);
110: perRawOffset.add(zonename);
111: }
112: }
113:
114: Map<String, String> a = zi.getAliases();
115: // If there are time zone names which refer to any of the
116: // excluded zones, add those names to the excluded list.
117: if (excludeList != null) {
118: for (String zoneName : a.keySet()) {
119: String realname = a.get(zoneName);
120: if (excludeList.contains(realname)) {
121: excludeList.add(zoneName);
122: }
123: }
124: }
125: aliases.putAll(a);
126: }
127:
128: /**
129: * Adds valid aliases to one of per-RawOffset table and removes
130: * invalid aliases from aliases List. Aliases referring to
131: * excluded zones are not added to a per-RawOffset table.
132: */
133: void resolve() {
134: int index = rawOffsetsIndexTable.size();
135: List<String> toBeRemoved = new ArrayList<String>();
136: for (String key : aliases.keySet()) {
137: boolean validname = false;
138: for (int j = 0; j < index; j++) {
139: Set<String> perRO = rawOffsetsIndexTable.get(j);
140: boolean isExcluded = (excludeList == null) ? false
141: : excludeList.contains(key);
142:
143: if ((perRO.contains(aliases.get(key)) || isExcluded)
144: && Zone.isTargetZone(key)) {
145: validname = true;
146: if (!isExcluded) {
147: perRO.add(key);
148: Main.info("Alias <" + key
149: + "> added to the list.");
150: }
151: break;
152: }
153: }
154:
155: if (!validname) {
156: Main.info("Alias <" + key + "> removed from the list.");
157: toBeRemoved.add(key);
158: }
159: }
160:
161: // Remove zones, if any, from the list.
162: for (String key : toBeRemoved) {
163: aliases.remove(key);
164: }
165: }
166:
167: Map<String, String> getAliases() {
168: return (aliases);
169: }
170:
171: List<Integer> getRawOffsetsIndex() {
172: return (rawOffsetsIndex);
173: }
174:
175: List<Set<String>> getRawOffsetsIndexTable() {
176: return (rawOffsetsIndexTable);
177: }
178:
179: List<String> getExcludeList() {
180: return excludeList;
181: }
182: }
|