package caltool.schedule;

import mvp.*;

/****
 *
 * Duration is the time length of a scheduled item, in hours and minutes.  The
 * miniumn duration value is 1 minute.  The maximum is 999 houus.
 *
 */

public class Duration extends Model {

    /**
     * Construct an empty duration value.
     */
    public Duration() {
        hours = 0;
        minutes = 0;
    }

    /**
     * Construct a duration from the given hours and minutes.
     */
    public Duration(int hours, int minutes) {
        this.hours = hours;
        this.minutes = minutes;
    }

    /**
     * Return true if this is an empty duration, indicated by both hours and
     * minutes = 0.
     */
    public boolean isEmpty() {
        return (hours == 0) && (minutes == 0);
    }

    /**
     * Return the string representation of this.
     */
    public String toString() {

        String hrString = (hours == 0) ?
            "" : Integer.toString(hours).concat(
                (hours == 1) ? " hr " : " hrs ");

        String minString = (minutes > 0) ?
            Integer.toString(minutes).concat(" min ") : "";

        return hrString.concat(minString);

    }

    /**
     * Define equality for this as componentwise equality.
     */
    public boolean equals(Object obj) {
        Duration otherDuration = (Duration) obj;

        return
            hours == otherDuration.hours &&
            minutes == otherDuration.minutes;
    }

    /**
     * Define the hash code for this as the sum of the components.  This hash
     * code is used in turn by ItemKey.hashCode.
     */
    public int hashCode() {
        return hours + minutes;
    }


    /*-*
     * Derived data fields
     */

    /** Hour component of a duration value, between 0 and 999 */
    int hours;

    /** Minute component of a duration value, between 0 and 60*999 */
    int minutes;

}