001: /*
002: * @(#)RuleDay.java 1.6 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.tools.javazic;
028:
029: import java.util.HashMap;
030:
031: /**
032: * RuleDay class represents the value of the "ON" field. The day of
033: * week values start from 1 following the {@link java.util.Calendar}
034: * convention.
035: *
036: * @since 1.4
037: */
038: class RuleDay {
039: private static HashMap dayOfWeek = new HashMap();
040: static {
041: dayOfWeek.put("Sun", new Integer(1));
042: dayOfWeek.put("Mon", new Integer(2));
043: dayOfWeek.put("Tue", new Integer(3));
044: dayOfWeek.put("Wed", new Integer(4));
045: dayOfWeek.put("Thu", new Integer(5));
046: dayOfWeek.put("Fri", new Integer(6));
047: dayOfWeek.put("Sat", new Integer(7));
048: }
049:
050: private String dayName = null;
051: private int dow;
052: private boolean lastOne = false;
053: private int soonerOrLater = 0;
054: private int thanDay;
055:
056: RuleDay() {
057: }
058:
059: RuleDay(int day) {
060: thanDay = day;
061: }
062:
063: int getDay() {
064: return thanDay;
065: }
066:
067: /**
068: * @return the day of week value (1-based)
069: */
070: int getDayOfWeek() {
071: return dow;
072: }
073:
074: /**
075: * @return true if this rule day represents the last day of
076: * week. (e.g., lastSun).
077: */
078: boolean isLast() {
079: return lastOne;
080: }
081:
082: /**
083: * @return true if this rule day represents the day of week on or
084: * later than (after) the {@link #getDay}. (e.g., Sun>=1)
085: */
086: boolean isLater() {
087: return soonerOrLater > 0;
088: }
089:
090: /**
091: * @return true if this rule day represents the day of week on or
092: * earlier than (before) the {@link #getDay}. (e.g., Sun<=15)
093: */
094: boolean isEarlier() {
095: return soonerOrLater < 0;
096: }
097:
098: /**
099: * @return true if this rule day represents an exact day.
100: */
101: boolean isExact() {
102: return soonerOrLater == 0;
103: }
104:
105: /**
106: * Parses the "ON" field and constructs a RuleDay.
107: * @param day an "ON" field string (e.g., "Sun>=1")
108: * @return a RuleDay representing the given "ON" field
109: */
110: static RuleDay parse(String day) {
111: RuleDay d = new RuleDay();
112: if (day.startsWith("last")) {
113: d.lastOne = true;
114: d.dayName = day.substring(4);
115: d.dow = getDOW(d.dayName);
116: } else {
117: int index;
118: if ((index = day.indexOf(">=")) != -1) {
119: d.dayName = day.substring(0, index);
120: d.dow = getDOW(d.dayName);
121: d.soonerOrLater = 1; // greater or equal
122: d.thanDay = Integer.parseInt(day.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.thanDay = Integer.parseInt(day.substring(index + 2));
128: } else {
129: // it should be an integer value.
130: d.thanDay = Integer.parseInt(day);
131: }
132: }
133: return d;
134: }
135:
136: /**
137: * Converts this RuleDay to the SimpleTimeZone day rule.
138: * @return the converted SimpleTimeZone day rule
139: */
140: int getDayForSimpleTimeZone() {
141: if (isLast()) {
142: return -1;
143: }
144: return getDay();
145: }
146:
147: /**
148: * Converts this RuleDay to the SimpleTimeZone day-of-week rule.
149: * @return the SimpleTimeZone day-of-week rule value
150: */
151: int getDayOfWeekForSimpleTimeZoneInt() {
152: if (!isLater() && !isEarlier() && !isLast()) {
153: return 0;
154: }
155: if (isLater()) {
156: return -getDayOfWeek();
157: }
158: return getDayOfWeek();
159: }
160:
161: /**
162: * @return the string representation of the {@link
163: * #getDayOfWeekForSimpleTimeZoneInt} value
164: */
165: String getDayOfWeekForSimpleTimeZone() {
166: int d = getDayOfWeekForSimpleTimeZoneInt();
167: if (d == 0) {
168: return "0";
169: }
170: String sign = "";
171: if (d < 0) {
172: sign = "-";
173: d = -d;
174: }
175: return sign + toString(d);
176: }
177:
178: private static int getDOW(String name) {
179: return ((Integer) dayOfWeek.get(name)).intValue();
180: }
181:
182: private static final String upper_DayOfWeek[] = { "SUNDAY",
183: "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
184: "SATURDAY" };
185:
186: /**
187: * Converts the specified day of week value to the day-of-week
188: * name defined in {@link java.util.Calenda}.
189: * @param dow 1-based day of week value
190: * @return the Calendar day of week name with "Calendar." prefix.
191: * @throws IllegalArgumentException if the specified dow value is out of range.
192: */
193: static String toString(int dow) {
194: --dow;
195: if (dow >= 0 && dow <= 6) {
196: return "Calendar." + upper_DayOfWeek[dow];
197: }
198: throw new IllegalArgumentException("wrong Day_of_Week number: "
199: + (dow + 1));
200: }
201: }
|