src/net/fortuna/ical4j/model/property/Trigger.java

changeset 0
fb9019fb1bf7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/net/fortuna/ical4j/model/property/Trigger.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,288 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012, Ben Fortuna
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + *
    1.12 + *  o Redistributions of source code must retain the above copyright
    1.13 + * notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *  o Redistributions in binary form must reproduce the above copyright
    1.16 + * notice, this list of conditions and the following disclaimer in the
    1.17 + * documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + *  o Neither the name of Ben Fortuna nor the names of any other contributors
    1.20 + * may be used to endorse or promote products derived from this software
    1.21 + * without specific prior written permission.
    1.22 + *
    1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.34 + */
    1.35 +package net.fortuna.ical4j.model.property;
    1.36 +
    1.37 +import java.text.ParseException;
    1.38 +
    1.39 +import net.fortuna.ical4j.model.DateTime;
    1.40 +import net.fortuna.ical4j.model.Dur;
    1.41 +import net.fortuna.ical4j.model.Parameter;
    1.42 +import net.fortuna.ical4j.model.ParameterList;
    1.43 +import net.fortuna.ical4j.model.PropertyFactoryImpl;
    1.44 +import net.fortuna.ical4j.model.ValidationException;
    1.45 +import net.fortuna.ical4j.model.parameter.Value;
    1.46 +import net.fortuna.ical4j.util.ParameterValidator;
    1.47 +
    1.48 +/**
    1.49 + * $Id$
    1.50 + * 
    1.51 + * Created: [Apr 6, 2004]
    1.52 + *
    1.53 + * Defines a TRIGGER iCalendar component property.
    1.54 + * 
    1.55 + * <pre>
    1.56 + *     4.8.6.3 Trigger
    1.57 + *     
    1.58 + *        Property Name: TRIGGER
    1.59 + *     
    1.60 + *        Purpose: This property specifies when an alarm will trigger.
    1.61 + *     
    1.62 + *        Value Type: The default value type is DURATION. The value type can be
    1.63 + *        set to a DATE-TIME value type, in which case the value MUST specify a
    1.64 + *        UTC formatted DATE-TIME value.
    1.65 + *     
    1.66 + *        Property Parameters: Non-standard, value data type, time zone
    1.67 + *        identifier or trigger relationship property parameters can be
    1.68 + *        specified on this property. The trigger relationship property
    1.69 + *        parameter MUST only be specified when the value type is DURATION.
    1.70 + *     
    1.71 + *        Conformance: This property MUST be specified in the &quot;VALARM&quot; calendar
    1.72 + *        component.
    1.73 + *     
    1.74 + *        Description: Within the &quot;VALARM&quot; calendar component, this property
    1.75 + *        defines when the alarm will trigger. The default value type is
    1.76 + *        DURATION, specifying a relative time for the trigger of the alarm.
    1.77 + *        The default duration is relative to the start of an event or to-do
    1.78 + *        that the alarm is associated with. The duration can be explicitly set
    1.79 + *     
    1.80 + *        to trigger from either the end or the start of the associated event
    1.81 + *        or to-do with the &quot;RELATED&quot; parameter. A value of START will set the
    1.82 + *        alarm to trigger off the start of the associated event or to-do. A
    1.83 + *        value of END will set the alarm to trigger off the end of the
    1.84 + *        associated event or to-do.
    1.85 + *     
    1.86 + *        Either a positive or negative duration may be specified for the
    1.87 + *        &quot;TRIGGER&quot; property. An alarm with a positive duration is triggered
    1.88 + *        after the associated start or end of the event or to-do. An alarm
    1.89 + *        with a negative duration is triggered before the associated start or
    1.90 + *        end of the event or to-do.
    1.91 + *     
    1.92 + *        The &quot;RELATED&quot; property parameter is not valid if the value type of
    1.93 + *        the property is set to DATE-TIME (i.e., for an absolute date and time
    1.94 + *        alarm trigger). If a value type of DATE-TIME is specified, then the
    1.95 + *        property value MUST be specified in the UTC time format. If an
    1.96 + *        absolute trigger is specified on an alarm for a recurring event or
    1.97 + *        to-do, then the alarm will only trigger for the specified absolute
    1.98 + *        date/time, along with any specified repeating instances.
    1.99 + *     
   1.100 + *        If the trigger is set relative to START, then the &quot;DTSTART&quot; property
   1.101 + *        MUST be present in the associated &quot;VEVENT&quot; or &quot;VTODO&quot; calendar
   1.102 + *        component. If an alarm is specified for an event with the trigger set
   1.103 + *        relative to the END, then the &quot;DTEND&quot; property or the &quot;DSTART&quot; and
   1.104 + *        &quot;DURATION' properties MUST be present in the associated &quot;VEVENT&quot;
   1.105 + *        calendar component. If the alarm is specified for a to-do with a
   1.106 + *        trigger set relative to the END, then either the &quot;DUE&quot; property or
   1.107 + *        the &quot;DSTART&quot; and &quot;DURATION' properties MUST be present in the
   1.108 + *        associated &quot;VTODO&quot; calendar component.
   1.109 + *     
   1.110 + *        Alarms specified in an event or to-do which is defined in terms of a
   1.111 + *        DATE value type will be triggered relative to 00:00:00 UTC on the
   1.112 + *        specified date. For example, if &quot;DTSTART:19980205, then the duration
   1.113 + *        trigger will be relative to19980205T000000Z.
   1.114 + *     
   1.115 + *        Format Definition: The property is defined by the following notation:
   1.116 + *     
   1.117 + *          trigger    = &quot;TRIGGER&quot; (trigrel / trigabs)
   1.118 + *     
   1.119 + *          trigrel    = *(
   1.120 + *     
   1.121 + *                     ; the following are optional,
   1.122 + *                     ; but MUST NOT occur more than once
   1.123 + *     
   1.124 + *                       (&quot;;&quot; &quot;VALUE&quot; &quot;=&quot; &quot;DURATION&quot;) /
   1.125 + *                       (&quot;;&quot; trigrelparam) /
   1.126 + *     
   1.127 + *                     ; the following is optional,
   1.128 + *                     ; and MAY occur more than once
   1.129 + *     
   1.130 + *                       (&quot;;&quot; xparam)
   1.131 + *                       ) &quot;:&quot;  dur-value
   1.132 + *     
   1.133 + *          trigabs    = 1*(
   1.134 + *     
   1.135 + *                     ; the following is REQUIRED,
   1.136 + *                     ; but MUST NOT occur more than once
   1.137 + *     
   1.138 + *                       (&quot;;&quot; &quot;VALUE&quot; &quot;=&quot; &quot;DATE-TIME&quot;) /
   1.139 + *     
   1.140 + *                     ; the following is optional,
   1.141 + *                     ; and MAY occur more than once
   1.142 + *     
   1.143 + *                       (&quot;;&quot; xparam)
   1.144 + *     
   1.145 + *                       ) &quot;:&quot; date-time
   1.146 + * </pre>
   1.147 + * 
   1.148 + * @author Ben Fortuna
   1.149 + */
   1.150 +public class Trigger extends UtcProperty {
   1.151 +
   1.152 +    private static final long serialVersionUID = 5049421499261722194L;
   1.153 +
   1.154 +    private Dur duration;
   1.155 +
   1.156 +    /**
   1.157 +     * Default constructor.
   1.158 +     */
   1.159 +    public Trigger() {
   1.160 +        super(TRIGGER, PropertyFactoryImpl.getInstance());
   1.161 +    }
   1.162 +
   1.163 +    /**
   1.164 +     * @param aList a list of parameters for this component
   1.165 +     * @param aValue a value string for this component
   1.166 +     */
   1.167 +    public Trigger(final ParameterList aList, final String aValue) {
   1.168 +        super(TRIGGER, aList, PropertyFactoryImpl.getInstance());
   1.169 +        setValue(aValue);
   1.170 +    }
   1.171 +
   1.172 +    /**
   1.173 +     * @param duration a duration in milliseconds
   1.174 +     */
   1.175 +    public Trigger(final Dur duration) {
   1.176 +        super(TRIGGER, PropertyFactoryImpl.getInstance());
   1.177 +        setDuration(duration);
   1.178 +    }
   1.179 +
   1.180 +    /**
   1.181 +     * @param aList a list of parameters for this component
   1.182 +     * @param duration a duration in milliseconds
   1.183 +     */
   1.184 +    public Trigger(final ParameterList aList, final Dur duration) {
   1.185 +        super(TRIGGER, aList, PropertyFactoryImpl.getInstance());
   1.186 +        setDuration(duration);
   1.187 +    }
   1.188 +
   1.189 +    /**
   1.190 +     * @param dateTime a date representation of a date-time
   1.191 +     */
   1.192 +    public Trigger(final DateTime dateTime) {
   1.193 +        super(TRIGGER, PropertyFactoryImpl.getInstance());
   1.194 +        setDateTime(dateTime);
   1.195 +    }
   1.196 +
   1.197 +    /**
   1.198 +     * @param aList a list of parameters for this component
   1.199 +     * @param dateTime a date representation of a date-time
   1.200 +     */
   1.201 +    public Trigger(final ParameterList aList, final DateTime dateTime) {
   1.202 +        super(TRIGGER, aList, PropertyFactoryImpl.getInstance());
   1.203 +        setDateTime(dateTime);
   1.204 +    }
   1.205 +
   1.206 +    /**
   1.207 +     * {@inheritDoc}
   1.208 +     */
   1.209 +    public final void validate() throws ValidationException {
   1.210 +        super.validate();
   1.211 +
   1.212 +        final Parameter relParam = getParameter(Parameter.RELATED);
   1.213 +        final Parameter valueParam = getParameter(Parameter.VALUE);
   1.214 +
   1.215 +        if (relParam != null || !Value.DATE_TIME.equals(valueParam)) {
   1.216 +
   1.217 +            ParameterValidator.getInstance().assertOneOrLess(Parameter.RELATED,
   1.218 +                    getParameters());
   1.219 +            
   1.220 +            ParameterValidator.getInstance().assertNullOrEqual(Value.DURATION,
   1.221 +                    getParameters());
   1.222 +
   1.223 +            if (getDuration() == null) {
   1.224 +                throw new ValidationException("Duration value not specified");
   1.225 +            }
   1.226 +        }
   1.227 +        else {
   1.228 +            ParameterValidator.getInstance().assertOne(Parameter.VALUE,
   1.229 +                    getParameters());
   1.230 +            
   1.231 +            ParameterValidator.getInstance().assertNullOrEqual(Value.DATE_TIME,
   1.232 +                    getParameters());
   1.233 +
   1.234 +            if (getDateTime() == null) {
   1.235 +                throw new ValidationException("DATE-TIME value not specified");
   1.236 +            }
   1.237 +        }
   1.238 +    }
   1.239 +
   1.240 +    /**
   1.241 +     * @return Returns the duration.
   1.242 +     */
   1.243 +    public final Dur getDuration() {
   1.244 +        return duration;
   1.245 +    }
   1.246 +
   1.247 +    /**
   1.248 +     * {@inheritDoc}
   1.249 +     */
   1.250 +    public final void setValue(final String aValue) {
   1.251 +        try {
   1.252 +            super.setValue(aValue);
   1.253 +            duration = null;
   1.254 +        }
   1.255 +        catch (ParseException pe) {
   1.256 +            duration = new Dur(aValue);
   1.257 +            super.setDateTime(null);
   1.258 +        }
   1.259 +    }
   1.260 +
   1.261 +    /**
   1.262 +     * {@inheritDoc}
   1.263 +     */
   1.264 +    public final String getValue() {
   1.265 +        if (duration != null) {
   1.266 +            return duration.toString();
   1.267 +        }
   1.268 +        return super.getValue();
   1.269 +    }
   1.270 +
   1.271 +    /**
   1.272 +     * @param dateTime The dateTime to set.
   1.273 +     */
   1.274 +    public final void setDateTime(final DateTime dateTime) {
   1.275 +        super.setDateTime(dateTime);
   1.276 +        duration = null;
   1.277 +        getParameters().replace(Value.DATE_TIME);
   1.278 +    }
   1.279 +
   1.280 +    /**
   1.281 +     * @param duration The duration to set.
   1.282 +     */
   1.283 +    public final void setDuration(final Dur duration) {
   1.284 +        this.duration = duration;
   1.285 +        super.setDateTime(null);
   1.286 +        // duration is the default value type for Trigger..
   1.287 +        if (getParameter(Parameter.VALUE) != null) {
   1.288 +            getParameters().replace(Value.DURATION);
   1.289 +        }
   1.290 +    }
   1.291 +}

mercurial