/**
 * Rule.java
 * Created on May 13, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.entities;

import java.util.Date;
import java.util.List;

/**
 * @author spuranik
 * 
 * This class represents the instance of the Rule created for a job.
 */
public class Rule {
	
	public static final int DEFAULT_ID = 0;
	private static final int HASHCODE_MULTIPLICATION_FACTOR = 7;
	private static final int HASHCODE_ADDITION_FACTOR = 103;
	
	// id for this rule in db
	private long id;

	// the date from when the rule will be applied
	private Date startDate;

	// date after which rule is no longer valid.
	// If not set, means never ending
	private Date endDate;

	// list of values supplied for this rule
	private List<RuleParameter> params;

	// Rule type corresponding to this Rule instance.
	private RuleType type;

	// fully qualified name of the class which can represent the time for this
	// rule.
	private String timeClass;

	// fully qualified name of the class which will be called when the job is
	// fired.
	private String callbackClass;

	/**
	 * Default ctor
	 */
	public Rule() {
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate
	 *            the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the params
	 */
	public List<RuleParameter> getParams() {
		return params;
	}

	/**
	 * @param params
	 *            the params to set
	 */
	public void setParams(List<RuleParameter> params) {
		this.params = params;
	}

	/**
	 * @return the type
	 */
	public RuleType getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(RuleType type) {
		this.type = type;
	}

	/**
	 * @return the timeClass this field is not persisted in the db but read from the config every
	 *         time the object is built.
	 */
	public String getTimeClass() {
		return timeClass;
	}

	/**
	 * @param timeClass
	 *            the timeClass to set
	 */
	public void setTimeClass(String timeClass) {
		this.timeClass = timeClass;
	}

	/**
	 * @return the callbackClass this field is not persisted in the db but read from the config every
	 *         time the object is built.
	 */
	public String getCallbackClass() {
		return callbackClass;
	}

	/**
	 * @param callbackClass the callbackClass to set
	 */
	public void setCallbackClass(String callbackClass) {
		this.callbackClass = callbackClass;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Rule)
		{
			Rule ruleObj = (Rule) obj;

			if (ruleObj.getId() != DEFAULT_ID && this.getId() != DEFAULT_ID)
			{
				return (ruleObj.getId() == this.getId());
			}
			else
			{
				return super.equals(obj);
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		long hash = this.id * HASHCODE_MULTIPLICATION_FACTOR + HASHCODE_ADDITION_FACTOR;
		int hashCode = new Long(hash).hashCode();
		if (this.id == DEFAULT_ID)
		{
			hashCode = super.hashCode();
		}
		return hashCode;
	}

}
