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