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.Arrays;
030: import java.util.Comparator;
031: import java.util.List;
032: import java.util.StringTokenizer;
033:
034: /**
035: * Rule manipulates Rule records.
036: *
037: * @since 1.4
038: */
039: class Rule {
040:
041: private List<RuleRec> list;
042: private String name;
043:
044: /**
045: * Constructs a Rule which consists of a Rule record list. The
046: * specified name is given to this Rule.
047: * @param name the Rule name
048: */
049: Rule(String name) {
050: this .name = name;
051: list = new ArrayList<RuleRec>();
052: }
053:
054: /**
055: * Added a RuleRec to the Rule record list.
056: */
057: void add(RuleRec rec) {
058: list.add(rec);
059: }
060:
061: /**
062: * @return the Rule name
063: */
064: String getName() {
065: return name;
066: }
067:
068: /**
069: * Gets all rule records that cover the given year.
070: *
071: * @param year the year number for which the rule is applicable.
072: * @return rules in List that are collated in time. If no rule is found, an empty
073: * List is returned.
074: */
075: List<RuleRec> getRules(int year) {
076: List<RuleRec> rules = new ArrayList<RuleRec>(3);
077: for (RuleRec rec : list) {
078: if (year >= rec.getFromYear() && year <= rec.getToYear()) {
079: if ((rec.isOdd() && year % 2 == 0)
080: || (rec.isEven() && year % 2 == 1))
081: continue;
082: rules.add(rec);
083: }
084: }
085: int n = rules.size();
086: if (n <= 1) {
087: return rules;
088: }
089: if (n == 2) {
090: RuleRec rec1 = rules.get(0);
091: RuleRec rec2 = rules.get(1);
092: if (rec1.getMonthNum() > rec2.getMonthNum()) {
093: rules.set(0, rec2);
094: rules.set(1, rec1);
095: } else if (rec1.getMonthNum() == rec2.getMonthNum()) {
096: // TODO: it's not accurate to ignore time types (STD, WALL, UTC)
097: long t1 = Time.getLocalTime(year, rec1.getMonth(), rec1
098: .getDay(), rec1.getTime().getTime());
099: long t2 = Time.getLocalTime(year, rec2.getMonth(), rec2
100: .getDay(), rec2.getTime().getTime());
101: if (t1 > t2) {
102: rules.set(0, rec2);
103: rules.set(1, rec1);
104: }
105: }
106: return rules;
107: }
108:
109: final int y = year;
110: RuleRec[] recs = new RuleRec[rules.size()];
111: rules.toArray(recs);
112: Arrays.sort(recs, new Comparator<RuleRec>() {
113: public int compare(RuleRec r1, RuleRec r2) {
114: int n = r1.getMonthNum() - r2.getMonthNum();
115: if (n != 0) {
116: return n;
117: }
118: // TODO: it's not accurate to ignore time types (STD, WALL, UTC)
119: long t1 = Time.getLocalTime(y, r1.getMonth(), r1
120: .getDay(), r1.getTime().getTime());
121: long t2 = Time.getLocalTime(y, r2.getMonth(), r2
122: .getDay(), r2.getTime().getTime());
123: return (int) (t1 - t2);
124: }
125:
126: public boolean equals(Object o) {
127: return this == o;
128: }
129: });
130: rules.clear();
131: for (int i = 0; i < n; i++) {
132: rules.add(recs[i]);
133: }
134: return rules;
135: }
136:
137: /**
138: * Gets rule records that have either "max" or cover the endYear
139: * value in its DST schedule.
140: *
141: * @return rules that contain last DST schedule. An empty
142: * ArrayList is returned if no last rules are found.
143: */
144: List<RuleRec> getLastRules() {
145: RuleRec start = null;
146: RuleRec end = null;
147:
148: for (int i = 0; i < list.size(); i++) {
149: RuleRec rec = list.get(i);
150: if (rec.isLastRule()) {
151: if (rec.getSave() > 0) {
152: start = rec;
153: } else {
154: end = rec;
155: }
156: }
157: }
158: if (start == null || end == null) {
159: int endYear = Zoneinfo.getEndYear();
160: for (int i = 0; i < list.size(); i++) {
161: RuleRec rec = list.get(i);
162: if (endYear >= rec.getFromYear()
163: && endYear <= rec.getToYear()) {
164: if (start == null && rec.getSave() > 0) {
165: start = rec;
166: } else {
167: if (end == null && rec.getSave() == 0) {
168: end = rec;
169: }
170: }
171: }
172: }
173: }
174:
175: List<RuleRec> r = new ArrayList<RuleRec>(2);
176: if (start == null || end == null) {
177: if (start != null || end != null) {
178: Main.warning("found last rules for " + name
179: + " inconsistent.");
180: }
181: return r;
182: }
183:
184: r.add(start);
185: r.add(end);
186: return r;
187: }
188: }
|