1 package org.quartz.impl.calendar; 2 3 import java.text.ParseException ; 4 import java.util.Date ; 5 import java.util.TimeZone ; 6 7 import org.quartz.Calendar; 8 import org.quartz.CronExpression; 9 10 26 public class CronCalendar extends BaseCalendar { 27 static final long serialVersionUID = -8172103999750856831L; 28 29 30 private String name; 31 32 CronExpression cronExpression; 33 34 40 public CronCalendar(String expression) 41 throws ParseException { 42 this(null, expression, null); 43 } 44 45 54 public CronCalendar(Calendar baseCalendar, 55 String expression) throws ParseException { 56 this(baseCalendar, expression, null); 57 } 58 59 74 public CronCalendar(Calendar baseCalendar, 75 String expression, TimeZone timeZone) throws ParseException { 76 super(baseCalendar); 77 this.cronExpression = new CronExpression(expression); 78 this.cronExpression.setTimeZone(timeZone); 79 } 80 81 86 public CronCalendar(String name, String expression) 87 throws ParseException { 88 this(expression); 89 this.name = name; 90 } 91 92 97 public CronCalendar(String name, Calendar baseCalendar, 98 String expression) throws ParseException { 99 this(baseCalendar, expression); 100 this.name = name; 101 } 102 103 108 public CronCalendar(String name, Calendar baseCalendar, 109 String expression, TimeZone timeZone) throws ParseException { 110 this(baseCalendar, expression, timeZone); 111 this.name = name; 112 } 113 114 122 public TimeZone getTimeZone() { 123 return cronExpression.getTimeZone(); 124 } 125 126 136 public void setTimeZone(TimeZone timeZone) { 137 cronExpression.setTimeZone(timeZone); 138 } 139 140 147 public String getName() { 148 return name; 149 } 150 151 159 public boolean isTimeIncluded(long timeInMillis) { 160 if ((getBaseCalendar() != null) && 161 (getBaseCalendar().isTimeIncluded(timeInMillis) == false)) { 162 return false; 163 } 164 165 return (!(cronExpression.isSatisfiedBy(new Date (timeInMillis)))); 166 } 167 168 177 public long getNextIncludedTime(long timeInMillis) { 178 long nextIncludedTime = timeInMillis + 1; 180 while (!isTimeIncluded(nextIncludedTime)) { 181 182 if (cronExpression.isSatisfiedBy(new Date (nextIncludedTime))) { 189 nextIncludedTime = cronExpression.getNextInvalidTimeAfter( 190 new Date (nextIncludedTime)).getTime(); 191 } else if ((getBaseCalendar() != null) && 192 (!getBaseCalendar().isTimeIncluded(nextIncludedTime))){ 193 nextIncludedTime = 194 getBaseCalendar().getNextIncludedTime(nextIncludedTime); 195 } else { 196 nextIncludedTime++; 197 } 198 } 199 200 return nextIncludedTime; 201 } 202 203 209 public String toString() { 210 StringBuffer buffer = new StringBuffer (); 211 if (name != null) { 212 buffer.append(name).append(": "); 213 } 214 buffer.append("base calendar: ["); 215 if (getBaseCalendar() != null) { 216 buffer.append(getBaseCalendar().toString()); 217 } else { 218 buffer.append("null"); 219 } 220 buffer.append("], excluded cron expression: '"); 221 buffer.append(cronExpression); 222 buffer.append("'"); 223 return buffer.toString(); 224 } 225 226 233 public CronExpression getCronExpression() { 234 return cronExpression; 235 } 236 237 244 public void setCronExpression(String expression) throws ParseException { 245 CronExpression newExp = new CronExpression(expression); 246 247 this.cronExpression = newExp; 248 } 249 250 255 public void setCronExpression(CronExpression expression) { 256 if (expression == null) { 257 throw new IllegalArgumentException ("expression cannot be null"); 258 } 259 260 this.cronExpression = expression; 261 } 262 } | Popular Tags |