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
23 import java.io.Serializable;
24
25 import java.util.Calendar;
26 import java.util.Collection;
27 import java.util.Date;
28 import java.util.TimeZone;
29 import java.util.TreeSet;
30
31 import org.apache.myfaces.custom.schedule.util.ScheduleUtil;
32
33
34 /**
35 * <p>
36 * This class represents a day in the Schedule component
37 * </p>
38 *
39 * @author Jurgen Lust (latest modification by $Author: werpu $)
40 * @version $Revision: 371736 $
41 */
42 public class Day
43 implements Serializable, Comparable
44 {
45 //~ Instance fields --------------------------------------------------------
46
47 /**
48 * serial id for serialisation versioning
49 */
50 private static final long serialVersionUID = 1L;
51 private final Date date;
52 private final Date dayEnd;
53 private final Date dayStart;
54 private String specialDayName;
55 private boolean workingDay;
56 private TreeSet intervals;
57 private final TimeZone timeZone;
58
59 //~ Constructors -----------------------------------------------------------
60
61 /**
62 * Creates a new Day object.
63 *
64 * @param date the date
65 *
66 * @throws NullPointerException when the date is null
67 */
68 public Day(Date date)
69 {
70 this(date, TimeZone.getDefault());
71 }
72
73 /**
74 * Creates a new Day object.
75 *
76 * @param date the date
77 * @param timeZone The timezone
78 *
79 * @throws NullPointerException when the date is null
80 */
81 public Day(Date date, TimeZone timeZone)
82 {
83 this.date = date;
84 this.timeZone = timeZone;
85
86 if (date == null) {
87 throw new NullPointerException("date should not be null");
88 }
89 Calendar cal = getCalendarInstance(date);
90 cal.set(Calendar.HOUR_OF_DAY, 0);
91 cal.set(Calendar.MINUTE, 0);
92 cal.set(Calendar.SECOND, 0);
93 cal.set(Calendar.MILLISECOND, 0);
94 this.dayStart = cal.getTime();
95 cal.add(Calendar.DATE, 1);
96 this.dayEnd = cal.getTime();
97 }
98
99 //~ Methods ----------------------------------------------------------------
100
101 protected Calendar getCalendarInstance(Date date) {
102
103 return ScheduleUtil.getCalendarInstance(date, timeZone);
104 }
105
106 /**
107 * @return Returns the date.
108 */
109 public Date getDate()
110 {
111 return (date == null) ? new Date() : date;
112 }
113
114 /**
115 * @return Returns 12PM of this day
116 */
117 public Date getDayEnd()
118 {
119 return dayEnd;
120 }
121
122 /**
123 * @return Returns 0AM of this day
124 */
125 public Date getDayStart()
126 {
127 return dayStart;
128 }
129
130 /**
131 * <p>
132 * If this day is a holiday of some kind, this sets the name
133 * </p>
134 *
135 * @param specialDayName The specialDayName to set.
136 */
137 public void setSpecialDayName(String specialDayName)
138 {
139 this.specialDayName = specialDayName;
140 }
141
142 /**
143 * <p>
144 * If this day is a holiday of some kind, this gets the name
145 * </p>
146 *
147 * @return Returns the specialDayName.
148 */
149 public String getSpecialDayName()
150 {
151 return specialDayName;
152 }
153
154 /**
155 * <p>
156 * Is this day a working day?
157 * </p>
158 *
159 * @param workingDay The workingDay to set.
160 */
161 public void setWorkingDay(boolean workingDay)
162 {
163 this.workingDay = workingDay;
164 }
165
166 /**
167 * <p>
168 * Is this day a working day?
169 * </p>
170 *
171 * @return Returns the workingDay.
172 */
173 public boolean isWorkingDay()
174 {
175 Calendar cal = getCalendarInstance(date);
176
177 int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
178
179 if ((dayOfWeek == Calendar.SATURDAY) || (dayOfWeek == Calendar.SUNDAY)) {
180 return false;
181 }
182
183 return workingDay;
184 }
185
186 /**
187 *
188 * @return A chronologically ordered set of intervals.
189 */
190 public TreeSet getIntervals()
191 {
192 return intervals;
193 }
194
195 /**
196 * Set user defined intervals during the day.
197 *
198 * @param intervals A Collection<Interval> of intervals during the day
199 */
200 public void setIntervals(Collection intervals)
201 {
202 if (intervals instanceof TreeSet)
203 {
204 this.intervals = (TreeSet) intervals;
205 }
206 else
207 {
208 this.intervals = new TreeSet(intervals);
209 }
210 }
211
212 public void addInterval(String label, Date startTime, Date endTime)
213 {
214 if (intervals == null)
215 {
216 intervals = new TreeSet();
217 }
218 intervals.add(new Interval(label, startTime, endTime));
219 }
220
221 /**
222 * @see java.lang.Comparable#compareTo(java.lang.Object)
223 */
224 public int compareTo(Object o)
225 {
226 if (o instanceof Day) {
227 Day other = (Day) o;
228
229 int returnint = ScheduleUtil.compareDays(date, other.getDate(), timeZone);
230
231 return returnint;
232 }
233
234 return 1;
235 }
236
237 /**
238 * @see java.lang.Object#equals(java.lang.Object)
239 */
240 public boolean equals(Object o)
241 {
242 if (o instanceof Day) {
243 Day other = (Day) o;
244
245 return ScheduleUtil.isSameDay(date, other.getDate(), timeZone);
246 }
247
248 return false;
249 }
250
251 /**
252 * <p>
253 * Check if the specified date is on this day
254 * </p>
255 *
256 * @param date the date to check
257 *
258 * @return if the date is on this day
259 */
260 public boolean equalsDate(Date date)
261 {
262 if (date == null) {
263 return false;
264 }
265
266 return ScheduleUtil.isSameDay(date, this.date, timeZone);
267 }
268
269 /**
270 * @see java.lang.Object#hashCode()
271 */
272 public int hashCode()
273 {
274 return ScheduleUtil.getHashCodeForDay(date, timeZone);
275 }
276 }
277 //The End