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> 043 * //15 minutes before the start time 044 * Duration duration = new Duration.Builder().prior(true).minutes(15).build(); 045 * Trigger trigger = new Trigger(duration, Related.START); 046 * </pre> 047 * 048 * </p> 049 * @author Michael Angstadt 050 * @see <a href="http://tools.ietf.org/html/rfc5545#page-133">RFC 5545 051 * p.133-6</a> 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 * @see <a href="http://tools.ietf.org/html/rfc5545#page-24">RFC 5545 115 * p.24</a> 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 * @see <a href="http://tools.ietf.org/html/rfc5545#page-24">RFC 5545 125 * p.24</a> 126 */ 127 public void setRelated(Related related) { 128 parameters.setRelated(related); 129 } 130 131 @Override 132 protected void validate(List<ICalComponent> components, List<String> warnings) { 133 if (duration == null && date == null) { 134 warnings.add("No duration or date defined."); 135 } 136 } 137 }