(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / guide / Time.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.crawler.kiss.guide;
18
19 import java.text.DecimalFormat;
20 import java.text.NumberFormat;
21
22 /**
23  * TIme at which a program starts or ends.
24  */
25 public class Time implements Comparable {
26
27     /**
28      * Number of seconds per minute.
29      */
30     private static final double SECONDS_PER_MINUTE = 60.0;
31
32     /**
33      * Hour of the time.
34      */
35     private int _hour;
36
37     /**
38      * Minute of the hour.
39      */
40     private int _minute;
41
42     /**
43      * Constructs the time.
44      * 
45      * @param aHour
46      *            Hour.
47      * @param aMinute
48      *            Minute.
49      */
50     public Time(int aHour, int aMinute) {
51         _hour = aHour;
52         _minute = aMinute;
53     }
54
55     /**
56      * Gets the hour.
57      * 
58      * @return Hour.
59      */
60     public int getHour() {
61         return _hour;
62     }
63
64     /**
65      * Gets te minute.
66      * 
67      * @return Minute.
68      */
69     public int getMinute() {
70         return _minute;
71     }
72
73     /*
74      * (non-Javadoc)
75      * 
76      * @see java.lang.Object#toString()
77      */
78     @Override
79     public String toString() {
80         NumberFormat format = new DecimalFormat("00");
81         return format.format(_hour) + ":" + format.format(_minute);
82     }
83
84     /**
85      * Convert time to floating point value. Useful for comparing two times.
86      * 
87      * @return Converted value.
88      */
89     float asFloat() {
90         return (float) _hour + (float) _minute / (float) SECONDS_PER_MINUTE;
91     }
92
93     /*
94      * (non-Javadoc)
95      * 
96      * @see java.lang.Object#equals(java.lang.Object)
97      */
98     @Override
99     public boolean equals(Object obj) {
100         if ( !(obj instanceof Time )) { 
101             return false; 
102         }
103         return toString().equals(obj.toString());
104     }
105     
106     /* (non-Javadoc)
107      * @see java.lang.Comparable#compareTo(T)
108      */
109     public int compareTo(Object o) {
110         if ( !(o instanceof Time)) { 
111             throw new RuntimeException("object not an instance of Time"); 
112         }
113         Time time = (Time)o; 
114         return new Float(asFloat()).compareTo(new Float(time.asFloat()));
115     }
116     
117     /* (non-Javadoc)
118      * @see java.lang.Object#hashCode()
119      */
120     @Override
121     public int hashCode() {
122         return toString().hashCode();
123     }
124 }