Commits

timemachine committed 9a3479a

Improved DateListSchedule so it can better support DateListProvider.

  • Participants
  • Parent commits 0721f13
  • Branches 1.1.x

Comments (0)

Files changed (1)

timemachine-scheduler/src/main/java/timemachine/scheduler/schedule/DateListSchedule.java

 	 */
 	@Override
 	public Date getNextRun(Date after) {
-		if ((nextRunIndex + 1) < dates.size()) {
+		if ((nextRunIndex + 1) < dates.size())
 			return dates.get(nextRunIndex + 1);
-		} else {
-			updateDateListAfterEnded();
-			if (dates.size() > 0 && dates.get(0).getTime() > System.currentTimeMillis()) {
-				nextRunIndex = 0;
-				Date date = dates.get(nextRunIndex);
-				if (date.getTime() < System.currentTimeMillis())
-					return null;
-				return date;
-			} else {
-				return null;
-			}
+		
+		if (dateListProviderClassName == null)
+			return null;
+		
+		// Our nextRunIndex has passed our dates size, and we have a provider, so let's generate a new list
+		dates.clear(); // clean up the dates list and reset the nextRunIndex
+		nextRunIndex = 0;
+		updateDateListAfterEnded();
+		
+		if (dates.size() > 0) {
+			return dates.get(nextRunIndex);
 		}
+
+		// If after we call provider, and we still get a empty date list, then we are done.
+		return null;
 	}
 
 	@Override
 	 * set, then it will instantiate the provider and get new date list to run.
 	 */
 	private void updateDateListAfterEnded() {
+		dates.addAll(generateNextDateList(this));
+	}
+	
+	private List<Date> generateNextDateList(DateListSchedule schedule) {
 		if (dateListProviderClassName == null)
-			return;
+			return new ArrayList<Date>();
 		
+		logger.debug("Generating new date list using {}", dateListProviderClassName);
 		Class<DateListProvider> cls = Utils.toClass(dateListProviderClassName);
 		DateListProvider dateListProvider = Utils.newInstance(cls);
-		dates.addAll(dateListProvider.getDateList(this));
+		List<Date> result = dateListProvider.getDateList(schedule);
+		logger.debug("Generated {} dates", result.size());
+		return result;
 	}
 
 	/**
 	 */
 	@Override
 	public List<Date> getNextRuns(Date after, int maxCount) {
-		List<Date> result = new ArrayList<Date>();
-		int max = Math.min(dates.size(), maxCount);
-		for (int i = 0; i < max; i++) {
-			result.add(dates.get(i));
+		List<Date> result = new ArrayList<Date>();		
+		try {
+			Date previewNextRun = null;
+			DateListSchedule clone = (DateListSchedule)clone();
+			
+			while ((previewNextRun = clone.getNextRun(after)) != null) {
+				result.add(previewNextRun);
+				clone.updateNextRun();
+				
+				if (result.size() >= maxCount)
+					break;
+			}
+		} catch (CloneNotSupportedException e) {
+			throw new SchedulerException("Failed to clone DateListSchedule to generate preview next runs.", e);
 		}
+		
 		return result;
 	}
 
 	@Override
 	public Object clone() throws CloneNotSupportedException {
 		DateListSchedule clone = (DateListSchedule) super.clone();
-		clone.setDates(new ArrayList<Date>(getDates()));
+		clone.dates = new ArrayList<Date>(this.dates);
 		return clone;
 	}
 
 		this.missedRunPolicy = missedRunPolicy;
 		return this;
 	}
+	
+	public DateListSchedule setDateListProviderClassName(String dateListProviderClassName) {
+		this.dateListProviderClassName = dateListProviderClassName;
+		return this;
+	}
 }