1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package net.fortuna.ical4j.model;
35
36 import java.text.ParseException;
37 import java.util.ArrayList;
38 import java.util.Date;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.StringTokenizer;
42
43 import net.fortuna.ical4j.model.parameter.Value;
44 import net.fortuna.ical4j.util.DateFormat;
45 import net.fortuna.ical4j.util.DateTimeFormat;
46
47 /***
48 * Defines a list of iCalendar dates.
49 * @author benfortuna
50 */
51 public class DateList {
52
53 private Value type;
54 private List dates;
55
56 /***
57 * Default constructor.
58 * @param aType specifies the type of dates (either date or
59 * date-time)
60 */
61 public DateList(final Value aType) {
62 dates = new ArrayList();
63
64 this.type = aType;
65 }
66
67 /***
68 * Parses the specified string representation to create
69 * a list of dates.
70 * @param aValue a string representation of a list of
71 * dates
72 * @param aType specifies the type of dates (either date or
73 * date-time)
74 * @throws ParseException if an invalid date representation
75 * exists in the date list string
76 */
77 public DateList(final String aValue, final Value aType)
78 throws ParseException {
79 dates = new ArrayList();
80
81 this.type = aType;
82
83 for (StringTokenizer t = new StringTokenizer(aValue, ","); t
84 .hasMoreTokens();) {
85 if (type != null && Value.DATE.equals(type.getValue())) {
86 dates.add(DateFormat.getInstance().parse(t.nextToken()));
87 }
88 else {
89 dates.add(DateTimeFormat.getInstance().parse(t.nextToken()));
90 }
91 }
92 }
93
94 /***
95 * @see java.util.AbstractCollection#toString()
96 */
97 public final String toString() {
98
99 StringBuffer b = new StringBuffer();
100
101 for (Iterator i = dates.iterator(); i.hasNext();) {
102
103 if (type != null && Value.DATE.equals(type.getValue())) {
104 b.append(DateFormat.getInstance().format((Date) i.next()));
105 }
106 else {
107 b.append(DateTimeFormat.getInstance().format((Date) i.next()));
108 }
109
110 if (i.hasNext()) {
111 b.append(',');
112 }
113 }
114
115 return b.toString();
116 }
117
118 /***
119 * Add a date to the list.
120 * @param date the date to add
121 * @return true
122 * @see List#add(java.lang.Object)
123 */
124 public final boolean add(final Date date) {
125 return dates.add(date);
126 }
127
128 /***
129 * @return boolean indicates if the list is empty
130 * @see List#isEmpty()
131 */
132 public final boolean isEmpty() {
133 return dates.isEmpty();
134 }
135
136 /***
137 * @return an iterator
138 * @see List#iterator()
139 */
140 public final Iterator iterator() {
141 return dates.iterator();
142 }
143
144 /***
145 * Remove a date from the list
146 * @param date the date to remove
147 * @return true if the list contained the specified date
148 * @see List#remove(java.lang.Object)
149 */
150 public final boolean remove(final Date date) {
151 return dates.remove(date);
152 }
153
154 /***
155 * @return the number of dates in the list
156 * @see List#size()
157 */
158 public final int size() {
159 return dates.size();
160 }
161
162 /***
163 * Returns the VALUE parameter specifying the type
164 * of dates (ie. date or date-time) stored in this
165 * date list.
166 * @return Returns a Value parameter.
167 */
168 public final Value getType() {
169 return type;
170 }
171 }