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