001package biweekly.property;
002
003import java.util.Date;
004import java.util.List;
005
006import biweekly.Warning;
007import biweekly.component.ICalComponent;
008import biweekly.component.VTimezone;
009import biweekly.util.ICalDateFormat;
010import biweekly.util.Period;
011
012/*
013 Copyright (c) 2013, Michael Angstadt
014 All rights reserved.
015
016 Redistribution and use in source and binary forms, with or without
017 modification, are permitted provided that the following conditions are met: 
018
019 1. Redistributions of source code must retain the above copyright notice, this
020 list of conditions and the following disclaimer. 
021 2. Redistributions in binary form must reproduce the above copyright notice,
022 this list of conditions and the following disclaimer in the documentation
023 and/or other materials provided with the distribution. 
024
025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
028 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
029 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
032 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
034 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
035 */
036
037/**
038 * <p>
039 * Defines a list of dates or time periods that help define a recurrence rule.
040 * It must contain either dates or time periods. It cannot contain a combination
041 * of both.
042 * </p>
043 * <p>
044 * <b>Code sample:</b>
045 * 
046 * <pre class="brush:java">
047 * VEvent event = new VEvent();
048 * 
049 * //date-time values
050 * Date datetime1 = ...
051 * Date datetime2 = ...
052 * List&lt;Date&gt; datetimes = Arrays.asList(datetime1, datetime2);
053 * RecurrenceDates rdate = new RecurrenceDates(datetimes, true);
054 * event.addRecurrenceDates(rdate);
055 * 
056 * //date values
057 * Date date1 = ...
058 * Date date2 = ...
059 * List&lt;Date&gt; dates = Arrays.asList(date1, date2);
060 * rdate = new RecurrenceDates(dates, false);
061 * event.addRecurrenceDates(rdate);
062 * 
063 * //periods
064 * Period period1 = ...
065 * Period period2 = ...
066 * List&lt;Period&gt; periods = Arrays.asList(period1, period2);
067 * rdate = new RecurrenceDates(periods, true);
068 * event.addRecurrenceDates(rdate);
069 * </pre>
070 * 
071 * </p>
072 * @author Michael Angstadt
073 * @see <a href="http://tools.ietf.org/html/rfc5545#page-120">RFC 5545 p.120-2</a>
074 */
075public class RecurrenceDates extends ICalProperty {
076        private List<Date> dates;
077        private boolean hasTime;
078        private List<Period> periods;
079
080        /**
081         * Creates a recurrence dates property.
082         * @param dates the recurrence dates
083         * @param hasTime true if the dates have a time component, false if they are
084         * strictly dates
085         */
086        public RecurrenceDates(List<Date> dates, boolean hasTime) {
087                this.dates = dates;
088                this.hasTime = hasTime;
089        }
090
091        /**
092         * Creates a recurrence dates property.
093         * @param periods the time periods
094         */
095        public RecurrenceDates(List<Period> periods) {
096                this.periods = periods;
097        }
098
099        /**
100         * Gets the recurrence dates.
101         * @return the dates or null if this property contains periods
102         */
103        public List<Date> getDates() {
104                return dates;
105        }
106
107        /**
108         * Gets whether the recurrence dates have time components.
109         * @return true if the dates have a time component, false if they are
110         * strictly dates
111         */
112        public boolean hasTime() {
113                return hasTime;
114        }
115
116        /**
117         * Gets the time periods.
118         * @return the time periods or null if this property contains dates
119         */
120        public List<Period> getPeriods() {
121                return periods;
122        }
123
124        @Override
125        public String getTimezoneId() {
126                return super.getTimezoneId();
127        }
128
129        @Override
130        public void setTimezoneId(String timezoneId) {
131                super.setTimezoneId(timezoneId);
132        }
133
134        @Override
135        public void setTimezone(VTimezone timezone) {
136                super.setTimezone(timezone);
137        }
138
139        @Override
140        protected void validate(List<ICalComponent> components, List<Warning> warnings) {
141                String tzid = getTimezoneId();
142                if (tzid != null && tzid.contains("/") && ICalDateFormat.parseTimeZoneId(tzid) == null) {
143                        warnings.add(Warning.validate(27, tzid));
144                }
145        }
146}