package de.fitsample.timerecording;

/**
 * An instance of this class represents a period in time.
 * 
 * @author Ralf Stuckert
 */
public class TimeFrame implements Comparable {

	private Time startTime;

	private Time endTime;

	/**
	 * Creates a TimeFrame. if (startTime > endTime) an IllegalArgumentException
	 * is thrown.
	 * 
	 * @param startTime the start time. If <code>startTime</code> is
	 *                  <code>null</code>, an IllegalArgumentException is thrown.
	 * @param endTime   the end time. If <code>endTime</code> is <code>null</code>,
	 *                  an IllegalArgumentException is thrown.
	 * 
	 * @throws IllegalArgumentException
	 */
	public TimeFrame(Time startTime, Time endTime)
			throws IllegalArgumentException {
		if (startTime == null) {
			throw new IllegalArgumentException("start Time must not be null");
		}
		if (endTime == null) {
			throw new IllegalArgumentException("end Time must not be null");
		}
		if (startTime.compareTo(endTime) == 1) {
			throw new IllegalArgumentException(
					"start Time must be <= end Time: " + startTime + " > "
							+ endTime);
		}
		this.startTime = startTime;
		this.endTime = endTime;
	}

	/**
	 * @return the start Time.
	 */
	public Time getStartTime() {
		return startTime;
	}

	/**
	 * @return the end Time.
	 */
	public Time getEndTime() {
		return endTime;
	}

	/**
	 * @return the difference of {@link #getEndTime() end}and
	 *         {@link #getStartTime() start Time}.
	 */
	public double getDuration() {
		return getEndTime().doubleValue() - getStartTime().doubleValue();
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object other) {
		if (!(other instanceof TimeFrame)) {
			return false;
		}
		TimeFrame otherTimeFrame = (TimeFrame) other;
		return getStartTime().equals(otherTimeFrame.getStartTime())
				&& getEndTime().equals(otherTimeFrame.getEndTime());
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return getStartTime().hashCode() + 13 * getEndTime().hashCode();
	}

	/**
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Object other) {
		TimeFrame otherTimeFrame = (TimeFrame) other;
		if (otherTimeFrame == null) {
			return 1;
		}
		int result = getStartTime().compareTo(otherTimeFrame.getStartTime());
		if (result != 0) {
			return result;
		}
		return getEndTime().compareTo(otherTimeFrame.getEndTime());
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return null;
	}
}
