001package biweekly.property;
002
003import java.util.Date;
004import java.util.List;
005
006import biweekly.Warning;
007import biweekly.component.ICalComponent;
008import biweekly.parameter.Related;
009import 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 to trigger an alarm.
039 * </p>
040 * <p>
041 * <b>Code sample:</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 * @see <a href="http://tools.ietf.org/html/rfc5545#page-133">RFC 5545
053 * p.133-6</a>
054 */
055public class Trigger extends ICalProperty {
056        private Duration duration;
057        private Date date;
058
059        /**
060         * Creates a trigger property.
061         * @param duration the relative time
062         * @param related the date-time field that the duration is relative to
063         */
064        public Trigger(Duration duration, Related related) {
065                setDuration(duration, related);
066        }
067
068        /**
069         * Creates a trigger property.
070         * @param date the date-time the alarm will trigger.
071         */
072        public Trigger(Date date) {
073                setDate(date);
074        }
075
076        /**
077         * Gets the relative time at which the alarm will trigger.
078         * @return the relative time or null if an absolute time is set
079         */
080        public Duration getDuration() {
081                return duration;
082        }
083
084        /**
085         * Sets a relative time at which the alarm will trigger.
086         * @param duration the relative time
087         * @param related the date-time field that the duration is relative to
088         */
089        public void setDuration(Duration duration, Related related) {
090                this.date = null;
091                this.duration = duration;
092                setRelated(related);
093        }
094
095        /**
096         * Gets the date-time that the alarm will trigger.
097         * @return the date-time or null if a relative duration is set
098         */
099        public Date getDate() {
100                return date;
101        }
102
103        /**
104         * Sets the date-time that the alarm will trigger.
105         * @param date the date-time the alarm will trigger.
106         */
107        public void setDate(Date date) {
108                this.date = date;
109                this.duration = null;
110                setRelated(null);
111        }
112
113        /**
114         * Gets the date-time field that the duration is relative to.
115         * @return the field or null if not set
116         * @see <a href="http://tools.ietf.org/html/rfc5545#page-24">RFC 5545
117         * p.24</a>
118         */
119        public Related getRelated() {
120                return parameters.getRelated();
121        }
122
123        /**
124         * Sets the date-time field that the duration is relative to.
125         * @param related the field or null to remove
126         * @see <a href="http://tools.ietf.org/html/rfc5545#page-24">RFC 5545
127         * p.24</a>
128         */
129        public void setRelated(Related related) {
130                parameters.setRelated(related);
131        }
132
133        @Override
134        protected void validate(List<ICalComponent> components, List<Warning> warnings) {
135                if (duration == null && date == null) {
136                        warnings.add(Warning.validate(33));
137                }
138
139                Related related = getRelated();
140                if (duration != null && related == null) {
141                        warnings.add(Warning.validate(10));
142                }
143        }
144}