View Javadoc

1   /*
2    * $Id: Recur.java [18-Apr-2004]
3    *
4    * Copyright (c) 2004, Ben Fortuna
5    * 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
9    * are met:
10   *
11   * 	o Redistributions of source code must retain the above copyright
12   * notice, this list of conditions and the following disclaimer.
13   *
14   * 	o Redistributions in binary form must reproduce the above copyright
15   * notice, this list of conditions and the following disclaimer in the
16   * documentation and/or other materials provided with the distribution.
17   *
18   * 	o Neither the name of Ben Fortuna nor the names of any other contributors
19   * may be used to endorse or promote products derived from this software
20   * without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   */
34  package net.fortuna.ical4j.model;
35  
36  import java.text.ParseException;
37  import java.util.Date;
38  import java.util.HashMap;
39  import java.util.Map;
40  import java.util.StringTokenizer;
41  
42  import net.fortuna.ical4j.util.DateFormat;
43  import net.fortuna.ical4j.util.DateTimeFormat;
44  
45  /***
46   * Defines a recurrence.
47   *
48   * @author benfortuna
49   */
50  public class Recur {
51  
52      private static final String FREQ = "FREQ";
53  
54      private static final String UNTIL = "UNTIL";
55  
56      private static final String COUNT = "COUNT";
57  
58      private static final String INTERVAL = "INTERVAL";
59  
60      private static final String BYSECOND = "BYSECOND";
61  
62      private static final String BYMINUTE = "BYMINUTE";
63  
64      private static final String BYHOUR = "BYHOUR";
65  
66      private static final String BYDAY = "BYDAY";
67  
68      private static final String BYMONTHDAY = "BYMONTHDAY";
69  
70      private static final String BYYEARDAY = "BYYEARDAY";
71  
72      private static final String BYWEEKNO = "BYWEEKNO";
73  
74      private static final String BYMONTH = "BYMONTH";
75  
76      private static final String BYSETPOS = "BYSETPOS";
77  
78      private static final String WKST = "WKST";
79  
80      // frequencies..
81      private static final String SECONDLY = "SECONDLY";
82  
83      private static final String MINUTELY = "MINUTELY";
84  
85      private static final String HOURLY = "HOURLY";
86  
87      private static final String DAILY = "DAILY";
88  
89      private static final String WEEKLY = "WEEKLY";
90  
91      private static final String MONTHLY = "MONTHLY";
92  
93      private static final String YEARLY = "YEARLY";
94  
95      // weekdays..
96      private static final String SU = "SU";
97  
98      private static final String MO = "MO";
99  
100     private static final String TU = "TU";
101 
102     private static final String WE = "WE";
103 
104     private static final String TH = "TH";
105 
106     private static final String FR = "FR";
107 
108     private static final String SA = "SA";
109 
110     private String frequency;
111 
112     private Date until;
113 
114     private int count = -1;
115 
116     private int interval = -1;
117 
118     private NumberList secondList;
119 
120     private NumberList minuteList;
121 
122     private NumberList hourList;
123 
124     private DayList dayList;
125 
126     private NumberList monthDayList;
127 
128     private NumberList yearDayList;
129 
130     private NumberList weekNoList;
131 
132     private NumberList monthList;
133 
134     private NumberList setPosList;
135 
136     private String weekStartDay;
137 
138     private Map experimentalValues;
139 
140     /***
141      * Constructor.
142      *
143      * @param aValue
144      *            a string representation of a recurrence.
145      * @throws ParseException thrown when the specified string
146      * contains an invalid representation of an UNTIL date value
147      */
148     public Recur(final String aValue) throws ParseException {
149 
150         experimentalValues = new HashMap();
151 
152         for (StringTokenizer t = new StringTokenizer(aValue, ";="); t
153                 .hasMoreTokens();) {
154 
155             String token = t.nextToken();
156 
157             if (FREQ.equals(token)) {
158                 frequency = t.nextToken();
159             }
160             else if (UNTIL.equals(token)) {
161                 String untilString = t.nextToken();
162 
163                 try {
164                     until = DateFormat.getInstance().parse(untilString);
165                 }
166                 catch (ParseException pe) {
167                     until = DateTimeFormat.getInstance().parse(untilString);
168                 }
169             }
170             else if (COUNT.equals(token)) {
171                 count = Integer.parseInt(t.nextToken());
172             }
173             else if (INTERVAL.equals(token)) {
174                 interval = Integer.parseInt(t.nextToken());
175             }
176             else if (BYSECOND.equals(token)) {
177                 secondList = new NumberList(t.nextToken());
178             }
179             else if (BYMINUTE.equals(token)) {
180                 minuteList = new NumberList(t.nextToken());
181             }
182             else if (BYHOUR.equals(token)) {
183                 hourList = new NumberList(t.nextToken());
184             }
185             else if (BYDAY.equals(token)) {
186                 dayList = new DayList(t.nextToken());
187             }
188             else if (BYMONTHDAY.equals(token)) {
189                 monthDayList = new NumberList(t.nextToken());
190             }
191             else if (BYYEARDAY.equals(token)) {
192                 yearDayList = new NumberList(t.nextToken());
193             }
194             else if (BYWEEKNO.equals(token)) {
195                 weekNoList = new NumberList(t.nextToken());
196             }
197             else if (BYMONTH.equals(token)) {
198                 monthList = new NumberList(t.nextToken());
199             }
200             else if (BYSETPOS.equals(token)) {
201                 setPosList = new NumberList(t.nextToken());
202             }
203             else if (WKST.equals(token)) {
204                 weekStartDay = t.nextToken();
205             }
206             // assume experimental value..
207             else {
208                 experimentalValues.put(token, t.nextToken());
209             }
210         }
211     }
212 
213     /***
214      * @return Returns the dayList.
215      */
216     public final DayList getDayList() {
217         return dayList;
218     }
219 
220     /***
221      * @return Returns the hourList.
222      */
223     public final NumberList getHourList() {
224         return hourList;
225     }
226 
227     /***
228      * @return Returns the minuteList.
229      */
230     public final NumberList getMinuteList() {
231         return minuteList;
232     }
233 
234     /***
235      * @return Returns the monthDayList.
236      */
237     public final NumberList getMonthDayList() {
238         return monthDayList;
239     }
240 
241     /***
242      * @return Returns the monthList.
243      */
244     public final NumberList getMonthList() {
245         return monthList;
246     }
247 
248     /***
249      * @return Returns the secondList.
250      */
251     public final NumberList getSecondList() {
252         return secondList;
253     }
254 
255     /***
256      * @return Returns the setPosList.
257      */
258     public final NumberList getSetPosList() {
259         return setPosList;
260     }
261 
262     /***
263      * @return Returns the weekNoList.
264      */
265     public final NumberList getWeekNoList() {
266         return weekNoList;
267     }
268 
269     /***
270      * @return Returns the yearDayList.
271      */
272     public final NumberList getYearDayList() {
273         return yearDayList;
274     }
275 
276     /***
277      * @return Returns the count.
278      */
279     public final int getCount() {
280         return count;
281     }
282 
283     /***
284      * @return Returns the experimentalValues.
285      */
286     public final Map getExperimentalValues() {
287         return experimentalValues;
288     }
289 
290     /***
291      * @return Returns the frequency.
292      */
293     public final String getFrequency() {
294         return frequency;
295     }
296 
297     /***
298      * @return Returns the interval.
299      */
300     public final int getInterval() {
301         return interval;
302     }
303 
304     /***
305      * @return Returns the until.
306      */
307     public final Date getUntil() {
308         return until;
309     }
310 
311     /***
312      * @return Returns the weekStartDay.
313      */
314     public final String getWeekStartDay() {
315         return weekStartDay;
316     }
317 
318     /***
319      * @see java.lang.Object#toString()
320      */
321     public final String toString() {
322 
323         StringBuffer b = new StringBuffer();
324 
325         b.append(FREQ);
326         b.append('=');
327         b.append(frequency);
328 
329         if (interval >= 0) {
330             b.append(';');
331             b.append(INTERVAL);
332             b.append('=');
333             b.append(interval);
334         }
335 
336         if (until != null) {
337             b.append(';');
338             b.append(UNTIL);
339             b.append('=');
340             // although UNTIL may be either date or date-time,
341             // for now we'll assume its a date and output as such..
342             b.append(DateFormat.getInstance().format(until));
343         }
344 
345         if (count >= 0) {
346             b.append(';');
347             b.append(COUNT);
348             b.append('=');
349             b.append(count);
350         }
351 
352         if (secondList != null) {
353             b.append(';');
354             b.append(BYSECOND);
355             b.append('=');
356             b.append(secondList);
357         }
358 
359         if (minuteList != null) {
360             b.append(';');
361             b.append(BYMINUTE);
362             b.append('=');
363             b.append(minuteList);
364         }
365 
366         if (hourList != null) {
367             b.append(';');
368             b.append(BYHOUR);
369             b.append('=');
370             b.append(hourList);
371         }
372 
373         if (dayList != null) {
374             b.append(';');
375             b.append(BYDAY);
376             b.append('=');
377             b.append(dayList);
378         }
379 
380         if (monthDayList != null) {
381             b.append(';');
382             b.append(BYMONTHDAY);
383             b.append('=');
384             b.append(monthDayList);
385         }
386 
387         if (yearDayList != null) {
388             b.append(';');
389             b.append(BYYEARDAY);
390             b.append('=');
391             b.append(yearDayList);
392         }
393 
394         if (weekNoList != null) {
395             b.append(';');
396             b.append(BYWEEKNO);
397             b.append('=');
398             b.append(weekNoList);
399         }
400 
401         if (monthList != null) {
402             b.append(';');
403             b.append(BYMONTH);
404             b.append('=');
405             b.append(monthList);
406         }
407 
408         if (setPosList != null) {
409             b.append(';');
410             b.append(BYSETPOS);
411             b.append('=');
412             b.append(setPosList);
413         }
414 
415         if (weekStartDay != null) {
416             b.append(';');
417             b.append(WKST);
418             b.append('=');
419             b.append(weekStartDay);
420         }
421 
422         return b.toString();
423     }
424 }