|
1 /** |
|
2 * Copyright (c) 2012, Ben Fortuna |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * o Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * o Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * o Neither the name of Ben Fortuna nor the names of any other contributors |
|
17 * may be used to endorse or promote products derived from this software |
|
18 * without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package net.fortuna.ical4j.model; |
|
33 |
|
34 import java.io.Serializable; |
|
35 import java.util.ArrayList; |
|
36 import java.util.Iterator; |
|
37 import java.util.StringTokenizer; |
|
38 |
|
39 import net.fortuna.ical4j.util.CompatibilityHints; |
|
40 |
|
41 /** |
|
42 * $Id$ [29-May-2004] |
|
43 * |
|
44 * Defines a list of days. |
|
45 * |
|
46 * @author Ben Fortuna |
|
47 */ |
|
48 public class WeekDayList extends ArrayList implements Serializable { |
|
49 |
|
50 private static final long serialVersionUID = 1243262497035300445L; |
|
51 |
|
52 /** |
|
53 * Default constructor. |
|
54 */ |
|
55 public WeekDayList() { |
|
56 } |
|
57 |
|
58 /** |
|
59 * Creates a new instance with the specified initial capacity. |
|
60 * @param initialCapacity the initial capacity of the list |
|
61 */ |
|
62 public WeekDayList(final int initialCapacity) { |
|
63 super(initialCapacity); |
|
64 } |
|
65 |
|
66 /** |
|
67 * Constructor. |
|
68 * @param aString a string representation of a day list |
|
69 */ |
|
70 public WeekDayList(final String aString) { |
|
71 final boolean outlookCompatibility = |
|
72 CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_OUTLOOK_COMPATIBILITY); |
|
73 |
|
74 final StringTokenizer t = new StringTokenizer(aString, ","); |
|
75 while (t.hasMoreTokens()) { |
|
76 if (outlookCompatibility) { |
|
77 add(new WeekDay(t.nextToken().replaceAll(" ", ""))); |
|
78 } |
|
79 else { |
|
80 add(new WeekDay(t.nextToken())); |
|
81 } |
|
82 } |
|
83 } |
|
84 |
|
85 /** |
|
86 * @param weekDay a day to add to the list |
|
87 * @return true if the week day is added, otherwise false |
|
88 */ |
|
89 public final boolean add(final WeekDay weekDay) { |
|
90 return add((Object) weekDay); |
|
91 } |
|
92 |
|
93 /** |
|
94 * Overrides superclass to throw an <code>IllegalArgumentException</code> |
|
95 * where argument is not a <code>net.fortuna.ical4j.model.WeekDay</code>. |
|
96 * @param weekday a week day to add |
|
97 * @return true if the week day is added, otherwise false |
|
98 * @see List#add(E) |
|
99 */ |
|
100 public final boolean add(final Object weekday) { |
|
101 if (!(weekday instanceof WeekDay)) { |
|
102 throw new IllegalArgumentException("Argument not a " + WeekDay.class.getName()); |
|
103 } |
|
104 return super.add(weekday); |
|
105 } |
|
106 |
|
107 /** |
|
108 * @param weekDay a day to remove from the list |
|
109 * @return true if the week day is removed, otherwise false |
|
110 */ |
|
111 public final boolean remove(final WeekDay weekDay) { |
|
112 return remove((Object) weekDay); |
|
113 } |
|
114 |
|
115 /** |
|
116 * {@inheritDoc} |
|
117 */ |
|
118 public final String toString() { |
|
119 final StringBuffer b = new StringBuffer(); |
|
120 for (final Iterator i = iterator(); i.hasNext();) { |
|
121 b.append(i.next()); |
|
122 if (i.hasNext()) { |
|
123 b.append(','); |
|
124 } |
|
125 } |
|
126 return b.toString(); |
|
127 } |
|
128 } |