package com.uas.search.schedule;

import com.uas.search.util.NumberGenerator;

import java.util.Objects;

/**
 * 定时任务信息
 * 
 * @author sunyj
 * @since 2016年12月19日 上午10:21:28
 */
public class TaskInformation {

	/**
	 * 任务 code
	 */
	private String code;

	/**
	 * 任务标题
	 */
	private String title;

	/**
	 * 所执行的任务
	 */
	private Executable command;

	/**
	 * 第一次执行的延迟时间间隔（毫秒）
	 */
	private long initialDelay;

	/**
	 * 两次任务之间的等待时间间隔（毫秒）
	 */
	private long interval;

	private ScheduleType scheduleType;

	public TaskInformation() {
		super();
	}

	public TaskInformation(String title, Executable command, long initialDelay, long interval, ScheduleType scheduleType) {
		init();
		this.title = title;
		this.command = command;
		this.initialDelay = initialDelay;
		this.interval = interval;
		this.scheduleType = scheduleType;
	}

	public void init(){
		code = NumberGenerator.generateId();
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Executable getCommand() {
		return command;
	}

	public void setCommand(Executable command) {
		this.command = command;
	}

	public long getInitialDelay() {
		return initialDelay;
	}

	public void setInitialDelay(long initialDelay) {
		this.initialDelay = initialDelay;
	}

	public long getInterval() {
		return interval;
	}

	public void setInterval(long interval) {
		this.interval = interval;
	}

	public ScheduleType getScheduleType() {
		return scheduleType;
	}

	public void setScheduleType(ScheduleType scheduleType) {
		this.scheduleType = scheduleType;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null || getClass() != obj.getClass() || !(obj instanceof TaskInformation)) {
			return false;
		}
		TaskInformation other = (TaskInformation) obj;
		// command不好比较，不进行比较，也不比较 code
		return Objects.equals(title, other.getTitle()) && initialDelay == other.getInitialDelay()
				&& interval == other.getInterval() && scheduleType == other.getScheduleType();
	}

	@Override
	public String toString() {
		return "TaskInformation [code=" + code + ", title=" + title + ", initialDelay=" + initialDelay + ", interval=" + interval + ", scheduleType=" + scheduleType + "]";
	}

	/**
	 * 定时的间隔类型
	 */
	public enum ScheduleType {
		/**
		 * Creates and executes a periodic action that becomes enabled first
		 * after the given initial delay, and subsequently with the given
		 * period; that is executions will commence after
		 * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
		 * <tt>initialDelay + 2 * period</tt>, and so on.
		 * If any execution of the task
		 * encounters an exception, subsequent executions are suppressed.
		 * Otherwise, the task will only terminate via cancellation or
		 * termination of the executor.  If any execution of this task
		 * takes longer than its period, then subsequent executions
		 * may start late, but will not concurrently execute.
		 */
		FixedRate,

		/**
		 * Creates and executes a periodic action that becomes enabled first
		 * after the given initial delay, and subsequently with the
		 * given delay between the termination of one execution and the
		 * commencement of the next.  If any execution of the task
		 * encounters an exception, subsequent executions are suppressed.
		 * Otherwise, the task will only terminate via cancellation or
		 * termination of the executor.
		 */
		FixedDelay
	}
}
