src/net/fortuna/ical4j/model/component/VAlarm.java

Tue, 10 Feb 2015 19:58:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 19:58:00 +0100
changeset 4
45d57ecba757
permissions
-rw-r--r--

Upgrade the upgraded ical4j component to use org.apache.commons.lang3.

michael@0 1 /**
michael@0 2 * Copyright (c) 2012, Ben Fortuna
michael@0 3 * All rights reserved.
michael@0 4 *
michael@0 5 * Redistribution and use in source and binary forms, with or without
michael@0 6 * modification, are permitted provided that the following conditions
michael@0 7 * are met:
michael@0 8 *
michael@0 9 * o Redistributions of source code must retain the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer.
michael@0 11 *
michael@0 12 * o Redistributions in binary form must reproduce the above copyright
michael@0 13 * notice, this list of conditions and the following disclaimer in the
michael@0 14 * documentation and/or other materials provided with the distribution.
michael@0 15 *
michael@0 16 * o Neither the name of Ben Fortuna nor the names of any other contributors
michael@0 17 * may be used to endorse or promote products derived from this software
michael@0 18 * without specific prior written permission.
michael@0 19 *
michael@0 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
michael@0 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
michael@0 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
michael@0 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
michael@0 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31 */
michael@0 32 package net.fortuna.ical4j.model.component;
michael@0 33
michael@0 34 import java.util.HashMap;
michael@0 35 import java.util.Map;
michael@0 36
michael@0 37 import net.fortuna.ical4j.model.DateTime;
michael@0 38 import net.fortuna.ical4j.model.Dur;
michael@0 39 import net.fortuna.ical4j.model.Property;
michael@0 40 import net.fortuna.ical4j.model.PropertyList;
michael@0 41 import net.fortuna.ical4j.model.ValidationException;
michael@0 42 import net.fortuna.ical4j.model.Validator;
michael@0 43 import net.fortuna.ical4j.model.property.Action;
michael@0 44 import net.fortuna.ical4j.model.property.Attach;
michael@0 45 import net.fortuna.ical4j.model.property.Description;
michael@0 46 import net.fortuna.ical4j.model.property.Duration;
michael@0 47 import net.fortuna.ical4j.model.property.Method;
michael@0 48 import net.fortuna.ical4j.model.property.Repeat;
michael@0 49 import net.fortuna.ical4j.model.property.Summary;
michael@0 50 import net.fortuna.ical4j.model.property.Trigger;
michael@0 51 import net.fortuna.ical4j.util.PropertyValidator;
michael@0 52
michael@0 53 /**
michael@0 54 * $Id$ [Apr 5, 2004]
michael@0 55 *
michael@0 56 * Defines an iCalendar VALARM component.
michael@0 57 *
michael@0 58 * <pre>
michael@0 59 * 4.6.6 Alarm Component
michael@0 60 *
michael@0 61 * Component Name: VALARM
michael@0 62 *
michael@0 63 * Purpose: Provide a grouping of component properties that define an
michael@0 64 * alarm.
michael@0 65 *
michael@0 66 * Formal Definition: A &quot;VALARM&quot; calendar component is defined by the
michael@0 67 * following notation:
michael@0 68 *
michael@0 69 * alarmc = &quot;BEGIN&quot; &quot;:&quot; &quot;VALARM&quot; CRLF
michael@0 70 * (audioprop / dispprop / emailprop / procprop)
michael@0 71 * &quot;END&quot; &quot;:&quot; &quot;VALARM&quot; CRLF
michael@0 72 *
michael@0 73 * audioprop = 2*(
michael@0 74 *
michael@0 75 * ; 'action' and 'trigger' are both REQUIRED,
michael@0 76 * ; but MUST NOT occur more than once
michael@0 77 *
michael@0 78 * action / trigger /
michael@0 79 *
michael@0 80 * ; 'duration' and 'repeat' are both optional,
michael@0 81 * ; and MUST NOT occur more than once each,
michael@0 82 * ; but if one occurs, so MUST the other
michael@0 83 *
michael@0 84 * duration / repeat /
michael@0 85 *
michael@0 86 * ; the following is optional,
michael@0 87 * ; but MUST NOT occur more than once
michael@0 88 *
michael@0 89 * attach /
michael@0 90 *
michael@0 91 * ; the following is optional,
michael@0 92 * ; and MAY occur more than once
michael@0 93 *
michael@0 94 * x-prop
michael@0 95 *
michael@0 96 * )
michael@0 97 *
michael@0 98 *
michael@0 99 *
michael@0 100 * dispprop = 3*(
michael@0 101 *
michael@0 102 * ; the following are all REQUIRED,
michael@0 103 * ; but MUST NOT occur more than once
michael@0 104 *
michael@0 105 * action / description / trigger /
michael@0 106 *
michael@0 107 * ; 'duration' and 'repeat' are both optional,
michael@0 108 * ; and MUST NOT occur more than once each,
michael@0 109 * ; but if one occurs, so MUST the other
michael@0 110 *
michael@0 111 * duration / repeat /
michael@0 112 *
michael@0 113 * ; the following is optional,
michael@0 114 * ; and MAY occur more than once
michael@0 115 *
michael@0 116 * *x-prop
michael@0 117 *
michael@0 118 * )
michael@0 119 *
michael@0 120 *
michael@0 121 *
michael@0 122 * emailprop = 5*(
michael@0 123 *
michael@0 124 * ; the following are all REQUIRED,
michael@0 125 * ; but MUST NOT occur more than once
michael@0 126 *
michael@0 127 * action / description / trigger / summary
michael@0 128 *
michael@0 129 * ; the following is REQUIRED,
michael@0 130 * ; and MAY occur more than once
michael@0 131 *
michael@0 132 * attendee /
michael@0 133 *
michael@0 134 * ; 'duration' and 'repeat' are both optional,
michael@0 135 * ; and MUST NOT occur more than once each,
michael@0 136 * ; but if one occurs, so MUST the other
michael@0 137 *
michael@0 138 * duration / repeat /
michael@0 139 *
michael@0 140 * ; the following are optional,
michael@0 141 * ; and MAY occur more than once
michael@0 142 *
michael@0 143 * attach / x-prop
michael@0 144 *
michael@0 145 * )
michael@0 146 *
michael@0 147 *
michael@0 148 *
michael@0 149 * procprop = 3*(
michael@0 150 *
michael@0 151 * ; the following are all REQUIRED,
michael@0 152 * ; but MUST NOT occur more than once
michael@0 153 *
michael@0 154 * action / attach / trigger /
michael@0 155 *
michael@0 156 * ; 'duration' and 'repeat' are both optional,
michael@0 157 * ; and MUST NOT occur more than once each,
michael@0 158 * ; but if one occurs, so MUST the other
michael@0 159 *
michael@0 160 * duration / repeat /
michael@0 161 *
michael@0 162 * ; 'description' is optional,
michael@0 163 * ; and MUST NOT occur more than once
michael@0 164 *
michael@0 165 * description /
michael@0 166 *
michael@0 167 * ; the following is optional,
michael@0 168 * ; and MAY occur more than once
michael@0 169 *
michael@0 170 * x-prop
michael@0 171 *
michael@0 172 * )
michael@0 173 * </pre>
michael@0 174 *
michael@0 175 * Example 1 - Creating an alarm to trigger at a specific time:
michael@0 176 *
michael@0 177 * <pre><code>
michael@0 178 * java.util.Calendar cal = java.util.Calendar.getInstance();
michael@0 179 * cal.set(java.util.Calendar.MONTH, java.util.Calendar.DECEMBER);
michael@0 180 * cal.set(java.util.Calendar.DAY_OF_MONTH, 25);
michael@0 181 *
michael@0 182 * VAlarm christmas = new VAlarm(cal.getTime());
michael@0 183 * </code></pre>
michael@0 184 *
michael@0 185 * Example 2 - Creating an alarm to trigger one (1) hour before the scheduled start of the parent event/the parent todo
michael@0 186 * is due:
michael@0 187 *
michael@0 188 * <pre><code>
michael@0 189 * VAlarm reminder = new VAlarm(new Dur(0, -1, 0, 0));
michael@0 190 *
michael@0 191 * // repeat reminder four (4) more times every fifteen (15) minutes..
michael@0 192 * reminder.getProperties().add(new Repeat(4));
michael@0 193 * reminder.getProperties().add(new Duration(new Dur(0, 0, 15, 0)));
michael@0 194 *
michael@0 195 * // display a message..
michael@0 196 * reminder.getProperties().add(Action.DISPLAY);
michael@0 197 * reminder.getProperties().add(new Description(&quot;Progress Meeting at 9:30am&quot;));
michael@0 198 * </code></pre>
michael@0 199 *
michael@0 200 * @author Ben Fortuna
michael@0 201 */
michael@0 202 public class VAlarm extends CalendarComponent {
michael@0 203
michael@0 204 private static final long serialVersionUID = -8193965477414653802L;
michael@0 205
michael@0 206 private final Map actionValidators = new HashMap();
michael@0 207 {
michael@0 208 actionValidators.put(Action.AUDIO, new AudioValidator());
michael@0 209 actionValidators.put(Action.DISPLAY, new DisplayValidator());
michael@0 210 actionValidators.put(Action.EMAIL, new EmailValidator());
michael@0 211 actionValidators.put(Action.PROCEDURE, new ProcedureValidator());
michael@0 212 }
michael@0 213
michael@0 214 private final Validator itipValidator = new ITIPValidator();
michael@0 215
michael@0 216 /**
michael@0 217 * Default constructor.
michael@0 218 */
michael@0 219 public VAlarm() {
michael@0 220 super(VALARM);
michael@0 221 }
michael@0 222
michael@0 223 /**
michael@0 224 * Constructor.
michael@0 225 * @param properties a list of properties
michael@0 226 */
michael@0 227 public VAlarm(final PropertyList properties) {
michael@0 228 super(VALARM, properties);
michael@0 229 }
michael@0 230
michael@0 231 /**
michael@0 232 * Constructs a new VALARM instance that will trigger at the specified time.
michael@0 233 * @param trigger the time the alarm will trigger
michael@0 234 */
michael@0 235 public VAlarm(final DateTime trigger) {
michael@0 236 this();
michael@0 237 getProperties().add(new Trigger(trigger));
michael@0 238 }
michael@0 239
michael@0 240 /**
michael@0 241 * Constructs a new VALARM instance that will trigger at the specified time relative to the event/todo component.
michael@0 242 * @param trigger a duration of time relative to the parent component that the alarm will trigger at
michael@0 243 */
michael@0 244 public VAlarm(final Dur trigger) {
michael@0 245 this();
michael@0 246 getProperties().add(new Trigger(trigger));
michael@0 247 }
michael@0 248
michael@0 249 /**
michael@0 250 * {@inheritDoc}
michael@0 251 */
michael@0 252 public final void validate(final boolean recurse)
michael@0 253 throws ValidationException {
michael@0 254
michael@0 255 /*
michael@0 256 * ; 'action' and 'trigger' are both REQUIRED, ; but MUST NOT occur more than once action / trigger /
michael@0 257 */
michael@0 258 PropertyValidator.getInstance().assertOne(Property.ACTION, getProperties());
michael@0 259 PropertyValidator.getInstance().assertOne(Property.TRIGGER, getProperties());
michael@0 260
michael@0 261 /*
michael@0 262 * ; 'duration' and 'repeat' are both optional, ; and MUST NOT occur more than once each, ; but if one occurs,
michael@0 263 * so MUST the other duration / repeat /
michael@0 264 */
michael@0 265 PropertyValidator.getInstance().assertOneOrLess(Property.DURATION, getProperties());
michael@0 266 PropertyValidator.getInstance().assertOneOrLess(Property.REPEAT, getProperties());
michael@0 267
michael@0 268 try {
michael@0 269 PropertyValidator.getInstance().assertNone(Property.DURATION, getProperties());
michael@0 270 PropertyValidator.getInstance().assertNone(Property.REPEAT, getProperties());
michael@0 271 }
michael@0 272 catch (ValidationException ve) {
michael@0 273 PropertyValidator.getInstance().assertOne(Property.DURATION, getProperties());
michael@0 274 PropertyValidator.getInstance().assertOne(Property.REPEAT, getProperties());
michael@0 275 }
michael@0 276
michael@0 277 /*
michael@0 278 * ; the following is optional, ; and MAY occur more than once x-prop
michael@0 279 */
michael@0 280
michael@0 281 final Validator actionValidator = (Validator) actionValidators.get(getAction());
michael@0 282 if (actionValidator != null) {
michael@0 283 actionValidator.validate();
michael@0 284 }
michael@0 285
michael@0 286 if (recurse) {
michael@0 287 validateProperties();
michael@0 288 }
michael@0 289 }
michael@0 290
michael@0 291 /**
michael@0 292 * {@inheritDoc}
michael@0 293 */
michael@0 294 protected Validator getValidator(Method method) {
michael@0 295 return itipValidator;
michael@0 296 }
michael@0 297
michael@0 298 private class AudioValidator implements Validator {
michael@0 299
michael@0 300 private static final long serialVersionUID = 1L;
michael@0 301
michael@0 302 /**
michael@0 303 * {@inheritDoc}
michael@0 304 */
michael@0 305 public void validate() throws ValidationException {
michael@0 306 /*
michael@0 307 * ; the following is optional, ; but MUST NOT occur more than once attach /
michael@0 308 */
michael@0 309 PropertyValidator.getInstance().assertOneOrLess(Property.ATTACH, getProperties());
michael@0 310 }
michael@0 311 }
michael@0 312
michael@0 313 private class DisplayValidator implements Validator {
michael@0 314
michael@0 315 private static final long serialVersionUID = 1L;
michael@0 316
michael@0 317 /**
michael@0 318 * {@inheritDoc}
michael@0 319 */
michael@0 320 public void validate() throws ValidationException {
michael@0 321 /*
michael@0 322 * ; the following are all REQUIRED, ; but MUST NOT occur more than once action / description / trigger /
michael@0 323 */
michael@0 324 PropertyValidator.getInstance().assertOne(Property.DESCRIPTION, getProperties());
michael@0 325 }
michael@0 326 }
michael@0 327
michael@0 328 private class EmailValidator implements Validator {
michael@0 329
michael@0 330 private static final long serialVersionUID = 1L;
michael@0 331
michael@0 332 /**
michael@0 333 * {@inheritDoc}
michael@0 334 */
michael@0 335 public void validate() throws ValidationException {
michael@0 336 /*
michael@0 337 * ; the following are all REQUIRED,
michael@0 338 * ; but MUST NOT occur more than once action / description / trigger / summary
michael@0 339 * ; the following is REQUIRED,
michael@0 340 * ; and MAY occur more than once attendee /
michael@0 341 * ; 'duration' and 'repeat' are both optional,
michael@0 342 * ; and MUST NOT occur more than once each,
michael@0 343 * ; but if one occurs, so MUST the other duration / repeat /
michael@0 344 * ; the following are optional,
michael@0 345 * ; and MAY occur more than once attach / x-prop
michael@0 346 */
michael@0 347 PropertyValidator.getInstance().assertOne(Property.DESCRIPTION, getProperties());
michael@0 348 PropertyValidator.getInstance().assertOne(Property.SUMMARY, getProperties());
michael@0 349
michael@0 350 PropertyValidator.getInstance().assertOneOrMore(Property.ATTENDEE, getProperties());
michael@0 351 }
michael@0 352 }
michael@0 353
michael@0 354 private class ProcedureValidator implements Validator {
michael@0 355
michael@0 356 private static final long serialVersionUID = 1L;
michael@0 357
michael@0 358 /**
michael@0 359 * {@inheritDoc}
michael@0 360 */
michael@0 361 public void validate() throws ValidationException {
michael@0 362 /*
michael@0 363 * ; the following are all REQUIRED,
michael@0 364 * ; but MUST NOT occur more than once action / attach / trigger /
michael@0 365 * ; 'duration' and 'repeat' are both optional,
michael@0 366 * ; and MUST NOT occur more than once each,
michael@0 367 * ; but if one occurs, so MUST the other duration / repeat /
michael@0 368 * ; 'description' is optional,
michael@0 369 * ; and MUST NOT occur more than once description /
michael@0 370 * ; the following is optional, ; and MAY occur more than once x-prop
michael@0 371 */
michael@0 372 PropertyValidator.getInstance().assertOne(Property.ATTACH, getProperties());
michael@0 373
michael@0 374 PropertyValidator.getInstance().assertOneOrLess(Property.DESCRIPTION, getProperties());
michael@0 375 }
michael@0 376 }
michael@0 377
michael@0 378 /**
michael@0 379 * Common validation for all iTIP methods.
michael@0 380 *
michael@0 381 * <pre>
michael@0 382 * Component/Property Presence
michael@0 383 * ------------------- ----------------------------------------------
michael@0 384 * VALARM 0+
michael@0 385 * ACTION 1
michael@0 386 * ATTACH 0+
michael@0 387 * DESCRIPTION 0 or 1
michael@0 388 * DURATION 0 or 1 if present REPEAT MUST be present
michael@0 389 * REPEAT 0 or 1 if present DURATION MUST be present
michael@0 390 * SUMMARY 0 or 1
michael@0 391 * TRIGGER 1
michael@0 392 * X-PROPERTY 0+
michael@0 393 * </pre>
michael@0 394 */
michael@0 395 private class ITIPValidator implements Validator {
michael@0 396
michael@0 397 private static final long serialVersionUID = 1L;
michael@0 398
michael@0 399 /**
michael@0 400 * {@inheritDoc}
michael@0 401 */
michael@0 402 public void validate() throws ValidationException {
michael@0 403 PropertyValidator.getInstance().assertOne(Property.ACTION, getProperties());
michael@0 404 PropertyValidator.getInstance().assertOne(Property.TRIGGER, getProperties());
michael@0 405
michael@0 406 PropertyValidator.getInstance().assertOneOrLess(Property.DESCRIPTION, getProperties());
michael@0 407 PropertyValidator.getInstance().assertOneOrLess(Property.DURATION, getProperties());
michael@0 408 PropertyValidator.getInstance().assertOneOrLess(Property.REPEAT, getProperties());
michael@0 409 PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY, getProperties());
michael@0 410 }
michael@0 411 }
michael@0 412
michael@0 413 /**
michael@0 414 * Returns the mandatory action property.
michael@0 415 * @return the ACTION property or null if not specified
michael@0 416 */
michael@0 417 public final Action getAction() {
michael@0 418 return (Action) getProperty(Property.ACTION);
michael@0 419 }
michael@0 420
michael@0 421 /**
michael@0 422 * Returns the mandatory trigger property.
michael@0 423 * @return the TRIGGER property or null if not specified
michael@0 424 */
michael@0 425 public final Trigger getTrigger() {
michael@0 426 return (Trigger) getProperty(Property.TRIGGER);
michael@0 427 }
michael@0 428
michael@0 429 /**
michael@0 430 * Returns the optional duration property.
michael@0 431 * @return the DURATION property or null if not specified
michael@0 432 */
michael@0 433 public final Duration getDuration() {
michael@0 434 return (Duration) getProperty(Property.DURATION);
michael@0 435 }
michael@0 436
michael@0 437 /**
michael@0 438 * Returns the optional repeat property.
michael@0 439 * @return the REPEAT property or null if not specified
michael@0 440 */
michael@0 441 public final Repeat getRepeat() {
michael@0 442 return (Repeat) getProperty(Property.REPEAT);
michael@0 443 }
michael@0 444
michael@0 445 /**
michael@0 446 * Returns the optional attachment property.
michael@0 447 * @return the ATTACH property or null if not specified
michael@0 448 */
michael@0 449 public final Attach getAttachment() {
michael@0 450 return (Attach) getProperty(Property.ATTACH);
michael@0 451 }
michael@0 452
michael@0 453 /**
michael@0 454 * Returns the optional description property.
michael@0 455 * @return the DESCRIPTION property or null if not specified
michael@0 456 */
michael@0 457 public final Description getDescription() {
michael@0 458 return (Description) getProperty(Property.DESCRIPTION);
michael@0 459 }
michael@0 460
michael@0 461 /**
michael@0 462 * Returns the optional summary property.
michael@0 463 * @return the SUMMARY property or null if not specified
michael@0 464 */
michael@0 465 public final Summary getSummary() {
michael@0 466 return (Summary) getProperty(Property.SUMMARY);
michael@0 467 }
michael@0 468 }

mercurial