/
ComponentGroup.java
86 lines (71 loc) · 2.58 KB
/
ComponentGroup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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.Collections;
import java.util.function.Predicate;
/**
* 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 functins.
*
* Created by fortuna on 20/07/2017.
*/
public class ComponentGroup<T extends Component> {
private final ComponentList<T> components;
private final Uid uid;
private final RecurrenceId recurrenceId;
private final Filter<T> componentFilter;
public ComponentGroup(ComponentList<T> components, Uid uid) {
this(components, uid, null);
}
public ComponentGroup(ComponentList<T> components, Uid uid, RecurrenceId recurrenceId) {
this.components = components;
this.uid = uid;
this.recurrenceId = recurrenceId;
Predicate<T> componentPredicate;
if (recurrenceId != null) {
componentPredicate = new HasPropertyRule<T>(uid).and(new HasPropertyRule<T>(recurrenceId));
} else {
componentPredicate = new HasPropertyRule<T>(uid);
}
componentFilter = new Filter<>(componentPredicate);
}
/**
* Apply filter to all components to create a subset containing components
* matching the specified UID.
*
* @return
*/
public ComponentList<T> getRevisions() {
return (ComponentList<T>) componentFilter.filter(components);
}
/**
* Returns the latest component revision based on ascending sequence number and modified date.
*
* @return
*/
public T getLatestRevision() {
ComponentList<T> 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();
for (Component component : getRevisions()) {
periods = periods.add(component.calculateRecurrenceSet(period));
}
return periods;
}
}