/
Component.java
422 lines (365 loc) · 14.4 KB
/
Component.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* Copyright (c) 2012, Ben Fortuna
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* o Neither the name of Ben Fortuna nor the names of any other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.fortuna.ical4j.model;
import net.fortuna.ical4j.model.parameter.Value;
import net.fortuna.ical4j.model.property.*;
import net.fortuna.ical4j.util.Strings;
import net.fortuna.ical4j.validate.ValidationException;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.time.temporal.TemporalAmount;
import java.util.List;
import java.util.stream.Collectors;
/**
* $Id$ [Apr 5, 2004]
* <p/>
* Defines an iCalendar component. Subclasses of this class provide additional validation and typed values for specific
* iCalendar components.
*
* @author Ben Fortuna
*/
public abstract class Component implements Serializable {
private static final long serialVersionUID = 4943193483665822201L;
/**
* Component start token.
*/
public static final String BEGIN = "BEGIN";
/**
* Component end token.
*/
public static final String END = "END";
/**
* Component token.
*/
public static final String PARTICIPANT = "PARTICIPANT";
/**
* Component token.
*/
public static final String VLOCATION = "VLOCATION";
/**
* Component token.
*/
public static final String VRESOURCE = "VRESOURCE";
/**
* Component token.
*/
public static final String VEVENT = "VEVENT";
/**
* Component token.
*/
public static final String VTODO = "VTODO";
/**
* Component token.
*/
public static final String VJOURNAL = "VJOURNAL";
/**
* Component token.
*/
public static final String VFREEBUSY = "VFREEBUSY";
/**
* Component token.
*/
public static final String VTIMEZONE = "VTIMEZONE";
/**
* Component token.
*/
public static final String VALARM = "VALARM";
/**
* Component token.
*/
public static final String VAVAILABILITY = "VAVAILABILITY";
/**
* Component token.
*/
public static final String VVENUE = "VVENUE";
/**
* Component token.
*/
public static final String AVAILABLE = "AVAILABLE";
/**
* Prefix for non-standard components.
*/
public static final String EXPERIMENTAL_PREFIX = "X-";
private String name;
private PropertyList<Property> properties;
/**
* Constructs a new component containing no properties.
*
* @param s a component name
*/
protected Component(final String s) {
this(s, new PropertyList<Property>());
}
/**
* Constructor made protected to enforce the use of <code>ComponentFactory</code> for component instantiation.
*
* @param s component name
* @param p a list of properties
*/
protected Component(final String s, final PropertyList<Property> p) {
this.name = s;
this.properties = p;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return BEGIN +
':' +
getName() +
Strings.LINE_SEPARATOR +
getProperties() +
END +
':' +
getName() +
Strings.LINE_SEPARATOR;
}
/**
* @return Returns the name.
*/
public final String getName() {
return name;
}
/**
* @return Returns the properties.
*/
public final PropertyList<Property> getProperties() {
return properties;
}
/**
* Convenience method for retrieving a list of named properties.
*
* @param name name of properties to retrieve
* @return a property list containing only properties with the specified name
*/
public final <C extends Property> PropertyList<C> getProperties(final String name) {
return getProperties().getProperties(name);
}
/**
* Convenience method for retrieving a named property.
*
* @param name name of the property to retrieve
* @return the first matching property in the property list with the specified name
*/
public final <T extends Property> T getProperty(final String name) {
return (T) getProperties().getProperty(name);
}
/**
* Convenience method for retrieving a required named property.
*
* @param name name of the property to retrieve
* @return the first matching property in the property list with the specified name
* @throws ConstraintViolationException when a property is not found
*/
protected final Property getRequiredProperty(String name) throws ConstraintViolationException {
Property p = getProperties().getProperty(name);
if (p == null) {
throw new ConstraintViolationException(String.format("Missing %s property", name));
}
return p;
}
/**
* Perform validation on a component and its properties.
*
* @throws ValidationException where the component is not in a valid state
*/
public final void validate() throws ValidationException {
validate(true);
}
/**
* Perform validation on a component.
*
* @param recurse indicates whether to validate the component's properties
* @throws ValidationException where the component is not in a valid state
*/
public abstract void validate(final boolean recurse)
throws ValidationException;
/**
* Invoke validation on the component properties in its current state.
*
* @throws ValidationException where any of the component properties is not in a valid state
*/
protected final void validateProperties() throws ValidationException {
for (final Property property : getProperties()) {
property.validate();
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(final Object arg0) {
if (arg0 instanceof Component) {
final Component c = (Component) arg0;
return new EqualsBuilder().append(getName(), c.getName())
.append(getProperties(), c.getProperties()).isEquals();
}
return super.equals(arg0);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return new HashCodeBuilder().append(getName()).append(getProperties())
.toHashCode();
}
/**
* Create a (deep) copy of this component.
*
* @return the component copy
* @throws IOException where an error occurs reading the component data
* @throws ParseException where parsing component data fails
* @throws URISyntaxException where component data contains an invalid URI
*/
public Component copy() throws ParseException, IOException,
URISyntaxException {
// Deep copy properties..
final PropertyList<Property> newprops = new PropertyList<Property>(getProperties());
return new ComponentFactoryImpl().createComponent(getName(),
newprops);
}
/**
* Calculates the recurrence set for this component using the specified period.
* The recurrence set is derived from a combination of the component start date,
* recurrence rules and dates, and exception rules and dates. Note that component
* transparency and anniversary-style dates do not affect the resulting
* intersection.
* <p>If an explicit DURATION is not specified, the effective duration of each
* returned period is derived from the DTSTART and DTEND or DUE properties.
* If the component has no DURATION, DTEND or DUE, the effective duration is set
* to PT0S</p>
*
* @param period a range to calculate recurrences for
* @return a list of periods
*/
public final PeriodList calculateRecurrenceSet(final Period period) {
// validate();
final PeriodList recurrenceSet = new PeriodList();
final DtStart start = getProperty(Property.DTSTART);
DateProperty end = getProperty(Property.DTEND);
if (end == null) {
end = getProperty(Property.DUE);
}
Duration duration = getProperty(Property.DURATION);
// if no start date specified return empty list..
if (start == null) {
return recurrenceSet;
}
final Value startValue = start.getParameter(Parameter.VALUE);
// initialise timezone..
// if (startValue == null || Value.DATE_TIME.equals(startValue)) {
if (start.isUtc()) {
recurrenceSet.setUtc(true);
} else if (start.getDate() instanceof DateTime) {
recurrenceSet.setTimeZone(((DateTime) start.getDate()).getTimeZone());
}
// if an explicit event duration is not specified, derive a value for recurring
// periods from the end date..
TemporalAmount rDuration;
// if no end or duration specified, end date equals start date..
if (end == null && duration == null) {
rDuration = java.time.Duration.ZERO;
} else if (duration == null) {
rDuration = TemporalAmountAdapter.fromDateRange(start.getDate(), end.getDate()).getDuration();
} else {
rDuration = duration.getDuration();
}
// add recurrence dates..
List<RDate> rDates = getProperties(Property.RDATE);
recurrenceSet.addAll(rDates.stream().filter(p -> p.getParameter(Parameter.VALUE) == Value.PERIOD)
.map(p -> p.getPeriods()).flatMap(PeriodList::stream).filter(rdatePeriod -> period.intersects(rdatePeriod))
.collect(Collectors.toList()));
recurrenceSet.addAll(rDates.stream().filter(p -> p.getParameter(Parameter.VALUE) == Value.DATE_TIME)
.map(p -> p.getDates()).flatMap(DateList::stream).filter(date -> period.includes(date))
.map(rdateTime -> new Period((DateTime) rdateTime, rDuration)).collect(Collectors.toList()));
recurrenceSet.addAll(rDates.stream().filter(p -> p.getParameter(Parameter.VALUE) == Value.DATE)
.map(p -> p.getDates()).flatMap(DateList::stream).filter(date -> period.includes(date))
.map(rdateDate -> new Period(new DateTime(rdateDate), rDuration)).collect(Collectors.toList()));
// allow for recurrence rules that start prior to the specified period
// but still intersect with it..
final DateTime startMinusDuration = new DateTime(period.getStart());
startMinusDuration.setTime(Date.from(period.getStart().toInstant().minus(rDuration)).getTime());
// add recurrence rules..
List<RRule> rRules = getProperties(Property.RRULE);
if (!rRules.isEmpty()) {
recurrenceSet.addAll(rRules.stream().map(r -> r.getRecur().getDates(start.getDate(),
new Period(startMinusDuration, period.getEnd()), startValue)).flatMap(DateList::stream)
.map(rruleDate -> new Period(new DateTime(rruleDate), rDuration)).collect(Collectors.toList()));
} else {
// add initial instance if intersection with the specified period..
Period startPeriod;
if (end != null) {
startPeriod = new Period(new DateTime(start.getDate()),
new DateTime(end.getDate()));
} else {
/*
* PeS: Anniversary type has no DTEND nor DUR, define DUR
* locally, otherwise we get NPE
*/
if (duration == null) {
duration = new Duration(rDuration);
}
startPeriod = new Period(new DateTime(start.getDate()),
duration.getDuration());
}
if (period.intersects(startPeriod)) {
recurrenceSet.add(startPeriod);
}
}
// subtract exception dates..
List<ExDate> exDateProps = getProperties(Property.EXDATE);
List<Date> exDates = exDateProps.stream().map(e -> e.getDates()).flatMap(DateList::stream)
.collect(Collectors.toList());
recurrenceSet.removeIf(recurrence -> {
// for DATE-TIME instances check for DATE-based exclusions also..
return exDates.contains(recurrence.getStart()) || exDates.contains(new Date(recurrence.getStart()));
});
// subtract exception rules..
List<ExRule> exRules = getProperties(Property.EXRULE);
List<Date> exRuleDates = exRules.stream().map(e -> e.getRecur().getDates(start.getDate(),
period, startValue)).flatMap(DateList::stream).collect(Collectors.toList());
recurrenceSet.removeIf(recurrence -> {
// for DATE-TIME instances check for DATE-based exclusions also..
return exRuleDates.contains(recurrence.getStart())
|| exRuleDates.contains(new Date(recurrence.getStart()));
});
// set a link to the origin
recurrenceSet.forEach( p -> p.setComponent(this));
return recurrenceSet;
}
}