Skip to content

Commit

Permalink
Reinstate period operations
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jul 29, 2019
1 parent 36fc86b commit 21a9767
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
14 changes: 5 additions & 9 deletions src/main/java/net/fortuna/ical4j/model/Component.java
Expand Up @@ -37,13 +37,11 @@
import net.fortuna.ical4j.validate.ValidationException;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.threeten.extra.Interval;

import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
Expand Down Expand Up @@ -325,28 +323,26 @@ public final <T extends Temporal> List<Period<T>> calculateRecurrenceSet(final P
if (end == null && duration == null) {
rDuration = java.time.Duration.ZERO;
} else if (duration == null) {
rDuration = java.time.Duration.between(start.getDate(), end.getDate());
rDuration = TemporalAmountAdapter.between(start.getDate(), end.getDate()).getDuration();
} else {
rDuration = duration.getDuration();
}

Interval interval = period.toInterval(zoneId);

// add recurrence dates..
List<RDate<T>> rDates = getProperties(Property.RDATE);
recurrenceSet.addAll(rDates.stream().filter(p -> p.getParameter(Parameter.VALUE) == Value.PERIOD)
.map(RDate::getPeriods).flatMap(List<Period<T>>::stream).filter(p -> p.toInterval(zoneId).overlaps(interval))
.map(RDate::getPeriods).flatMap(List<Period<T>>::stream).filter(period::intersects)
.collect(Collectors.toList()));

List<Period<T>> calculated = rDates.stream().filter(p -> p.getParameter(Parameter.VALUE) == Value.DATE_TIME)
.map(DateListProperty::getDates).map(DateList::getDates)
.flatMap(List<T>::stream).filter(d -> interval.contains(Instant.from(d)))
.flatMap(List<T>::stream).filter(period::includes)
.map(rdateTime -> new Period<>(rdateTime, rDuration)).collect(Collectors.toList());
recurrenceSet.addAll(calculated);

recurrenceSet.addAll(rDates.stream().filter(p -> p.getParameter(Parameter.VALUE) == Value.DATE)
.map(DateListProperty::getDates).map(DateList::getDates)
.flatMap(List<T>::stream).filter(d -> interval.contains(Instant.from(d)))
.flatMap(List<T>::stream).filter(period::includes)
.map(rdateDate -> new Period<>(rdateDate, rDuration)).collect(Collectors.toList()));

// allow for recurrence rules that start prior to the specified period
Expand Down Expand Up @@ -375,7 +371,7 @@ public final <T extends Temporal> List<Period<T>> calculateRecurrenceSet(final P

startPeriod = new Period<>(start.getDate(), duration.getDuration());
}
if (!period.toInterval(zoneId).intersection(startPeriod.toInterval(zoneId)).isEmpty()) {
if (period.intersects(startPeriod)) {
recurrenceSet.add(startPeriod);
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/net/fortuna/ical4j/model/Period.java
Expand Up @@ -170,6 +170,10 @@ public Period(final T start, final TemporalAmount duration) {
this(start, new TemporalAmountAdapter(duration));
}

public Period(final T start, final TemporalAmount duration, CalendarDateFormat dateFormat) {
this(start, new TemporalAmountAdapter(duration), dateFormat);
}

/**
* Constructs a new period with the specified start date and duration.
*
Expand Down Expand Up @@ -295,7 +299,9 @@ public final boolean includes(final Date date, final boolean inclusive) {
*
*/
public final boolean includes(final Temporal date) {
return toInterval().contains(Instant.from(date));
// return toInterval().contains(Instant.from(date));
Objects.requireNonNull(date, "date");
return DATE_RANGE_COMPARATOR.compare(start, date) >= 0 && DATE_RANGE_COMPARATOR.compare(date, end) <= 0;
}

/**
Expand Down Expand Up @@ -435,6 +441,18 @@ String toString(CalendarDateFormat dateFormat, ZoneId zoneId) {
}
return b.toString();
}

/**
* Decides whether this period intersects with another one.
*
* @param other a possible intersecting period
* @return true if the specified period intersects this one, false otherwise.
*/
public boolean intersects(Period other) {
Objects.requireNonNull(other, "other");
return other.equals(this) || (DATE_RANGE_COMPARATOR.compare(start, other.end) < 0
&& DATE_RANGE_COMPARATOR.compare(other.start, end) < 0);
}

public Interval toInterval() {
return toInterval(ZoneId.systemDefault());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/fortuna/ical4j/model/PeriodList.java
Expand Up @@ -158,7 +158,7 @@ public final PeriodList<T> normalise() {
// ignore periods contained by other periods..
period = prevPeriod;
normalised = true;
} else if (!prevInterval.intersection(periodInterval).isEmpty()) {
} else if (prevPeriod.intersects(period)) {
// combine intersecting periods..
period = prevPeriod.add(period);
normalised = true;
Expand Down Expand Up @@ -186,7 +186,7 @@ public final PeriodList<T> normalise() {
}
else {
return this;
}
}
}

/**
Expand Down

0 comments on commit 21a9767

Please sign in to comment.