package org.treesched.schedules;

import java.io.Serializable;
import java.util.Date;

import org.apache.log4j.Logger;
import org.treesched.AbstractSchedule;
import org.treesched.DateUtils;
import org.treesched.Interval;
import org.treesched.Schedule;

/**
 * This schedule returns an interval 
 * which is a point in time after it's child interval.
 * 
 * <p>
 * Typical configuration is:
 * <code><pre>
 * &lt;after&lt;
 *   &lt;interval interval="00:20"/>&gt;
 * &gt;/after&lt;
 * </pre></code>
 * 
 * @author Rob Gordon
 */

final public class AfterSchedule extends AbstractSchedule implements Serializable {

    private static final long serialVersionUID = 20050226;
    
    private static final Logger logger = Logger.getLogger(AfterSchedule.class);
    
	public Interval nextDue(Date now) {
	    if (now == null) {
	        throw new NullPointerException();
	    }
		if (getChildSchedule() == null) {
		    throw new IllegalStateException("After must have a child schedule.");
		}
		
		logger.debug(this + ": in date is " + now);
				
		Schedule child = getChildSchedule();
		child.setLimits(getLimits());
		Interval next = child.nextDue(now);
		if (next == null) {
		    return null;
		}
		Date after = DateUtils.oneMillisAfter(next.getToDate());
		
		return new Interval(after, after);
	}
}
