de206f557b5739204acceaec86ed26cd61f03eec
[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
102         // sorted
103         // after those running during the day.
104         if (hour <= EARLY_HOUR) {
105             hour += HOURS_PER_DAY;
106         }
107         return (float) hour + (float) _minute / (float) SECONDS_PER_MINUTE;
108     }
109
110     /*
111      * (non-Javadoc)
112      * 
113      * @see java.lang.Object#equals(java.lang.Object)
114      */
115     @Override
116     public boolean equals(Object aObject) {
117         if (!(aObject instanceof Time)) {
118             return false;
119         }
120         return toString().equals(aObject.toString());
121     }
122
123     /**
124      * Compares based on time.
125      * 
126      * @param aObject
127      *            Time object to compare to.
128      * @return See {@link Comparable#compareTo(T)}.
129      */
130     public int compareTo(Object aObject) {
131         if (!(aObject instanceof Time)) {
132             throw new IllegalArgumentException("object not an instance of Time");
133         }
134         Time time = (Time) aObject;
135         return new Float(asFloat()).compareTo(new Float(time.asFloat()));
136     }
137
138     /*
139      * (non-Javadoc)
140      * 
141      * @see java.lang.Object#hashCode()
142      */
143     @Override
144     public int hashCode() {
145         return toString().hashCode();
146     }
147 }