
net.fortuna.ical4j.model.ComponentGroup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ical4j Show documentation
Show all versions of ical4j Show documentation
A Java library for reading and writing iCalendar (*.ics) files
package net.fortuna.ical4j.model;
import net.fortuna.ical4j.filter.Filter;
import net.fortuna.ical4j.filter.HasPropertyRule;
import net.fortuna.ical4j.model.property.RecurrenceId;
import net.fortuna.ical4j.model.property.Uid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Support for operations applicable to a group of components. Typically this class is used to manage
* component revisions (whereby each revision is a separate component), and the resulting ouput of
* such group functions.
*
* Example - Find latest revision of an event:
*
*
* Calendar calendar = ...
* String uidString = ...
*
* ComponentGroup group = new ComponentGroup(
* calendar.getComponents(Component.VEVENT),
* new Uid(uidString));
*
* return group.getLatestRevision();
*
*
* Created by fortuna on 20/07/2017.
*/
public class ComponentGroup {
private final ComponentList components;
private final Filter componentFilter;
public ComponentGroup(ComponentList components, Uid uid) {
this(components, uid, null);
}
public ComponentGroup(ComponentList components, Uid uid, RecurrenceId recurrenceId) {
this.components = components;
Predicate componentPredicate;
if (recurrenceId != null) {
componentPredicate = new HasPropertyRule(uid).and(new HasPropertyRule(recurrenceId));
} else {
componentPredicate = new HasPropertyRule(uid);
}
componentFilter = new Filter<>(componentPredicate);
}
/**
* Apply filter to all components to create a subset containing components
* matching the specified UID.
*
* @return
*/
public ComponentList getRevisions() {
return (ComponentList) componentFilter.filter(components);
}
/**
* Returns the latest component revision based on ascending sequence number and modified date.
*
* @return
*/
public T getLatestRevision() {
ComponentList revisions = getRevisions();
revisions.sort(new ComponentSequenceComparator());
Collections.reverse(revisions);
return revisions.iterator().next();
}
/**
* Calculate all recurring periods for the specified date range. This method will take all
* revisions into account when generating the set.
*
* @param period
* @return
*
* @see Component#calculateRecurrenceSet(Period)
*/
public PeriodList calculateRecurrenceSet(final Period period) {
PeriodList periods = new PeriodList();
List replacements = new ArrayList<>();
for (Component component : getRevisions()) {
if (!component.getProperties(Property.RECURRENCE_ID).isEmpty()) {
replacements.add(component);
} else {
periods = periods.add(component.calculateRecurrenceSet(period));
}
}
PeriodList finalPeriods = periods;
replacements.forEach(component -> {
RecurrenceId recurrenceId = component.getProperty(Property.RECURRENCE_ID);
List match = finalPeriods.stream().filter(p -> p.getStart().equals(recurrenceId.getDate()))
.collect(Collectors.toList());
finalPeriods.removeAll(match);
finalPeriods.addAll(component.calculateRecurrenceSet(period));
});
return periods;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy