-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
VAlarm.java
393 lines (363 loc) · 12.6 KB
/
VAlarm.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
/**
* 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.component;
import net.fortuna.ical4j.model.ComponentFactory;
import net.fortuna.ical4j.model.ComponentList;
import net.fortuna.ical4j.model.Content;
import net.fortuna.ical4j.model.PropertyList;
import net.fortuna.ical4j.model.property.*;
import net.fortuna.ical4j.validate.ComponentValidator;
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.ValidationRule;
import net.fortuna.ical4j.validate.Validator;
import java.io.Serializable;
import java.time.Instant;
import java.time.temporal.TemporalAmount;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import static net.fortuna.ical4j.model.Property.*;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.*;
/**
* $Id$ [Apr 5, 2004]
*
* Defines an iCalendar VALARM component.
*
* <pre>
* 4.6.6 Alarm Component
*
* Component Name: VALARM
*
* Purpose: Provide a grouping of component properties that define an
* alarm.
*
* Formal Definition: A "VALARM" calendar component is defined by the
* following notation:
*
* alarmc = "BEGIN" ":" "VALARM" CRLF
* (audioprop / dispprop / emailprop / procprop)
* "END" ":" "VALARM" CRLF
*
* audioprop = 2*(
*
* ; 'action' and 'trigger' are both REQUIRED,
* ; but MUST NOT occur more than once
*
* action / trigger /
*
* ; 'duration' and 'repeat' are both optional,
* ; and MUST NOT occur more than once each,
* ; but if one occurs, so MUST the other
*
* duration / repeat /
*
* ; the following is optional,
* ; but MUST NOT occur more than once
*
* attach /
*
* ; the following is optional,
* ; and MAY occur more than once
*
* x-prop
*
* )
*
*
*
* dispprop = 3*(
*
* ; the following are all REQUIRED,
* ; but MUST NOT occur more than once
*
* action / description / trigger /
*
* ; 'duration' and 'repeat' are both optional,
* ; and MUST NOT occur more than once each,
* ; but if one occurs, so MUST the other
*
* duration / repeat /
*
* ; the following is optional,
* ; and MAY occur more than once
*
* *x-prop
*
* )
*
*
*
* emailprop = 5*(
*
* ; the following are all REQUIRED,
* ; but MUST NOT occur more than once
*
* action / description / trigger / summary
*
* ; the following is REQUIRED,
* ; and MAY occur more than once
*
* attendee /
*
* ; 'duration' and 'repeat' are both optional,
* ; and MUST NOT occur more than once each,
* ; but if one occurs, so MUST the other
*
* duration / repeat /
*
* ; the following are optional,
* ; and MAY occur more than once
*
* attach / x-prop
*
* )
*
*
*
* procprop = 3*(
*
* ; the following are all REQUIRED,
* ; but MUST NOT occur more than once
*
* action / attach / trigger /
*
* ; 'duration' and 'repeat' are both optional,
* ; and MUST NOT occur more than once each,
* ; but if one occurs, so MUST the other
*
* duration / repeat /
*
* ; 'description' is optional,
* ; and MUST NOT occur more than once
*
* description /
*
* ; the following is optional,
* ; and MAY occur more than once
*
* x-prop
*
* )
* </pre>
*
* Example 1 - Creating an alarm to trigger at a specific time:
*
* <pre><code>
* java.util.Calendar cal = java.util.Calendar.getInstance();
* cal.set(java.util.Calendar.MONTH, java.util.Calendar.DECEMBER);
* cal.set(java.util.Calendar.DAY_OF_MONTH, 25);
*
* VAlarm christmas = new VAlarm(cal.getTime());
* </code></pre>
*
* Example 2 - Creating an alarm to trigger one (1) hour before the scheduled start of the parent event/the parent todo
* is due:
*
* <pre><code>
* VAlarm reminder = new VAlarm(new Dur(0, -1, 0, 0));
*
* // repeat reminder four (4) more times every fifteen (15) minutes..
* reminder.add(new Repeat(4));
* reminder.add(new Duration(new Dur(0, 0, 15, 0)));
*
* // display a message..
* reminder.add(Action.DISPLAY);
* reminder.add(new Description("Progress Meeting at 9:30am"));
* </code></pre>
*
* @author Ben Fortuna
*/
public class VAlarm extends CalendarComponent {
private static final long serialVersionUID = -8193965477414653802L;
private final Map<Action, Validator> actionValidators = new HashMap<Action, Validator>();
{
actionValidators.put(Action.AUDIO, new ComponentValidator<VAlarm>(new ValidationRule(OneOrLess, ATTACH)));
actionValidators.put(Action.DISPLAY, new ComponentValidator<VAlarm>(new ValidationRule(One, DESCRIPTION)));
actionValidators.put(Action.EMAIL, new ComponentValidator<VAlarm>(new ValidationRule(One, DESCRIPTION, SUMMARY),
new ValidationRule(OneOrMore, ATTENDEE)));
actionValidators.put(Action.PROCEDURE, new ComponentValidator<VAlarm>(new ValidationRule(One, ATTACH),
new ValidationRule(OneOrLess, DESCRIPTION)));
}
private Validator<VAlarm> vAlarmValidator = new ComponentValidator<>(
new ValidationRule<>(One, ACTION, TRIGGER),
new ValidationRule<>(OneOrLess, DURATION, REPEAT),
// DURATION and REPEAT must both be present or both absent
new ValidationRule<>(One,
(Predicate<VAlarm> & Serializable) p->p.getProperties().getFirst(DURATION).isPresent(), REPEAT)
);
/**
* Default constructor.
*/
public VAlarm() {
super(VALARM);
}
/**
* Constructor.
* @param properties a list of properties
*/
public VAlarm(final PropertyList properties) {
super(VALARM, properties);
}
/**
* Constructs a new VALARM instance that will trigger at the specified time.
* @param trigger the time the alarm will trigger
*/
public VAlarm(final Instant trigger) {
this();
add(new Trigger(trigger));
}
/**
* Constructs a new VALARM instance that will trigger at the specified time relative to the event/todo component.
* @param trigger a duration of time relative to the parent component that the alarm will trigger at
*/
public VAlarm(final TemporalAmount trigger) {
this();
add(new Trigger(trigger));
}
/**
* {@inheritDoc}
*/
public final void validate(final boolean recurse)
throws ValidationException {
/*
* ; 'action' and 'trigger' are both REQUIRED, ; but MUST NOT occur more than once action / trigger /
*/
/*
* ; 'duration' and 'repeat' are both optional, ; and MUST NOT occur more than once each, ; but if one occurs,
* so MUST the other duration / repeat /
*/
/*
* ; the following is optional, ; and MAY occur more than once x-prop
*/
Optional<Action> action = getProperties().getFirst(ACTION);
if (action.isPresent()) {
final Validator<VAlarm> actionValidator = actionValidators.get(action.get());
if (actionValidator != null) {
actionValidator.validate(this);
}
}
if (recurse) {
validateProperties();
}
}
/**
* {@inheritDoc}
*/
protected Validator<CalendarComponent> getValidator(Method method) {
throw new UnsupportedOperationException("VALARM validation included in VEVENT or VTODO method validator.");
}
/**
* Returns the mandatory action property.
* @return the ACTION property or null if not specified
* @deprecated use {@link VAlarm#getProperty(String)}
*/
@Deprecated
public final Optional<Action> getAction() {
return getProperty(ACTION);
}
/**
* Returns the mandatory trigger property.
* @return the TRIGGER property or null if not specified
* @deprecated use {@link VAlarm#getProperty(String)}
*/
@Deprecated
public final Optional<Trigger> getTrigger() {
return getProperty(TRIGGER);
}
/**
* Returns the optional duration property.
* @return the DURATION property or null if not specified
* @deprecated use {@link VAlarm#getProperty(String)}
*/
@Deprecated
public final Optional<Duration> getDuration() {
return getProperty(DURATION);
}
/**
* Returns the optional repeat property.
* @return the REPEAT property or null if not specified
* @deprecated use {@link VAlarm#getProperty(String)}
*/
@Deprecated
public final Optional<Repeat> getRepeat() {
return getProperty(REPEAT);
}
/**
* Returns the optional attachment property.
* @return the ATTACH property or null if not specified
* @deprecated use {@link VAlarm#getProperty(String)}
*/
@Deprecated
public final Optional<Attach> getAttachment() {
return getProperty(ATTACH);
}
/**
* Returns the optional description property.
* @return the DESCRIPTION property or null if not specified
* @deprecated use {@link VAlarm#getProperty(String)}
*/
@Deprecated
public final Optional<Description> getDescription() {
return getProperty(DESCRIPTION);
}
/**
* Returns the optional summary property.
* @return the SUMMARY property or null if not specified
* @deprecated use {@link VAlarm#getProperty(String)}
*/
@Deprecated
public final Optional<Summary> getSummary() {
return getProperty(SUMMARY);
}
@Override
protected ComponentFactory<VAlarm> newFactory() {
return new Factory();
}
public static class Factory extends Content.Factory implements ComponentFactory<VAlarm> {
public Factory() {
super(VALARM);
}
@Override
public VAlarm createComponent() {
return new VAlarm();
}
@Override
public VAlarm createComponent(PropertyList properties) {
return new VAlarm(properties);
}
@Override
public VAlarm createComponent(PropertyList properties, ComponentList<?> subComponents) {
throw new UnsupportedOperationException(String.format("%s does not support sub-components", VALARM));
}
}
}