Skip to content

Commit

Permalink
Added skip behaviour for BYMONTH rules
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jul 6, 2021
1 parent 19493b4 commit 73c8bad
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/fortuna/ical4j/model/Recur.java
Expand Up @@ -397,7 +397,7 @@ private void initTransformers() {
}
if (monthList != null) {
transformers.put(BYMONTH, new ByMonthRule(monthList, frequency,
Optional.ofNullable(weekStartDay)));
Optional.ofNullable(weekStartDay), skip));
} else {
monthList = new MonthList(chronology.range(ChronoField.MONTH_OF_YEAR));
}
Expand Down
Expand Up @@ -19,13 +19,24 @@ public class ByMonthRule extends AbstractDateExpansionRule {

private final MonthList monthList;

private final Recur.Skip skip;

public ByMonthRule(MonthList monthList, Frequency frequency) {
this(monthList, frequency, Optional.empty());
this(monthList, frequency, Recur.Skip.OMIT);
}

public ByMonthRule(MonthList monthList, Frequency frequency, Recur.Skip skip) {
this(monthList, frequency, Optional.empty(), skip);
}

public ByMonthRule(MonthList monthList, Frequency frequency, Optional<WeekDay.Day> weekStartDay) {
this(monthList, frequency, weekStartDay, Recur.Skip.OMIT);
}

public ByMonthRule(MonthList monthList, Frequency frequency, Optional<WeekDay.Day> weekStartDay, Recur.Skip skip) {
super(frequency, weekStartDay);
this.monthList = monthList;
this.skip = skip;
}

@Override
Expand Down Expand Up @@ -73,12 +84,22 @@ public List<Date> apply(Date date) {
List<Date> retVal = new ArrayList<>();
final Calendar cal = getCalendarInstance(date, true);
// construct a list of possible months..
monthList.forEach(month -> {
// Java months are zero-based..
for (Month month : monthList) {
if (month.isLeapMonth()) {
if (skip == Recur.Skip.BACKWARD) {
cal.roll(Calendar.MONTH, (month.getMonthOfYear() - 1) - cal.get(Calendar.MONTH));
} else if (skip == Recur.Skip.FORWARD) {
cal.roll(Calendar.MONTH, month.getMonthOfYear() - cal.get(Calendar.MONTH));
} else {
continue;
}
} else {
// Java months are zero-based..
// cal.set(Calendar.MONTH, month - 1);
cal.roll(Calendar.MONTH, (month.getMonthOfYear() - 1) - cal.get(Calendar.MONTH));
cal.roll(Calendar.MONTH, (month.getMonthOfYear() - 1) - cal.get(Calendar.MONTH));
}
retVal.add(Dates.getInstance(getTime(date, cal), type));
});
}
return retVal;
}
}
Expand Down
@@ -1,9 +1,6 @@
package net.fortuna.ical4j.transform.recurrence

import net.fortuna.ical4j.model.Date
import net.fortuna.ical4j.model.DateList
import net.fortuna.ical4j.model.DateTime
import net.fortuna.ical4j.model.MonthList
import net.fortuna.ical4j.model.*
import net.fortuna.ical4j.model.parameter.Value
import spock.lang.Specification

Expand All @@ -29,5 +26,38 @@ class ByMonthRuleTest extends Specification {
'2' | YEARLY | [new DateTime("20200229T000000")] | Value.DATE_TIME | [new DateTime("20200229T000000")]
'10,12' | YEARLY | [new Date("20181010")] | Value.DATE | [new Date("20181010"), new Date("20181210")]
'10,12' | DAILY | [new Date("20181010"), new Date("20181110"), new Date("20181210")] | Value.DATE | [new Date("20181010"), new Date("20181210")]
'5L' | YEARLY | [new Date('20150103')] | Value.DATE | []
}

def 'verify transformations by month with skip backward'() {
given: 'a bymonth rule'
ByMonthRule rule = [new MonthList(byMonthPart), frequency, Recur.Skip.BACKWARD]

and: 'a list of dates'
DateList dateList = [valueType]
dateList.addAll(dates)

expect: 'the rule transforms the dates correctly'
rule.transform(dateList) == expectedResult

where:
byMonthPart | frequency | dates | valueType | expectedResult
'5L' | YEARLY | [new Date('20150103')] | Value.DATE | [new Date('20150503')]
}

def 'verify transformations by month with skip forward'() {
given: 'a bymonth rule'
ByMonthRule rule = [new MonthList(byMonthPart), frequency, Recur.Skip.FORWARD]

and: 'a list of dates'
DateList dateList = [valueType]
dateList.addAll(dates)

expect: 'the rule transforms the dates correctly'
rule.transform(dateList) == expectedResult

where:
byMonthPart | frequency | dates | valueType | expectedResult
'5L' | YEARLY | [new Date('20150103')] | Value.DATE | [new Date('20150603')]
}
}

0 comments on commit 73c8bad

Please sign in to comment.