KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > calendar > struts > EventHourComparator


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: EventHourComparator.java,v $
31  * Revision 1.2 2005/04/09 17:19:18 colinmacleod
32  * Changed copyright text to GPL v2 explicitly.
33  *
34  * Revision 1.1.1.1 2005/03/10 17:47:44 colinmacleod
35  * Restructured ivata op around Hibernate/PicoContainer.
36  * Renamed ivata groupware.
37  *
38  * Revision 1.3 2004/03/21 21:16:22 colinmacleod
39  * Shortened name to ivata op.
40  *
41  * Revision 1.2 2004/02/01 22:07:29 colinmacleod
42  * Added full names to author tags
43  *
44  * Revision 1.1.1.1 2004/01/27 20:58:21 colinmacleod
45  * Moved ivata openportal to SourceForge..
46  *
47  * Revision 1.2 2003/10/15 13:57:23 colin
48  * fixing for XDoclet
49  *
50  * Revision 1.1 2003/02/24 19:09:22 colin
51  * moved to business
52  *
53  * Revision 1.1 2003/02/13 08:52:24 colin
54  * first complete version of Struts calendar
55  * -----------------------------------------------------------------------------
56  */

57 package com.ivata.groupware.business.calendar.struts;
58
59 import java.text.SimpleDateFormat JavaDoc;
60 import java.util.Comparator JavaDoc;
61 import java.util.GregorianCalendar JavaDoc;
62
63 import com.ivata.groupware.business.calendar.event.EventDO;
64
65
66 /**
67  * <p>This class is used to sort events which appear in a particular
68  * day by start time (if they start on that day) or finish time (if
69  * they
70  * start on another day but finish on that day.</p>
71  *
72  * <p>This is the comparator applied in the <code>TreeSet</code>
73  * instances in the <code>normalDayEvents</code> contained in
74  * <code>IndexForm</code>.</p>
75  *
76  * @since 2003-02-07
77  * @author Colin MacLeod
78  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
79  * @version $Revision: 1.2 $
80  */

81 public class EventHourComparator implements Comparator JavaDoc {
82     /**
83      * <p>Represents the day for which the events are sorted.</p>
84      */

85     private GregorianCalendar JavaDoc day = null;
86     /**
87      * <p>Represents midnight the day after the day for which the events
88      * are sorted. This is set automatically when <code>setDay</code> is
89      * called.</p>
90      */

91     GregorianCalendar JavaDoc dayAfter = null;
92     /**
93      * <p>Used to format times in 24hour format, to sort them.</p>
94      */

95     private SimpleDateFormat JavaDoc orderFormat = new SimpleDateFormat JavaDoc("HHmmssSS");
96
97     /**
98      * <p>Compares the two parameters which are both actually instances of
99      * <code>EventDO</code>.</p>.
100      *
101      * <p>If both events take place on the current day, start times are
102      * compared, otherwise finish times are compared.</p>.
103      *
104      * @param arg0 first event to compare.
105      * @param arg1 second event to compare
106      * @return a positive number if the event <code>arg1</code> comes
107      * after <code>arg0</code> otherwise a negative number.
108      */

109     public final int compare(final Object JavaDoc arg0,
110             final Object JavaDoc arg1) {
111         String JavaDoc key0 = getKey((EventDO) arg0);
112         String JavaDoc key1 = getKey((EventDO) arg1);
113
114         // check the day was set first
115
if (day == null) {
116             throw new NullPointerException JavaDoc("ERROR in EventHourComparator: day is null.");
117         }
118         return key0.compareTo(key1);
119     }
120
121     /**
122      * <p>Always returns <code>false</code> since we do not want events to
123      * be removed from the set</code>.
124      *
125      * @param arg0 event to compare, Always ignored by this
126      * implementation.
127      * @return always <code>false</code>.
128      */

129     public boolean equals(final Object JavaDoc arg0) {
130         return false;
131     }
132
133     /**
134      * <p>Represents the day for which the events are sorted.</p>
135      *
136      * @return the current value of day.
137      */

138     public final GregorianCalendar JavaDoc getDay() {
139         return day;
140     }
141
142     /**
143      * <p>Represents the day for which the events are sorted.</p>
144      *
145      * @param day the new value of day.
146      */

147     public final void setDay(final GregorianCalendar JavaDoc day) {
148         this.day = day;
149         dayAfter = new GregorianCalendar JavaDoc();
150         dayAfter.setTime(day.getTime());
151         // set the time to midnight tomorrow - when we compare, anything after
152
// midnight is after today
153
dayAfter.set(GregorianCalendar.HOUR_OF_DAY,0);
154         dayAfter.set(GregorianCalendar.MINUTE,0);
155         dayAfter.set(GregorianCalendar.SECOND,0);
156         dayAfter.set(GregorianCalendar.MILLISECOND,0);
157         dayAfter.set(GregorianCalendar.DAY_OF_YEAR,
158             dayAfter.get(GregorianCalendar.DAY_OF_YEAR)+1);
159     }
160
161     /**
162      * <p>Private helper method. Gets a key which is used to order the
163      * events.</p>
164      *
165      * @param event event for which to get the key
166      * @return a <code>String</code> which, when compared, will put the
167      * event in the correct sequence for this day.
168      */

169     private String JavaDoc getKey(final EventDO event) {
170         String JavaDoc key;
171         // if the event starts today, use the start time to compare
172
if (!event.getStart().before(day)) {
173             key = orderFormat.format(event.getStart().getTime()) + event.getId().toString();
174
175         // if the event finishes today but started before today, it is the
176
// to time which is important
177
} else if (!isAfterDay(event)) {
178             // "to" ensures finishing events come after starting ones
179
key = orderFormat.format(event.getFinish().getTime())
180                 + "to" + event.getId().toString();
181
182         // if it gets here, it must be an all day event
183
// just use the id to sort - "0000000000" should always come before the
184
// other numbers
185
} else {
186             key = "0000000000" + event.getId().toString();
187         }
188
189         return key;
190     }
191
192     /**
193      * <p>Find out if an event occurs after the current day.</p>
194      *
195      * @param event the event to compare with the current day.
196      * @return <code>true</code> if the event occurs after the current day
197      * otherwise <code>false</code>.
198      */

199     public boolean isAfterDay(final EventDO event) {
200         // see if this event finishes after today (sometime tomorrow or
201
// beyond)
202
return (event.getFinish() != null) && !dayAfter.after(event.getFinish());
203     }
204 }
205
Popular Tags