-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
ByWeekNoRule.java
74 lines (65 loc) · 2.52 KB
/
ByWeekNoRule.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
package net.fortuna.ical4j.transform.recurrence;
import net.fortuna.ical4j.model.NumberList;
import net.fortuna.ical4j.model.Recur;
import net.fortuna.ical4j.model.Recur.Frequency;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.time.temporal.IsoFields;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.List;
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
/**
* Applies BYWEEKNO rules specified in this Recur instance to the specified date list. If no BYWEEKNO rules are
* specified the date list is returned unmodified.
*/
public class ByWeekNoRule<T extends Temporal> extends AbstractDateExpansionRule<T> {
private transient Logger log = LoggerFactory.getLogger(ByWeekNoRule.class);
private final NumberList weekNoList;
public ByWeekNoRule(NumberList weekNoList, Frequency frequency) {
super(frequency);
this.weekNoList = weekNoList;
}
@Override
public List<T> transform(List<T> dates) {
if (weekNoList.isEmpty()) {
return dates;
}
final List<T> weekNoDates = new ArrayList<>();
for (final T date : dates) {
final int numWeeksInYear = (int) IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date).getMaximum();
for (final Integer weekNo : weekNoList) {
if (weekNo == 0 || weekNo < -numWeeksInYear || weekNo > numWeeksInYear) {
if (log.isTraceEnabled()) {
log.trace("Invalid week of year: " + weekNo);
}
continue;
}
T candidate;
if (weekNo > 0) {
if (numWeeksInYear < weekNo) {
continue;
}
candidate = withTemporalField(date, ALIGNED_WEEK_OF_YEAR, weekNo);
} else {
if (numWeeksInYear < -weekNo) {
continue;
}
candidate = withTemporalField(date, ALIGNED_WEEK_OF_YEAR, numWeeksInYear + 1 + weekNo);
}
weekNoDates.add(candidate);
}
}
return weekNoDates;
}
/**
* @param stream
* @throws IOException
* @throws ClassNotFoundException
*/
private void readObject(final java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
log = LoggerFactory.getLogger(Recur.class);
}
}