001    package biweekly.property;
002    
003    import java.util.Date;
004    import java.util.List;
005    
006    import biweekly.component.ICalComponent;
007    import biweekly.parameter.Related;
008    import biweekly.util.Duration;
009    
010    /*
011     Copyright (c) 2013, Michael Angstadt
012     All rights reserved.
013    
014     Redistribution and use in source and binary forms, with or without
015     modification, are permitted provided that the following conditions are met: 
016    
017     1. Redistributions of source code must retain the above copyright notice, this
018     list of conditions and the following disclaimer. 
019     2. Redistributions in binary form must reproduce the above copyright notice,
020     this list of conditions and the following disclaimer in the documentation
021     and/or other materials provided with the distribution. 
022    
023     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
024     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
025     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
026     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
027     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
030     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033     */
034    
035    /**
036     * <p>
037     * Defines when an alarm will be triggered.
038     * </p>
039     * <p>
040     * <b>Examples:</b>
041     * 
042     * <pre class="brush:java">
043     * //15 minutes before the start time
044     * Duration duration = Duration.builder().prior(true).minutes(15).build();
045     * Trigger trigger = new Trigger(duration, Related.START);
046     * VAlarm alarm = VAlarm.display(trigger, &quot;Meeting in 15 minutes&quot;);
047     * </pre>
048     * 
049     * </p>
050     * @author Michael Angstadt
051     * @rfc 5545 p.133-6
052     */
053    public class Trigger extends ICalProperty {
054            private Duration duration;
055            private Date date;
056    
057            /**
058             * Creates a trigger property.
059             * @param duration the relative time
060             * @param related the date-time field that the duration is relative to
061             */
062            public Trigger(Duration duration, Related related) {
063                    setDuration(duration, related);
064            }
065    
066            /**
067             * Creates a trigger property.
068             * @param date the date-time the alarm will trigger.
069             */
070            public Trigger(Date date) {
071                    setDate(date);
072            }
073    
074            /**
075             * Gets the relative time at which the alarm will trigger.
076             * @return the relative time or null if an absolute time is set
077             */
078            public Duration getDuration() {
079                    return duration;
080            }
081    
082            /**
083             * Sets a relative time at which the alarm will trigger.
084             * @param duration the relative time
085             * @param related the date-time field that the duration is relative to
086             */
087            public void setDuration(Duration duration, Related related) {
088                    this.date = null;
089                    this.duration = duration;
090                    setRelated(related);
091            }
092    
093            /**
094             * Gets the date-time that the alarm will trigger.
095             * @return the date-time or null if a relative duration is set
096             */
097            public Date getDate() {
098                    return date;
099            }
100    
101            /**
102             * Sets the date-time that the alarm will trigger.
103             * @param date the date-time the alarm will trigger.
104             */
105            public void setDate(Date date) {
106                    this.date = date;
107                    this.duration = null;
108                    setRelated(null);
109            }
110    
111            /**
112             * Gets the date-time field that the duration is relative to.
113             * @return the field or null if not set
114             * @rfc 5545 p.24
115             */
116            public Related getRelated() {
117                    return parameters.getRelated();
118            }
119    
120            /**
121             * Sets the date-time field that the duration is relative to.
122             * @param related the field or null to remove
123             * @rfc 5545 p.24
124             */
125            public void setRelated(Related related) {
126                    parameters.setRelated(related);
127            }
128    
129            @Override
130            protected void validate(List<ICalComponent> components, List<String> warnings) {
131                    if (duration == null && date == null) {
132                            warnings.add("No duration or date defined.");
133                    }
134            }
135    }