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