View Javadoc

1   /*
2    * $Id: Trigger.java [Apr 6, 2004]
3    *
4    * Copyright (c) 2004, Ben Fortuna
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 	o Redistributions of source code must retain the above copyright
12   * notice, this list of conditions and the following disclaimer.
13   *
14   * 	o Redistributions in binary form must reproduce the above copyright
15   * notice, this list of conditions and the following disclaimer in the
16   * documentation and/or other materials provided with the distribution.
17   *
18   * 	o Neither the name of Ben Fortuna nor the names of any other contributors
19   * may be used to endorse or promote products derived from this software
20   * without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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              * ; the following are optional, ; but MUST NOT occur more than once
117              *
118              * (";" "VALUE" "=" "DURATION") / (";" trigrelparam) /
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              * ; the following is optional, ; and MAY occur more than once
135              *
136              * (";" xparam) ) ":" dur-value
137              */
138 
139         }
140         else {
141 
142             /*
143              * ; the following is REQUIRED, ; but MUST NOT occur more than once
144              *
145              * (";" "VALUE" "=" "DATE-TIME") /
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              * ; the following is optional, ; and MAY occur more than once
159              *
160              * (";" xparam) ) ":" date-time
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      * (non-Javadoc)
181      *
182      * @see net.fortuna.ical4j.model.Property#getValue()
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 }