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.List;
031: import java.util.Map;
032:
033: /**
034: * RuleDay class represents the value of the "ON" field. The day of
035: * week values start from 1 following the {@link java.util.Calendar}
036: * convention.
037: *
038: * @since 1.4
039: */
040: class RuleDay {
041: private static final Map<String, DayOfWeek> abbreviations = new HashMap<String, DayOfWeek>(
042: 7);
043: static {
044: for (DayOfWeek day : DayOfWeek.values()) {
045: abbreviations.put(day.getAbbr(), day);
046: }
047: }
048:
049: private String dayName = null;
050: private DayOfWeek dow;
051: private boolean lastOne = false;
052: private int soonerOrLater = 0;
053: private int thanDayOfMonth; // day of month (e.g., 8 for "Sun>=8")
054:
055: RuleDay() {
056: }
057:
058: RuleDay(int day) {
059: thanDayOfMonth = day;
060: }
061:
062: int getDay() {
063: return thanDayOfMonth;
064: }
065:
066: /**
067: * @return the day of week value (1-based)
068: */
069: int getDayOfWeekNum() {
070: return dow.value();
071: }
072:
073: /**
074: * @return true if this rule day represents the last day of
075: * week. (e.g., lastSun).
076: */
077: boolean isLast() {
078: return lastOne;
079: }
080:
081: /**
082: * @return true if this rule day represents the day of week on or
083: * later than (after) the {@link #getDay}. (e.g., Sun>=1)
084: */
085: boolean isLater() {
086: return soonerOrLater > 0;
087: }
088:
089: /**
090: * @return true if this rule day represents the day of week on or
091: * earlier than (before) the {@link #getDay}. (e.g., Sun<=15)
092: */
093: boolean isEarlier() {
094: return soonerOrLater < 0;
095: }
096:
097: /**
098: * @return true if this rule day represents an exact day.
099: */
100: boolean isExact() {
101: return soonerOrLater == 0;
102: }
103:
104: /**
105: * Parses the "ON" field and constructs a RuleDay.
106: * @param day an "ON" field string (e.g., "Sun>=1")
107: * @return a RuleDay representing the given "ON" field
108: */
109: static RuleDay parse(String day) {
110: RuleDay d = new RuleDay();
111: if (day.startsWith("last")) {
112: d.lastOne = true;
113: d.dayName = day.substring(4);
114: d.dow = getDOW(d.dayName);
115: } else {
116: int index;
117: if ((index = day.indexOf(">=")) != -1) {
118: d.dayName = day.substring(0, index);
119: d.dow = getDOW(d.dayName);
120: d.soonerOrLater = 1; // greater or equal
121: d.thanDayOfMonth = Integer.parseInt(day
122: .substring(index + 2));
123: } else if ((index = day.indexOf("<=")) != -1) {
124: d.dayName = day.substring(0, index);
125: d.dow = getDOW(d.dayName);
126: d.soonerOrLater = -1; // less or equal
127: d.thanDayOfMonth = Integer.parseInt(day
128: .substring(index + 2));
129: } else {
130: // it should be an integer value.
131: d.thanDayOfMonth = Integer.parseInt(day);
132: }
133: }
134: return d;
135: }
136:
137: /**
138: * Converts this RuleDay to the SimpleTimeZone day rule.
139: * @return the converted SimpleTimeZone day rule
140: */
141: int getDayForSimpleTimeZone() {
142: if (isLast()) {
143: return -1;
144: }
145: return getDay();
146: }
147:
148: /**
149: * Converts this RuleDay to the SimpleTimeZone day-of-week rule.
150: * @return the SimpleTimeZone day-of-week rule value
151: */
152: int getDayOfWeekForSimpleTimeZoneInt() {
153: if (!isLater() && !isEarlier() && !isLast()) {
154: return 0;
155: }
156: if (isLater()) {
157: return -getDayOfWeekNum();
158: }
159: return getDayOfWeekNum();
160: }
161:
162: /**
163: * @return the string representation of the {@link
164: * #getDayOfWeekForSimpleTimeZoneInt} value
165: */
166: String getDayOfWeekForSimpleTimeZone() {
167: int d = getDayOfWeekForSimpleTimeZoneInt();
168: if (d == 0) {
169: return "0";
170: }
171: String sign = "";
172: if (d < 0) {
173: sign = "-";
174: d = -d;
175: }
176: return sign + toString(d);
177: }
178:
179: private static DayOfWeek getDOW(String abbr) {
180: return abbreviations.get(abbr);
181: }
182:
183: /**
184: * Converts the specified day of week value to the day-of-week
185: * name defined in {@link java.util.Calenda}.
186: * @param dow 1-based day of week value
187: * @return the Calendar day of week name with "Calendar." prefix.
188: * @throws IllegalArgumentException if the specified dow value is out of range.
189: */
190: static String toString(int dow) {
191: if (dow >= DayOfWeek.SUNDAY.value()
192: && dow <= DayOfWeek.SATURDAY.value()) {
193: return "Calendar." + DayOfWeek.values()[dow - 1];
194: }
195: throw new IllegalArgumentException("wrong Day_of_Week number: "
196: + dow);
197: }
198: }
|