1 package org.oddjob.schedules.schedules; 2 3 import java.io.Serializable ; 4 import java.util.Date ; 5 6 import org.apache.log4j.Logger; 7 import org.oddjob.schedules.AbstractSchedule; 8 import org.oddjob.schedules.DateUtils; 9 import org.oddjob.schedules.Interval; 10 import org.oddjob.schedules.ScheduleContext; 11 12 31 32 final public class OccurrenceSchedule extends AbstractSchedule implements Serializable { 33 34 private static final long serialVersionUID = 20050226; 35 36 private final static Logger logger = Logger.getLogger(OccurrenceSchedule.class); 37 38 43 private int occurrence; 44 45 50 public void setOccurrence(String occurrence) { 51 this.occurrence = Integer.parseInt(occurrence); 52 } 53 54 59 60 public String getOccurrence() { 61 return Integer.toString(occurrence); 62 } 63 64 68 69 public Interval nextDue(ScheduleContext context) { 70 Date now = context.getDate(); 71 if (getChildSchedule() == null) { 72 throw new IllegalStateException ("Occurence must have a child schedule."); 73 } 74 75 logger.debug(this + ": in Date is " + now); 76 77 Date use = now; 78 if (getLimits() != null) { 80 use = getLimits().getFromDate(); 81 } 82 Interval candidate = null; 83 84 for (int i = 0; i < occurrence && use != null; ++i) { 85 logger.debug(this + ": use interval is " + use); 86 getChildSchedule().setLimits(getLimits()); 87 candidate = getChildSchedule().nextDue(context.spawn(use)); 88 89 if (candidate != null) { 90 use = DateUtils.oneMillisAfter(candidate.getToDate()); 91 } 92 else { 93 use = null; 95 } 96 } 97 return candidate; 98 } 99 100 103 104 public String toString() { 105 106 return "Occurence Schedule, occurences " + getOccurrence(); 107 } 108 } 109 | Popular Tags |