1 17 18 22 23 package org.quartz.impl.calendar; 24 25 import java.io.Serializable ; 26 import java.util.TimeZone ; 27 28 import org.quartz.Calendar; 29 30 42 public class MonthlyCalendar extends BaseCalendar implements Calendar, 43 Serializable { 44 45 static final long serialVersionUID = 419164961091807944L; 46 47 private static final int MAX_DAYS_IN_MONTH = 31; 48 49 private boolean[] excludeDays = new boolean[MAX_DAYS_IN_MONTH]; 52 53 private boolean excludeAll = false; 55 56 public MonthlyCalendar() { 57 this(null, null); 58 } 59 60 public MonthlyCalendar(Calendar baseCalendar) { 61 this(baseCalendar, null); 62 } 63 64 public MonthlyCalendar(TimeZone timeZone) { 65 this(null, timeZone); 66 } 67 68 public MonthlyCalendar(Calendar baseCalendar, TimeZone timeZone) { 69 super(baseCalendar, timeZone); 70 71 excludeAll = areAllDaysExcluded(); 73 } 74 75 82 public boolean[] getDaysExcluded() { 83 return excludeDays; 84 } 85 86 93 public boolean isDayExcluded(int day) { 94 if ((day < 1) || (day > MAX_DAYS_IN_MONTH)) { 95 throw new IllegalArgumentException ( 96 "The day parameter must be in the range of 1 to " + MAX_DAYS_IN_MONTH); 97 } 98 99 return excludeDays[day - 1]; 100 } 101 102 109 public void setDaysExcluded(boolean[] days) { 110 if (days == null) { 111 throw new IllegalArgumentException ("The days parameter cannot be null."); 112 } 113 114 if (days.length < MAX_DAYS_IN_MONTH) { 115 throw new IllegalArgumentException ( 116 "The days parameter must have a length of at least " + MAX_DAYS_IN_MONTH + " elements."); 117 } 118 119 excludeDays = days; 120 excludeAll = areAllDaysExcluded(); 121 } 122 123 131 public void setDayExcluded(int day, boolean exclude) { 132 if ((day < 1) || (day > MAX_DAYS_IN_MONTH)) { 133 throw new IllegalArgumentException ( 134 "The day parameter must be in the range of 1 to " + MAX_DAYS_IN_MONTH); 135 } 136 137 excludeDays[day - 1] = exclude; 138 excludeAll = areAllDaysExcluded(); 139 } 140 141 146 public boolean areAllDaysExcluded() { 147 for (int i = 1; i <= MAX_DAYS_IN_MONTH; i++) { 148 if (isDayExcluded(i) == false) { 149 return false; 150 } 151 } 152 153 return true; 154 } 155 156 166 public boolean isTimeIncluded(long timeStamp) { 167 if (excludeAll == true) { 168 return false; 169 } 170 171 if (super.isTimeIncluded(timeStamp) == false) { return false; } 174 175 java.util.Calendar cl = createJavaCalendar(timeStamp); 176 int day = cl.get(java.util.Calendar.DAY_OF_MONTH); 177 178 return !(isDayExcluded(day)); 179 } 180 181 192 public long getNextIncludedTime(long timeStamp) { 193 if (excludeAll == true) { 194 return 0; 195 } 196 197 long baseTime = super.getNextIncludedTime(timeStamp); 199 if ((baseTime > 0) && (baseTime > timeStamp)) { 200 timeStamp = baseTime; 201 } 202 203 java.util.Calendar cl = getStartOfDayJavaCalendar(timeStamp); 205 int day = cl.get(java.util.Calendar.DAY_OF_MONTH); 206 207 if (!isDayExcluded(day)) { 208 return timeStamp; } 210 211 while (isDayExcluded(day) == true) { 212 cl.add(java.util.Calendar.DATE, 1); 213 day = cl.get(java.util.Calendar.DAY_OF_WEEK); 214 } 215 216 return cl.getTime().getTime(); 217 } 218 } 219 | Popular Tags |