Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for generating occurrences for a given period for recur…
…ring components
- Loading branch information
1 parent
f26d856
commit b4cfdec
Showing
5 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/net/fortuna/ical4j/model/RecurrenceSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) 2023, Ben Fortuna | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* o Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* o Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* o Neither the name of Ben Fortuna nor the names of any other contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
package net.fortuna.ical4j.model; | ||
|
||
import net.fortuna.ical4j.model.component.CalendarComponent; | ||
import net.fortuna.ical4j.model.property.RecurrenceId; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.text.ParseException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public interface RecurrenceSupport<T extends CalendarComponent> extends PropertyContainer { | ||
|
||
<C extends Component> C copy() throws ParseException, IOException, URISyntaxException; | ||
|
||
/** | ||
* Calculates the recurrence set for this component using the specified period. | ||
* The recurrence set is derived from a combination of the component start date, | ||
* recurrence rules and dates, and exception rules and dates. Note that component | ||
* transparency and anniversary-style dates do not affect the resulting | ||
* intersection. | ||
* <p>If an explicit DURATION is not specified, the effective duration of each | ||
* returned period is derived from the DTSTART and DTEND or DUE properties. | ||
* If the component has no DURATION, DTEND or DUE, the effective duration is set | ||
* to PT0S</p> | ||
* | ||
* @param period a range to calculate recurrences for | ||
* @return a list of periods | ||
*/ | ||
PeriodList calculateRecurrenceSet(final Period period); | ||
|
||
default List<T> getOccurrences(Period period) throws ParseException, IOException, URISyntaxException { | ||
List<T> occurrences = new ArrayList<>(); | ||
|
||
PeriodList periods = RecurrenceSupport.this.calculateRecurrenceSet(period); | ||
for (Period p : periods) { | ||
final T occurrence = this.copy(); | ||
occurrence.getProperties().add(new RecurrenceId(p.getStart())); | ||
occurrences.add(occurrence); | ||
} | ||
|
||
return occurrences; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/test/groovy/net/fortuna/ical4j/model/RecurrenceSupportTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2023, Ben Fortuna | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* o Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* o Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* o Neither the name of Ben Fortuna nor the names of any other contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
package net.fortuna.ical4j.model | ||
|
||
import net.fortuna.ical4j.model.component.VEvent | ||
import spock.lang.Specification | ||
|
||
class RecurrenceSupportTest extends Specification { | ||
|
||
def 'test get occurrences for recurring event'() { | ||
given: 'a recurring event' | ||
VEvent event = new ContentBuilder().vevent { | ||
summary('a recurring event') | ||
dtstart('20230101T010000') | ||
dtend('20230101T020000') | ||
rrule('FREQ=WEEKLY;INTERVAL=2;BYDAY=SU') | ||
} | ||
|
||
expect: 'calculated occurrences match expected' | ||
event.getOccurrences(new Period(period)).collect { it.getProperty('RECURRENCE-ID').value} == expectedOccurrences | ||
|
||
where: | ||
period | expectedOccurrences | ||
'20230101T010000/P3W' | ['20230101T010000', '20230115T010000'] | ||
} | ||
} |