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