Skip to content

Commit

Permalink
Added support for generating occurrences for a given period for recur…
Browse files Browse the repository at this point in the history
…ring components
  • Loading branch information
benfortuna committed Mar 10, 2023
1 parent f26d856 commit b4cfdec
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 3 deletions.
77 changes: 77 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/RecurrenceSupport.java
@@ -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;
}
}
4 changes: 3 additions & 1 deletion src/main/java/net/fortuna/ical4j/model/component/VEvent.java
Expand Up @@ -190,7 +190,7 @@
*
* @author Ben Fortuna
*/
public class VEvent extends CalendarComponent implements ComponentContainer<Component> {
public class VEvent extends CalendarComponent implements ComponentContainer<Component>, RecurrenceSupport<VEvent> {

private static final long serialVersionUID = 2547948989200697335L;

Expand Down Expand Up @@ -451,7 +451,9 @@ public final PeriodList getConsumedTime(final Date rangeStart,
* @throws IOException where an error occurs reading data
* @throws URISyntaxException where an invalid URI is encountered
* @throws ParseException where an error occurs parsing data
* @deprecated use {@link RecurrenceSupport#getOccurrences(Period)}
*/
@Deprecated
public final VEvent getOccurrence(final Date date) throws IOException,
URISyntaxException, ParseException {

Expand Down
Expand Up @@ -100,7 +100,7 @@
*
* @author Ben Fortuna
*/
public class VJournal extends CalendarComponent implements ComponentContainer<Component> {
public class VJournal extends CalendarComponent implements ComponentContainer<Component>, RecurrenceSupport<VJournal> {

private static final long serialVersionUID = -7635140949183238830L;

Expand Down
Expand Up @@ -112,7 +112,7 @@
*
* @author Ben Fortuna
*/
public class VToDo extends CalendarComponent implements ComponentContainer<Component> {
public class VToDo extends CalendarComponent implements ComponentContainer<Component>, RecurrenceSupport<VToDo> {

private static final long serialVersionUID = -269658210065896668L;

Expand Down
@@ -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']
}
}

0 comments on commit b4cfdec

Please sign in to comment.