001    package biweekly.property;
002    
003    import java.util.Date;
004    import java.util.List;
005    
006    import biweekly.component.ICalComponent;
007    import biweekly.component.VTimezone;
008    import biweekly.util.ICalDateFormatter;
009    import biweekly.util.Period;
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 a list of dates or periods that help define a recurrence rule. This
039     * property must contain either dates or time periods. It cannot contain a
040     * combination of both.
041     * </p>
042     * <p>
043     * <b>Examples:</b>
044     * 
045     * <pre>
046     * //date-time values
047     * Date one = ...
048     * Date two = ...
049     * List&lt;Date&gt; dates = Arrays.asList(one, two);
050     * RecurrenceDates prop = new RecurrenceDates(dates, true);
051     * 
052     * //date values
053     * Date one = ...
054     * Date two = ...
055     * List&lt;Date&gt; dates = Arrays.asList(one, two);
056     * RecurrenceDates prop = new RecurrenceDates(dates, false);
057     * 
058     * //periods
059     * Period one = ...
060     * Period two = ...
061     * List&lt;Period&gt; periods = Arrays.asList(one, two);
062     * RecurrenceDates prop = new RecurrenceDates(periods, true);
063     * </pre>
064     * 
065     * </p>
066     * @author Michael Angstadt
067     * @see <a href="http://tools.ietf.org/html/rfc5545#page-120">RFC 5545
068     * p.120-2</a>
069     */
070    public class RecurrenceDates extends ICalProperty {
071            private List<Date> dates;
072            private boolean hasTime;
073            private List<Period> periods;
074    
075            /**
076             * Creates a recurrence dates property.
077             * @param dates the recurrence dates
078             * @param hasTime true if the dates have a time component, false if they are
079             * strictly dates
080             */
081            public RecurrenceDates(List<Date> dates, boolean hasTime) {
082                    this.dates = dates;
083                    this.hasTime = hasTime;
084            }
085    
086            /**
087             * Creates a recurrence dates property.
088             * @param periods the time periods
089             */
090            public RecurrenceDates(List<Period> periods) {
091                    this.periods = periods;
092            }
093    
094            /**
095             * Gets the recurrence dates.
096             * @return the dates or null if this property contains periods
097             */
098            public List<Date> getDates() {
099                    return dates;
100            }
101    
102            /**
103             * Gets whether the recurrence dates have time components.
104             * @return true if the dates have a time component, false if they are
105             * strictly dates
106             */
107            public boolean hasTime() {
108                    return hasTime;
109            }
110    
111            /**
112             * Gets the time periods.
113             * @return the time periods or null if this property contains dates
114             */
115            public List<Period> getPeriods() {
116                    return periods;
117            }
118    
119            @Override
120            public String getTimezoneId() {
121                    return super.getTimezoneId();
122            }
123    
124            @Override
125            public void setTimezoneId(String timezoneId) {
126                    super.setTimezoneId(timezoneId);
127            }
128    
129            @Override
130            public void setTimezone(VTimezone timezone) {
131                    super.setTimezone(timezone);
132            }
133    
134            @Override
135            protected void validate(List<ICalComponent> components, List<String> warnings) {
136                    String tzid = getTimezoneId();
137                    if (tzid != null && tzid.contains("/") && ICalDateFormatter.parseTimeZoneId(tzid) == null) {
138                            warnings.add("Unrecognized timezone ID: " + tzid);
139                    }
140            }
141    }