Skip to content

Commit

Permalink
Applied changes manually from develop branch
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Dec 30, 2020
1 parent 6097e7b commit 4707bee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
29 changes: 15 additions & 14 deletions src/main/java/net/fortuna/ical4j/model/ComponentGroup.java
Expand Up @@ -75,7 +75,8 @@ public C getLatestRevision() {

/**
* Calculate all recurring periods for the specified date range. This method will take all
* revisions into account when generating the set.
* revisions into account when generating the set. Component revisions with a RECURRENCE_ID property are
* processed last, as they override instances in the default recurrence set.
*
* @param period
* @return
Expand All @@ -85,31 +86,31 @@ public C getLatestRevision() {
public <T extends Temporal> List<Period<T>> calculateRecurrenceSet(final Period<T> period) {
// Use set to exclude duplicates..
Set<Period<T>> periods = new HashSet<>();
List<Component> replacements = new ArrayList<>();
List<Component> overrides = new ArrayList<>();

for (Component component : getRevisions()) {
if (component.getProperties().getFirst(Property.RECURRENCE_ID).isPresent()) {
replacements.add(component);
overrides.add(component);
} else {
periods.addAll(component.calculateRecurrenceSet(period));
}
}

List<Period<T>> finalPeriods = new ArrayList<>(periods);
replacements.forEach(component -> calculateReplacements(period, component, finalPeriods));
overrides.forEach(component -> {
try {
RecurrenceId<?> recurrenceId = component.getProperties().getRequired(Property.RECURRENCE_ID);
finalPeriods.removeIf(p -> p.getStart().equals(recurrenceId.getDate()));
component.calculateRecurrenceSet(period).stream()
.filter(p -> p.getStart().equals(recurrenceId.getDate()))
.forEach(finalPeriods::add);
} catch (ConstraintViolationException cve) {
// Should never reach as we previously determined existence of RECURRENCE_ID property..
}
});

// Natural sort of final list..
Collections.sort(finalPeriods);
return finalPeriods;
}

private <T extends Temporal> void calculateReplacements(Period<T> period, Component component, List<Period<T>> finalPeriods) {
Optional<RecurrenceId<?>> recurrenceId = component.getProperties().getFirst(Property.RECURRENCE_ID);
if (recurrenceId.isPresent()) {
List<Period<T>> match = finalPeriods.stream().filter(p -> p.getStart().equals(recurrenceId.get().getDate()))
.collect(Collectors.toList());
finalPeriods.removeAll(match);
}
finalPeriods.addAll(component.calculateRecurrenceSet(period));
}
}
Expand Up @@ -100,6 +100,6 @@ class ComponentGroupTest extends Specification {
def recurrences = new ComponentGroup(components.all, uid).calculateRecurrenceSet(period)

then: 'the instance override is removed from the recurrence set'
recurrences.size() == event.calculateRecurrenceSet(period).normalise().size() - 1
recurrences.size() == event.calculateRecurrenceSet(period).size() - 1
}
}

0 comments on commit 4707bee

Please sign in to comment.