-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
Trigger.java
333 lines (302 loc) · 12 KB
/
Trigger.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
/**
* 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.property;
import net.fortuna.ical4j.model.*;
import net.fortuna.ical4j.model.parameter.Value;
import net.fortuna.ical4j.validate.PropertyValidator;
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.ValidationRule;
import net.fortuna.ical4j.validate.Validator;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAmount;
import java.util.Optional;
import java.util.function.Predicate;
import static net.fortuna.ical4j.model.Parameter.RELATED;
import static net.fortuna.ical4j.model.Parameter.VALUE;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.None;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.OneOrLess;
/**
* $Id$
* <p/>
* Created: [Apr 6, 2004]
* <p/>
* Defines a TRIGGER iCalendar component property.
* <p/>
* <pre>
* 4.8.6.3 Trigger
*
* Property Name: TRIGGER
*
* Purpose: This property specifies when an alarm will trigger.
*
* Value Type: The default value type is DURATION. The value type can be
* set to a DATE-TIME value type, in which case the value MUST specify a
* UTC formatted DATE-TIME value.
*
* Property Parameters: Non-standard, value data type, time zone
* identifier or trigger relationship property parameters can be
* specified on this property. The trigger relationship property
* parameter MUST only be specified when the value type is DURATION.
*
* Conformance: This property MUST be specified in the "VALARM" calendar
* component.
*
* Description: Within the "VALARM" calendar component, this property
* defines when the alarm will trigger. The default value type is
* DURATION, specifying a relative time for the trigger of the alarm.
* The default duration is relative to the start of an event or to-do
* that the alarm is associated with. The duration can be explicitly set
*
* to trigger from either the end or the start of the associated event
* or to-do with the "RELATED" parameter. A value of START will set the
* alarm to trigger off the start of the associated event or to-do. A
* value of END will set the alarm to trigger off the end of the
* associated event or to-do.
*
* Either a positive or negative duration may be specified for the
* "TRIGGER" property. An alarm with a positive duration is triggered
* after the associated start or end of the event or to-do. An alarm
* with a negative duration is triggered before the associated start or
* end of the event or to-do.
*
* The "RELATED" property parameter is not valid if the value type of
* the property is set to DATE-TIME (i.e., for an absolute date and time
* alarm trigger). If a value type of DATE-TIME is specified, then the
* property value MUST be specified in the UTC time format. If an
* absolute trigger is specified on an alarm for a recurring event or
* to-do, then the alarm will only trigger for the specified absolute
* date/time, along with any specified repeating instances.
*
* If the trigger is set relative to START, then the "DTSTART" property
* MUST be present in the associated "VEVENT" or "VTODO" calendar
* component. If an alarm is specified for an event with the trigger set
* relative to the END, then the "DTEND" property or the "DSTART" and
* "DURATION' properties MUST be present in the associated "VEVENT"
* calendar component. If the alarm is specified for a to-do with a
* trigger set relative to the END, then either the "DUE" property or
* the "DSTART" and "DURATION' properties MUST be present in the
* associated "VTODO" calendar component.
*
* Alarms specified in an event or to-do which is defined in terms of a
* DATE value type will be triggered relative to 00:00:00 UTC on the
* specified date. For example, if "DTSTART:19980205, then the duration
* trigger will be relative to19980205T000000Z.
*
* Format Definition: The property is defined by the following notation:
*
* trigger = "TRIGGER" (trigrel / trigabs)
*
* trigrel = *(
*
* ; the following are optional,
* ; but MUST NOT occur more than once
*
* (";" "VALUE" "=" "DURATION") /
* (";" trigrelparam) /
*
* ; the following is optional,
* ; and MAY occur more than once
*
* (";" xparam)
* ) ":" dur-value
*
* trigabs = 1*(
*
* ; the following is REQUIRED,
* ; but MUST NOT occur more than once
*
* (";" "VALUE" "=" "DATE-TIME") /
*
* ; the following is optional,
* ; and MAY occur more than once
*
* (";" xparam)
*
* ) ":" date-time
* </pre>
*
* @author Ben Fortuna
*/
public class Trigger extends DateProperty<Instant> {
private static final long serialVersionUID = 5049421499261722194L;
private TemporalAmountAdapter duration;
private Validator<Trigger> validator = new PropertyValidator<>(
new ValidationRule<>(OneOrLess, VALUE),
new ValidationRule<>(None, (Predicate<Trigger> & Serializable) Trigger::isAbsolute, RELATED)
);
/**
* Default constructor.
*/
public Trigger() {
this(Instant.now());
}
/**
* @param aList a list of parameters for this component
* @param aValue a value string for this component
*/
public Trigger(final ParameterList aList, final String aValue) {
super(TRIGGER, aList, CalendarDateFormat.UTC_DATE_TIME_FORMAT);
setValue(aValue);
}
/**
* @param duration a duration in milliseconds
*/
@Deprecated
public Trigger(final Dur duration) {
this(TemporalAmountAdapter.from(duration));
}
/**
* @param duration a duration in milliseconds
*/
public Trigger(final TemporalAmount duration) {
this(new TemporalAmountAdapter(duration));
}
private Trigger(final TemporalAmountAdapter duration) {
super(TRIGGER, CalendarDateFormat.UTC_DATE_TIME_FORMAT);
this.duration = duration;
}
/**
* @param aList a list of parameters for this component
* @param duration a duration in milliseconds
*/
@Deprecated
public Trigger(final ParameterList aList, final Dur duration) {
this(aList, TemporalAmountAdapter.from(duration));
}
/**
* @param aList a list of parameters for this component
* @param duration a duration in milliseconds
*/
public Trigger(final ParameterList aList, final TemporalAmount duration) {
this(aList, new TemporalAmountAdapter(duration));
}
private Trigger(final ParameterList aList, final TemporalAmountAdapter duration) {
super(TRIGGER, aList, CalendarDateFormat.UTC_DATE_TIME_FORMAT);
this.duration = duration;
}
/**
* @param dateTime a date representation of a date-time
*/
public Trigger(final Instant dateTime) {
super(TRIGGER, CalendarDateFormat.UTC_DATE_TIME_FORMAT);
setDate(dateTime);
}
/**
* @param aList a list of parameters for this component
* @param dateTime a date representation of a date-time
*/
public Trigger(final ParameterList aList, final Instant dateTime) {
super(TRIGGER, aList, CalendarDateFormat.UTC_DATE_TIME_FORMAT);
setDate(dateTime);
}
/**
* Indicates whether the trigger is relative or absolute.
* @return true if the trigger is absolute
*/
public boolean isAbsolute() {
return Optional.of(Value.DATE_TIME).equals(getParameters().getFirst(VALUE));
}
/**
* {@inheritDoc}
*/
public final void validate() throws ValidationException {
super.validate();
validator.validate(this);
}
/**
* @return Returns the duration.
*/
public final TemporalAmount getDuration() {
if (duration != null) {
return duration.getDuration();
}
return null;
}
/**
* {@inheritDoc}
*/
public final void setValue(final String aValue) {
try {
super.setValue(aValue);
duration = null;
} catch (DateTimeParseException pe) {
LoggerFactory.getLogger(Trigger.class).warn(String.format("Not a valid DATE-TIME value: %s", aValue));
duration = TemporalAmountAdapter.parse(aValue);
super.setDate(null);
}
}
/**
* {@inheritDoc}
*/
public final String getValue() {
if (duration != null) {
return duration.toString();
}
return super.getValue();
}
/**
* @param dateTime The dateTime to set.
*/
public void setDate(final Instant dateTime) {
super.setDate(dateTime);
duration = null;
setParameters((ParameterList) getParameters().replace(Value.DATE_TIME));
}
/**
* @param duration The duration to set.
*/
public final void setDuration(final TemporalAmount duration) {
this.duration = new TemporalAmountAdapter(duration);
super.setDate(null);
// duration is the default value type for Trigger..
setParameters((ParameterList) getParameters().replace(Value.DURATION));
}
@Override
protected PropertyFactory<Trigger> newFactory() {
return new Factory();
}
public static class Factory extends Content.Factory implements PropertyFactory<Trigger> {
private static final long serialVersionUID = 1L;
public Factory() {
super(TRIGGER);
}
public Trigger createProperty(final ParameterList parameters, final String value) {
return new Trigger(parameters, value);
}
public Trigger createProperty() {
return new Trigger();
}
}
}