Tue, 10 Feb 2015 18:12:00 +0100
Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.
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.property; |
michael@0 | 33 | |
michael@0 | 34 | import java.text.ParseException; |
michael@0 | 35 | |
michael@0 | 36 | import net.fortuna.ical4j.model.DateTime; |
michael@0 | 37 | import net.fortuna.ical4j.model.Dur; |
michael@0 | 38 | import net.fortuna.ical4j.model.Parameter; |
michael@0 | 39 | import net.fortuna.ical4j.model.ParameterList; |
michael@0 | 40 | import net.fortuna.ical4j.model.PropertyFactoryImpl; |
michael@0 | 41 | import net.fortuna.ical4j.model.ValidationException; |
michael@0 | 42 | import net.fortuna.ical4j.model.parameter.Value; |
michael@0 | 43 | import net.fortuna.ical4j.util.ParameterValidator; |
michael@0 | 44 | |
michael@0 | 45 | /** |
michael@0 | 46 | * $Id$ |
michael@0 | 47 | * |
michael@0 | 48 | * Created: [Apr 6, 2004] |
michael@0 | 49 | * |
michael@0 | 50 | * Defines a TRIGGER iCalendar component property. |
michael@0 | 51 | * |
michael@0 | 52 | * <pre> |
michael@0 | 53 | * 4.8.6.3 Trigger |
michael@0 | 54 | * |
michael@0 | 55 | * Property Name: TRIGGER |
michael@0 | 56 | * |
michael@0 | 57 | * Purpose: This property specifies when an alarm will trigger. |
michael@0 | 58 | * |
michael@0 | 59 | * Value Type: The default value type is DURATION. The value type can be |
michael@0 | 60 | * set to a DATE-TIME value type, in which case the value MUST specify a |
michael@0 | 61 | * UTC formatted DATE-TIME value. |
michael@0 | 62 | * |
michael@0 | 63 | * Property Parameters: Non-standard, value data type, time zone |
michael@0 | 64 | * identifier or trigger relationship property parameters can be |
michael@0 | 65 | * specified on this property. The trigger relationship property |
michael@0 | 66 | * parameter MUST only be specified when the value type is DURATION. |
michael@0 | 67 | * |
michael@0 | 68 | * Conformance: This property MUST be specified in the "VALARM" calendar |
michael@0 | 69 | * component. |
michael@0 | 70 | * |
michael@0 | 71 | * Description: Within the "VALARM" calendar component, this property |
michael@0 | 72 | * defines when the alarm will trigger. The default value type is |
michael@0 | 73 | * DURATION, specifying a relative time for the trigger of the alarm. |
michael@0 | 74 | * The default duration is relative to the start of an event or to-do |
michael@0 | 75 | * that the alarm is associated with. The duration can be explicitly set |
michael@0 | 76 | * |
michael@0 | 77 | * to trigger from either the end or the start of the associated event |
michael@0 | 78 | * or to-do with the "RELATED" parameter. A value of START will set the |
michael@0 | 79 | * alarm to trigger off the start of the associated event or to-do. A |
michael@0 | 80 | * value of END will set the alarm to trigger off the end of the |
michael@0 | 81 | * associated event or to-do. |
michael@0 | 82 | * |
michael@0 | 83 | * Either a positive or negative duration may be specified for the |
michael@0 | 84 | * "TRIGGER" property. An alarm with a positive duration is triggered |
michael@0 | 85 | * after the associated start or end of the event or to-do. An alarm |
michael@0 | 86 | * with a negative duration is triggered before the associated start or |
michael@0 | 87 | * end of the event or to-do. |
michael@0 | 88 | * |
michael@0 | 89 | * The "RELATED" property parameter is not valid if the value type of |
michael@0 | 90 | * the property is set to DATE-TIME (i.e., for an absolute date and time |
michael@0 | 91 | * alarm trigger). If a value type of DATE-TIME is specified, then the |
michael@0 | 92 | * property value MUST be specified in the UTC time format. If an |
michael@0 | 93 | * absolute trigger is specified on an alarm for a recurring event or |
michael@0 | 94 | * to-do, then the alarm will only trigger for the specified absolute |
michael@0 | 95 | * date/time, along with any specified repeating instances. |
michael@0 | 96 | * |
michael@0 | 97 | * If the trigger is set relative to START, then the "DTSTART" property |
michael@0 | 98 | * MUST be present in the associated "VEVENT" or "VTODO" calendar |
michael@0 | 99 | * component. If an alarm is specified for an event with the trigger set |
michael@0 | 100 | * relative to the END, then the "DTEND" property or the "DSTART" and |
michael@0 | 101 | * "DURATION' properties MUST be present in the associated "VEVENT" |
michael@0 | 102 | * calendar component. If the alarm is specified for a to-do with a |
michael@0 | 103 | * trigger set relative to the END, then either the "DUE" property or |
michael@0 | 104 | * the "DSTART" and "DURATION' properties MUST be present in the |
michael@0 | 105 | * associated "VTODO" calendar component. |
michael@0 | 106 | * |
michael@0 | 107 | * Alarms specified in an event or to-do which is defined in terms of a |
michael@0 | 108 | * DATE value type will be triggered relative to 00:00:00 UTC on the |
michael@0 | 109 | * specified date. For example, if "DTSTART:19980205, then the duration |
michael@0 | 110 | * trigger will be relative to19980205T000000Z. |
michael@0 | 111 | * |
michael@0 | 112 | * Format Definition: The property is defined by the following notation: |
michael@0 | 113 | * |
michael@0 | 114 | * trigger = "TRIGGER" (trigrel / trigabs) |
michael@0 | 115 | * |
michael@0 | 116 | * trigrel = *( |
michael@0 | 117 | * |
michael@0 | 118 | * ; the following are optional, |
michael@0 | 119 | * ; but MUST NOT occur more than once |
michael@0 | 120 | * |
michael@0 | 121 | * (";" "VALUE" "=" "DURATION") / |
michael@0 | 122 | * (";" trigrelparam) / |
michael@0 | 123 | * |
michael@0 | 124 | * ; the following is optional, |
michael@0 | 125 | * ; and MAY occur more than once |
michael@0 | 126 | * |
michael@0 | 127 | * (";" xparam) |
michael@0 | 128 | * ) ":" dur-value |
michael@0 | 129 | * |
michael@0 | 130 | * trigabs = 1*( |
michael@0 | 131 | * |
michael@0 | 132 | * ; the following is REQUIRED, |
michael@0 | 133 | * ; but MUST NOT occur more than once |
michael@0 | 134 | * |
michael@0 | 135 | * (";" "VALUE" "=" "DATE-TIME") / |
michael@0 | 136 | * |
michael@0 | 137 | * ; the following is optional, |
michael@0 | 138 | * ; and MAY occur more than once |
michael@0 | 139 | * |
michael@0 | 140 | * (";" xparam) |
michael@0 | 141 | * |
michael@0 | 142 | * ) ":" date-time |
michael@0 | 143 | * </pre> |
michael@0 | 144 | * |
michael@0 | 145 | * @author Ben Fortuna |
michael@0 | 146 | */ |
michael@0 | 147 | public class Trigger extends UtcProperty { |
michael@0 | 148 | |
michael@0 | 149 | private static final long serialVersionUID = 5049421499261722194L; |
michael@0 | 150 | |
michael@0 | 151 | private Dur duration; |
michael@0 | 152 | |
michael@0 | 153 | /** |
michael@0 | 154 | * Default constructor. |
michael@0 | 155 | */ |
michael@0 | 156 | public Trigger() { |
michael@0 | 157 | super(TRIGGER, PropertyFactoryImpl.getInstance()); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * @param aList a list of parameters for this component |
michael@0 | 162 | * @param aValue a value string for this component |
michael@0 | 163 | */ |
michael@0 | 164 | public Trigger(final ParameterList aList, final String aValue) { |
michael@0 | 165 | super(TRIGGER, aList, PropertyFactoryImpl.getInstance()); |
michael@0 | 166 | setValue(aValue); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | /** |
michael@0 | 170 | * @param duration a duration in milliseconds |
michael@0 | 171 | */ |
michael@0 | 172 | public Trigger(final Dur duration) { |
michael@0 | 173 | super(TRIGGER, PropertyFactoryImpl.getInstance()); |
michael@0 | 174 | setDuration(duration); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * @param aList a list of parameters for this component |
michael@0 | 179 | * @param duration a duration in milliseconds |
michael@0 | 180 | */ |
michael@0 | 181 | public Trigger(final ParameterList aList, final Dur duration) { |
michael@0 | 182 | super(TRIGGER, aList, PropertyFactoryImpl.getInstance()); |
michael@0 | 183 | setDuration(duration); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | /** |
michael@0 | 187 | * @param dateTime a date representation of a date-time |
michael@0 | 188 | */ |
michael@0 | 189 | public Trigger(final DateTime dateTime) { |
michael@0 | 190 | super(TRIGGER, PropertyFactoryImpl.getInstance()); |
michael@0 | 191 | setDateTime(dateTime); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | /** |
michael@0 | 195 | * @param aList a list of parameters for this component |
michael@0 | 196 | * @param dateTime a date representation of a date-time |
michael@0 | 197 | */ |
michael@0 | 198 | public Trigger(final ParameterList aList, final DateTime dateTime) { |
michael@0 | 199 | super(TRIGGER, aList, PropertyFactoryImpl.getInstance()); |
michael@0 | 200 | setDateTime(dateTime); |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | /** |
michael@0 | 204 | * {@inheritDoc} |
michael@0 | 205 | */ |
michael@0 | 206 | public final void validate() throws ValidationException { |
michael@0 | 207 | super.validate(); |
michael@0 | 208 | |
michael@0 | 209 | final Parameter relParam = getParameter(Parameter.RELATED); |
michael@0 | 210 | final Parameter valueParam = getParameter(Parameter.VALUE); |
michael@0 | 211 | |
michael@0 | 212 | if (relParam != null || !Value.DATE_TIME.equals(valueParam)) { |
michael@0 | 213 | |
michael@0 | 214 | ParameterValidator.getInstance().assertOneOrLess(Parameter.RELATED, |
michael@0 | 215 | getParameters()); |
michael@0 | 216 | |
michael@0 | 217 | ParameterValidator.getInstance().assertNullOrEqual(Value.DURATION, |
michael@0 | 218 | getParameters()); |
michael@0 | 219 | |
michael@0 | 220 | if (getDuration() == null) { |
michael@0 | 221 | throw new ValidationException("Duration value not specified"); |
michael@0 | 222 | } |
michael@0 | 223 | } |
michael@0 | 224 | else { |
michael@0 | 225 | ParameterValidator.getInstance().assertOne(Parameter.VALUE, |
michael@0 | 226 | getParameters()); |
michael@0 | 227 | |
michael@0 | 228 | ParameterValidator.getInstance().assertNullOrEqual(Value.DATE_TIME, |
michael@0 | 229 | getParameters()); |
michael@0 | 230 | |
michael@0 | 231 | if (getDateTime() == null) { |
michael@0 | 232 | throw new ValidationException("DATE-TIME value not specified"); |
michael@0 | 233 | } |
michael@0 | 234 | } |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | /** |
michael@0 | 238 | * @return Returns the duration. |
michael@0 | 239 | */ |
michael@0 | 240 | public final Dur getDuration() { |
michael@0 | 241 | return duration; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | /** |
michael@0 | 245 | * {@inheritDoc} |
michael@0 | 246 | */ |
michael@0 | 247 | public final void setValue(final String aValue) { |
michael@0 | 248 | try { |
michael@0 | 249 | super.setValue(aValue); |
michael@0 | 250 | duration = null; |
michael@0 | 251 | } |
michael@0 | 252 | catch (ParseException pe) { |
michael@0 | 253 | duration = new Dur(aValue); |
michael@0 | 254 | super.setDateTime(null); |
michael@0 | 255 | } |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | /** |
michael@0 | 259 | * {@inheritDoc} |
michael@0 | 260 | */ |
michael@0 | 261 | public final String getValue() { |
michael@0 | 262 | if (duration != null) { |
michael@0 | 263 | return duration.toString(); |
michael@0 | 264 | } |
michael@0 | 265 | return super.getValue(); |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | /** |
michael@0 | 269 | * @param dateTime The dateTime to set. |
michael@0 | 270 | */ |
michael@0 | 271 | public final void setDateTime(final DateTime dateTime) { |
michael@0 | 272 | super.setDateTime(dateTime); |
michael@0 | 273 | duration = null; |
michael@0 | 274 | getParameters().replace(Value.DATE_TIME); |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | /** |
michael@0 | 278 | * @param duration The duration to set. |
michael@0 | 279 | */ |
michael@0 | 280 | public final void setDuration(final Dur duration) { |
michael@0 | 281 | this.duration = duration; |
michael@0 | 282 | super.setDateTime(null); |
michael@0 | 283 | // duration is the default value type for Trigger.. |
michael@0 | 284 | if (getParameter(Parameter.VALUE) != null) { |
michael@0 | 285 | getParameters().replace(Value.DURATION); |
michael@0 | 286 | } |
michael@0 | 287 | } |
michael@0 | 288 | } |