001package biweekly.property; 002 003import static biweekly.util.Google2445Utils.convert; 004 005import java.util.Date; 006import java.util.List; 007import java.util.NoSuchElementException; 008import java.util.TimeZone; 009 010import biweekly.ICalVersion; 011import biweekly.Warning; 012import biweekly.component.ICalComponent; 013import biweekly.util.ICalDate; 014import biweekly.util.Recurrence; 015import biweekly.util.Recurrence.Frequency; 016 017import com.google.ical.compat.javautil.DateIterator; 018import com.google.ical.compat.javautil.DateIteratorFactory; 019import com.google.ical.iter.RecurrenceIterator; 020import com.google.ical.iter.RecurrenceIteratorFactory; 021import com.google.ical.values.DateValue; 022import com.google.ical.values.RRule; 023 024/* 025 Copyright (c) 2013-2014, Michael Angstadt 026 All rights reserved. 027 028 Redistribution and use in source and binary forms, with or without 029 modification, are permitted provided that the following conditions are met: 030 031 1. Redistributions of source code must retain the above copyright notice, this 032 list of conditions and the following disclaimer. 033 2. Redistributions in binary form must reproduce the above copyright notice, 034 this list of conditions and the following disclaimer in the documentation 035 and/or other materials provided with the distribution. 036 037 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 038 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 039 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 040 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 041 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 042 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 043 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 044 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 045 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 046 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 047 */ 048 049/** 050 * Represents a property whose value is a recurrence rule. 051 * @author Michael Angstadt 052 */ 053public class RecurrenceProperty extends ValuedProperty<Recurrence> { 054 /** 055 * Creates a new recurrence property. 056 * @param recur the recurrence value 057 */ 058 public RecurrenceProperty(Recurrence recur) { 059 super(recur); 060 } 061 062 /** 063 * Gets the date values of this recurrence property. 064 * @param startDate the date that the recurrence starts (typically, the 065 * value of its accompanying {@link DateStart} property) 066 * @return an iterator containing the dates 067 * @see <a 068 * href="https://code.google.com/p/google-rfc-2445/">google-rfc-2445</a> 069 */ 070 public DateIterator getDateIterator(Date startDate) { 071 return getDateIterator(new ICalDate(startDate)); 072 } 073 074 /** 075 * Gets the date values of this recurrence property. 076 * @param startDate the date that the recurrence starts (typically, the 077 * value of its accompanying {@link DateStart} property) 078 * @return an iterator containing the dates 079 * @see <a 080 * href="https://code.google.com/p/google-rfc-2445/">google-rfc-2445</a> 081 */ 082 public DateIterator getDateIterator(ICalDate startDate) { 083 Recurrence recur = getValue(); 084 if (recur == null) { 085 return new EmptyDateIterator(); 086 } 087 088 RRule rruleValue = convert(recur); 089 DateValue dtstartValue = convert(startDate); 090 RecurrenceIterator it = RecurrenceIteratorFactory.createRecurrenceIterator(rruleValue, dtstartValue, TimeZone.getDefault()); 091 return DateIteratorFactory.createDateIterator(it); 092 } 093 094 @Override 095 protected void validate(List<ICalComponent> components, ICalVersion version, List<Warning> warnings) { 096 super.validate(components, version, warnings); 097 if (value == null) { 098 return; 099 } 100 101 if (value.getFrequency() == null) { 102 warnings.add(Warning.validate(30)); 103 } 104 105 if (value.getUntil() != null && value.getCount() != null) { 106 warnings.add(Warning.validate(31)); 107 } 108 109 switch (version) { 110 case V1_0: 111 if (!value.getXRules().isEmpty()) { 112 warnings.add(new Warning("X-Rules are not supported by vCal.")); 113 } 114 if (!value.getBySetPos().isEmpty()) { 115 warnings.add(new Warning("BYSETPOS is not supported by vCal.")); 116 } 117 if (value.getFrequency() == Frequency.SECONDLY) { 118 warnings.add(new Warning(Frequency.SECONDLY.name() + " frequency is not supported by vCal.")); 119 } 120 break; 121 122 case V2_0_DEPRECATED: 123 //empty 124 break; 125 126 case V2_0: 127 if (!value.getXRules().isEmpty()) { 128 warnings.add(Warning.validate(32)); 129 } 130 131 break; 132 } 133 } 134 135 private static class EmptyDateIterator implements DateIterator { 136 public boolean hasNext() { 137 return false; 138 } 139 140 public Date next() { 141 throw new NoSuchElementException(); 142 } 143 144 public void remove() { 145 throw new UnsupportedOperationException(); 146 } 147 148 public void advanceTo(Date newStartUtc) { 149 //empty 150 } 151 } 152}