Corrected sorting of programs.
[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      * 
29      */
30     private static final int HOURS_PER_DAY = 24;
31
32     /**
33      * 
34      */
35     private static final int EARLY_HOUR = 3;
36
37     /**
38      * Number of seconds per minute.
39      */
40     private static final double SECONDS_PER_MINUTE = 60.0;
41
42     /**
43      * Hour of the time.
44      */
45     private int _hour;
46
47     /**
48      * Minute of the hour.
49      */
50     private int _minute;
51
52     /**
53      * Constructs the time.
54      * 
55      * @param aHour
56      *            Hour.
57      * @param aMinute
58      *            Minute.
59      */
60     public Time(int aHour, int aMinute) {
61         _hour = aHour;
62         _minute = aMinute;
63     }
64
65     /**
66      * Gets the hour.
67      * 
68      * @return Hour.
69      */
70     public int getHour() {
71         return _hour;
72     }
73
74     /**
75      * Gets te minute.
76      * 
77      * @return Minute.
78      */
79     public int getMinute() {
80         return _minute;
81     }
82
83     /*
84      * (non-Javadoc)
85      * 
86      * @see java.lang.Object#toString()
87      */
88     @Override
89     public String toString() {
90         NumberFormat format = new DecimalFormat("00");
91         return format.format(_hour) + ":" + format.format(_minute);
92     }
93
94     /**
95      * Convert time to floating point value. Useful for comparing two times.
96      * 
97      * @return Converted value.
98      */
99     float asFloat() {
100         int hour = _hour;
101         // Hack to make sure that programs appearing shortly after midnight are sorted
102         // after those running during the day. 
103         if ( hour <= EARLY_HOUR ) { 
104             hour += HOURS_PER_DAY; 
105         }
106         return (float) hour + (float) _minute / (float) SECONDS_PER_MINUTE;
107     }
108
109     /*
110      * (non-Javadoc)
111      * 
112      * @see java.lang.Object#equals(java.lang.Object)
113      */
114     @Override
115     public boolean equals(Object aObject) {
116         if (!(aObject instanceof Time)) {
117             return false;
118         }
119         return toString().equals(aObject.toString());
120     }
121
122     /**
123      * Compares based on time. 
124      * @param aObject Time object to compare to. 
125      * @return See {@link Comparable#compareTo(T)}.
126      */
127     public int compareTo(Object aObject) {
128         if (!(aObject instanceof Time)) {
129             throw new RuntimeException("object not an instance of Time");
130         }
131         Time time = (Time) aObject;
132         return new Float(asFloat()).compareTo(new Float(time.asFloat()));
133     }
134
135     /*
136      * (non-Javadoc)
137      * 
138      * @see java.lang.Object#hashCode()
139      */
140     @Override
141     public int hashCode() {
142         return toString().hashCode();
143     }
144 }