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.property;
35
36 import java.text.ParseException;
37 import java.util.Date;
38
39 import net.fortuna.ical4j.model.Parameter;
40 import net.fortuna.ical4j.model.ParameterList;
41 import net.fortuna.ical4j.model.Property;
42 import net.fortuna.ical4j.model.ValidationException;
43 import net.fortuna.ical4j.model.parameter.Value;
44 import net.fortuna.ical4j.util.DateTimeFormat;
45 import net.fortuna.ical4j.util.DurationFormat;
46 import net.fortuna.ical4j.util.ParameterValidator;
47
48 /***
49 * Defines a TRIGGER iCalendar component property.
50 *
51 * @author benf
52 */
53 public class Trigger extends Property {
54
55 private long duration;
56
57 /***
58 * The value type can be set to a DATE-TIME value type, in which case the
59 * value MUST specify a UTC formatted DATE-TIME value.
60 */
61 private Date dateTime;
62
63 /***
64 * @param aList
65 * a list of parameters for this component
66 * @param aValue
67 * a value string for this component
68 * @throws ParseException
69 * when the specified string is not a valid duration or
70 * date-time representation
71 */
72 public Trigger(final ParameterList aList, final String aValue)
73 throws ParseException {
74 super(TRIGGER, aList);
75
76 try {
77 duration = DurationFormat.getInstance().parse(aValue);
78 }
79 catch (Exception e) {
80 dateTime = DateTimeFormat.getInstance().parse(aValue);
81 }
82 }
83
84 /***
85 * @param aList
86 * a list of parameters for this component
87 * @param aDuration
88 * a duration in milliseconds
89 */
90 public Trigger(final ParameterList aList, final long aDuration) {
91 super(TRIGGER, aList);
92
93 duration = aDuration;
94 }
95
96 /***
97 * @param aList
98 * a list of parameters for this component
99 * @param aDate
100 * a date representation of a date-time
101 */
102 public Trigger(final ParameterList aList, final Date aDate) {
103 super(TRIGGER, aList);
104
105 dateTime = aDate;
106 }
107
108 /***
109 * @see net.fortuna.ical4j.model.Property#validate()
110 */
111 public final void validate() throws ValidationException {
112
113 if (duration > 0) {
114
115
116
117
118
119
120 ParameterValidator.getInstance().validateOneOrLess(Parameter.VALUE,
121 getParameters());
122
123 Parameter valueParam = getParameters()
124 .getParameter(Parameter.VALUE);
125
126 if (valueParam != null
127 && !Value.DURATION.equals(valueParam.getValue())) { throw new ValidationException(
128 "Parameter [" + Parameter.VALUE + "] is invalid"); }
129
130 ParameterValidator.getInstance().validateOneOrLess(
131 Parameter.RELATED, getParameters());
132
133
134
135
136
137
138
139 }
140 else {
141
142
143
144
145
146
147 ParameterValidator.getInstance().validateOne(Parameter.VALUE,
148 getParameters());
149
150 Parameter valueParam = getParameters()
151 .getParameter(Parameter.VALUE);
152
153 if (valueParam == null
154 || !Value.DATE_TIME.equals(valueParam.getValue())) { throw new ValidationException(
155 "Parameter [" + Parameter.VALUE + "] is invalid"); }
156
157
158
159
160
161
162 }
163 }
164
165 /***
166 * @return Returns the dateTime.
167 */
168 public final Date getDateTime() {
169 return dateTime;
170 }
171
172 /***
173 * @return Returns the duration.
174 */
175 public final long getDuration() {
176 return duration;
177 }
178
179
180
181
182
183
184 public String getValue() {
185 if (getDateTime() != null) {
186 return DateTimeFormat.getInstance().format(getDateTime());
187 }
188 else {
189 return DurationFormat.getInstance().format(getDuration());
190 }
191 }
192 }