(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / guide / TimeInterval.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
20 /**
21  * Time interval. 
22  */
23 public class TimeInterval {
24
25     /**
26      * Begin time. 
27      */
28     private Time _begin;
29
30     /**
31      * End time. 
32      */
33     private Time _end;
34
35     /**
36      * Construts the interval. 
37      * @param aBegin Start time. 
38      * @param aEnd End time. 
39      */
40     public TimeInterval(Time aBegin, Time aEnd) {
41         _begin = aBegin;
42         _end = aEnd;
43     }
44
45     /**
46      * Gets the begin time. 
47      * @return Begin time.  
48      */
49     public Time getBegin() {
50         return _begin;
51     }
52
53     /**
54      * Gets the end time. 
55      * @return End time. 
56      */
57     public Time getEnd() {
58         return _end;
59     }
60
61     /*
62      * (non-Javadoc)
63      * 
64      * @see java.lang.Object#toString()
65      */
66     @Override
67     public String toString() {
68         return _begin + " - " + _end;
69     }
70
71     /**
72      * Determines if there is an overlap between the current interval and given
73      * one.
74      * 
75      * @param aInterval
76      *            Interval to compare with.
77      * @return True iff there is overlap
78      */
79     public boolean overlap(TimeInterval aInterval) {
80
81         if (isUncertain() || aInterval.isUncertain()) {
82             // Optimistic assume there is no overlap if one of the intervals is
83             // uncertain.
84             return false;
85         }
86
87         if (_end.asFloat() <= aInterval._begin.asFloat()
88                 || aInterval._end.asFloat() <= _begin.asFloat()) {
89             return false;
90         }
91
92         return true;
93     }
94
95     /**
96      * Determines if the actual time that the program corresponds to is
97      * uncertain due to the representation of a period of more than 24 hours
98      * using a 24 hour clock.
99      * 
100      * @return True iff the interval is uncertain.
101      */
102     boolean isUncertain() {
103         return _begin.asFloat() > _end.asFloat();
104     }
105     
106     /* (non-Javadoc)
107      * @see java.lang.Object#equals(java.lang.Object)j
108      */
109     @Override
110     public boolean equals(Object obj) {
111         if ( !(obj instanceof TimeInterval)) { 
112             return false; 
113         } 
114         return obj.toString().equals(obj.toString());
115     }
116     
117     /* (non-Javadoc)
118      * @see java.lang.Object#hashCode()
119      */
120     @Override
121     public int hashCode() {
122         return _begin.hashCode();
123     }
124 }