1 package org.oddjob.schedules; 2 3 import java.util.Date ; 4 5 import org.apache.log4j.Logger; 6 7 12 13 abstract public class RegularSchedule extends AbstractSchedule { 14 15 private static final long serialVersionUID = 20050226; 16 17 private static Logger logger = Logger.getLogger(RegularSchedule.class); 18 19 26 abstract public Date next(Date from); 27 28 32 public Interval nextDue(ScheduleContext context) { 33 Date now = context.getDate(); 34 if (now == null) { 35 return null; 36 } 37 Interval nextInterval = null; 38 if (getLimits() == null) { 39 nextInterval = new Interval(now, DateUtils.oneMillisBefore(next(now))); 41 } 42 else { 43 Date start = getLimits().getFromDate(); 46 Date end = getLimits().getToDate(); 47 if (now.compareTo(start) < 0) { 49 nextInterval = new Interval(start, 50 DateUtils.oneMillisBefore(next(start))); 51 } 52 else if (now.compareTo(end) > 0) { 54 return null; 55 } 56 else { 57 while (true) { 60 Date next = next(start); 61 if (now.compareTo(next) < 0) { 62 nextInterval = new Interval(start, DateUtils.oneMillisBefore(next)); 63 break; 64 } 65 start = next; 66 } 67 } 68 } 69 logger.debug(this + ": in date is " + now 70 + ", next Interval is " + nextInterval); 71 72 if (nextInterval == null) { 73 return null; 74 } 75 Schedule child = getChildSchedule(); 76 if (child == null) { 77 return nextInterval; 78 } 79 child.setLimits(nextInterval); 80 Interval childInterval = child.nextDue(context); 81 if (childInterval == null) { 85 return nextDue(context.spawn(DateUtils.oneMillisAfter( 86 nextInterval.getToDate()))); 87 } 88 return childInterval; 89 } 90 91 96 public String toString() { 97 return this.getClass().getName() 98 + ", interval=" + next(new Date (0)).getTime() 99 + ", limits=" + getLimits(); 100 } 101 } 102 | Popular Tags |