View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.custom.schedule.model;
21  
22  import java.io.Serializable;
23  import java.util.Calendar;
24  import java.util.Date;
25  import java.util.TimeZone;
26  
27  import org.apache.myfaces.custom.schedule.util.ScheduleUtil;
28  
29  /**
30   * <p>
31   * This class represents a period of time, which may be given a label.
32   * An interval is inclusive of the start time, but excludes the end time.
33   * </p>
34   * @since 1.1.7
35   * @author Peter Mahoney
36   * @version $Revision: 371736 $
37   */
38  public class Interval implements Serializable, Comparable
39  {
40  
41      private String label;
42      private Date startTime;
43      private Date endTime;
44      
45      public Interval(String label, Date startTime, Date endTime)
46      {
47          this.label = label;
48          this.startTime = startTime;
49          this.endTime = endTime;
50      }
51      
52      public String getLabel()
53      {
54          return label;
55      }
56      public void setLabel(String label)
57      {
58          this.label = label;
59      }
60      
61      public Date getStartTime()
62      {
63          return startTime;
64      }
65      public void setStartTime(Date startTime)
66      {
67          this.startTime = startTime;
68      }
69      
70      public Date getEndTime()
71      {
72          return endTime;
73      }
74      public void setEndTime(Date endTime)
75      {
76          this.endTime = endTime;
77      }
78  
79      public boolean containsDate(Date clickedDate)
80      {
81          return !getStartTime().after(clickedDate) && clickedDate.before(getEndTime());
82      }
83      
84      public int compareTo(Object o)
85      {
86          if (o instanceof Interval)
87          {
88              
89              return startTime.compareTo(((Interval) o).startTime);
90          }
91  
92          return 1;
93      }
94  
95      public boolean after(Interval last)
96      {
97          
98          return !startTime.before(last.getEndTime());
99      }
100 
101     public int getStartHours(TimeZone timeZone)
102     {
103         
104         return ScheduleUtil.getCalendarInstance(getStartTime(), timeZone).get(Calendar.HOUR_OF_DAY);
105     }
106 
107     public int getStartMinutes(TimeZone timeZone)
108     {
109         
110         return ScheduleUtil.getCalendarInstance(getStartTime(), timeZone).get(Calendar.MINUTE);
111     }
112     
113     public long getDuration()
114     {
115         
116         return getEndTime().getTime() - getStartTime().getTime();
117     }
118     
119     /**
120      * <p>
121      * Intervals are equivalent if their label is the same and they begin and end
122      * at the same time of day.
123      * </p>
124      *
125      * @param other the interval to compare with
126      *
127      * @return true, if this interval is equivalent to the given interval
128      */
129     public boolean isEquivalent(Interval other)
130     {
131             
132         return label.equals(other.label) 
133                 && ScheduleUtil.isSameTime(startTime, other.startTime) 
134                 && ScheduleUtil.isSameTime(endTime, other.endTime);
135     }
136     
137     public int hashCode() {
138 
139         return label.hashCode() + startTime.hashCode() + endTime.hashCode();
140     }
141     
142     public boolean equals(Object obj)
143     {
144         if (obj != null && obj instanceof Interval) {
145             Interval other = (Interval) obj;
146             
147             return label.equals(other.label) 
148                     && startTime.equals(other.startTime) 
149                     && endTime.equals(other.endTime);
150         }
151         
152         return false;
153     }
154     
155     public String toString()
156     {
157         
158         return this.getClass().getName() + " Start:" + startTime + " End:" + endTime;
159     }
160 }