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