1 /*
2 * ==================================================================== The
3 * Apache Software License, Version 1.1
4 *
5 * Copyright (c) 2003 Digital Clash LLC. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met: 1.
9 * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer. 2. Redistributions in
11 * binary form must reproduce the above copyright notice, this list of
12 * conditions and the following disclaimer in the documentation and/or other
13 * materials provided with the distribution. 3. The end-user documentation
14 * included with the redistribution, if any, must include the following
15 * acknowledgment: "This product includes software developed by the ChronicJ
16 * team (http://www.chronicj.org/)." Alternately, this acknowledgment may
17 * appear in the software itself, if and wherever such third-party
18 * acknowledgments normally appear. 4. The names "ChronicJ" and "Digital Clash"
19 * not be used to endorse or promote products derived from this software
20 * without prior written permission. For written permission, please contact
21 * info@digitalclash.com. 5. Products derived from this software may not be
22 * called "ChronicJ", "Digital Clash", nor may "ChronicJ" or "Digital Clash"
23 * appear in their name, without prior written permission of Digital Clash LLC.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
27 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * ==================================================================== This
36 * product includes software developed by the by the Apache Software Foundation
37 * (http://www.apache.org/).
38 * ====================================================================
39 */
40 package org.chronicj.impl;
41
42 import org.chronicj.DateRange;
43 import org.chronicj.Event;
44 import org.chronicj.RangeIterator;
45 import org.chronicj.Schedule;
46 import org.chronicj.ScheduleElement;
47 import org.chronicj.TemporalExpression;
48 import org.chronicj.TimePoint;
49
50 import java.util.ArrayList;
51 import java.util.HashMap;
52 import java.util.Iterator;
53 import java.util.List;
54 import java.util.Map;
55
56
57 /***
58 * Default implementation of a {@link org.chronicj.Schedule Schedule}.
59 *
60 * @author <a href="mlipper@US-ABP.com">Matthew Lipper</a>
61 *
62 * @see <a href="http://martinfowler.com/apsupp/recurring.pdf">Recurring
63 * Events for Calendars</a>
64 */
65 public class DefaultSchedule implements Schedule {
66 private Map elements;
67
68 /***
69 * Creates an instance of a <code>DefaultSchedule</code>.
70 */
71 public DefaultSchedule() {
72 super();
73 elements = new HashMap();
74 }
75
76 /***
77 * Default implementation of
78 * {@link org.chronicj.Schedule#isOccuring(org.chronicj.Event,org.chronicj.DateRangeImpl)}.
79 *
80 * @param anEvent
81 * the Event whose schedule is being queried
82 * @param aDateRange
83 * the time period in which to check schedule information
84 *
85 * @return a boolean value indicating whether the event occurs within the
86 * specified DateRange
87 *
88 * @throws NullPointerException
89 * if either argument is null
90 * @throws IllegalArgumentException
91 * if the DateRange argument is empty
92 *
93 * @see org.chronicj.Schedule#isOccuring(org.chronicj.Event,org.chronicj.DateRangeImpl)
94 */
95 public boolean isOccuring(Event anEvent, DateRange aDateRange) {
96 if (!isScheduled(anEvent)) {
97 return false;
98 }
99
100 for (Iterator i = elements.values().iterator(); i.hasNext();) {
101 for (Iterator j = ((List) i.next()).iterator(); j.hasNext();) {
102 if (((ScheduleElement) j.next()).isOccuring(anEvent, aDateRange)) {
103 return true;
104 }
105 }
106 }
107
108 return false;
109 }
110
111 /***
112 * Default implementation of
113 * {@link org.chronicj.Schedule#isOccuring(org.chronicj.Event,org.chronicj.TimePoint)}.
114 *
115 * @param anEvent
116 * the Event whose schedule is being queried
117 * @param aTimePoint
118 * the moment in time
119 *
120 * @return a boolean value indicating whether the event occurs during the
121 * specified TimePoint
122 *
123 * @throws NullPointerException
124 * if either argument is null
125 *
126 * @see org.chronicj.Schedule#isOccuring(org.chronicj.Event,org.chronicj.DateRangeImpl)
127 */
128 public boolean isOccuring(Event anEvent, TimePoint aTimePoint) {
129 if (!isScheduled(anEvent)) {
130 return false;
131 }
132
133 for (Iterator i = elements.values().iterator(); i.hasNext();) {
134 for (Iterator j = ((List) i.next()).iterator(); j.hasNext();) {
135 if (((ScheduleElement) j.next()).isOccuring(anEvent, aTimePoint)) {
136 return true;
137 }
138 }
139 }
140
141 return false;
142 }
143
144 public boolean isScheduled(Event anEvent) {
145 return elements.containsKey(anEvent.getId());
146 }
147
148 public void add(Event anEvent, TemporalExpression aTemporalExpression) {
149 List scheduleElements = null;
150
151 if (!isScheduled(anEvent)) {
152 scheduleElements = new ArrayList();
153 scheduleElements.add(new DefaultScheduleElement(anEvent,
154 aTemporalExpression));
155 elements.put(anEvent.getId(), scheduleElements);
156 } else {
157 scheduleElements = (List) elements.get(anEvent.getId());
158
159 ScheduleElement newScheduleElement = new DefaultScheduleElement(anEvent,
160 aTemporalExpression);
161
162 if (!scheduleElements.contains(newScheduleElement)) {
163 scheduleElements.add(newScheduleElement);
164 }
165 }
166 }
167
168 /***
169 * All calculations are made using {@link DatePrecision#DATE} precision.
170 *
171 * @see org.chronicj.Schedule#dates(org.chronicj.Event,
172 * org.chronicj.DateRangeImpl)
173 */
174 public List dates(Event anEvent, DateRange during) {
175 if (!isScheduled(anEvent)) {
176 return null;
177 }
178
179 List results = new ArrayList();
180
181 for (RangeIterator rangeIter = new RangeIterator(during);
182 rangeIter.hasNext();) {
183 DateRange subRange = (DateRange) rangeIter.next();
184 List anEventSchedule = (List) elements.get(anEvent.getId());
185
186 for (Iterator scheduleIter = anEventSchedule.iterator();
187 scheduleIter.hasNext();) {
188 ScheduleElement element = (ScheduleElement) scheduleIter.next();
189 System.out.println("Checking schedule element " + element +
190 " against range " + subRange);
191
192 if (element.isOccuring(anEvent, subRange.start())) {
193 results.add(subRange.start());
194 }
195 }
196 }
197
198 return results;
199 }
200
201 /*
202 * @see org.chronicj.Schedule#nextOccurence(org.chronicj.Event,
203 * org.chronicj.TimePoint)
204 */
205 public TimePoint nextOccurence(Event anEvent, TimePoint aTimePoint) {
206 // TODO Auto-generated method stub
207 return null;
208 }
209 }
This page was automatically generated by Maven