Skip to content

Commit

Permalink
Use Interval to represent period between two instants
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jun 12, 2022
1 parent 39481fa commit 1a4349d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
9 changes: 9 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/PeriodList.java
Expand Up @@ -85,6 +85,11 @@ public PeriodList(Collection<Period<T>> periods, CalendarDateFormat dateFormat)
this.dateFormat = dateFormat;
}

public PeriodList(Interval[] intervals) {
this(Arrays.stream(intervals).map(i -> new Period<>((T) i.getStart(), (T) i.getEnd())).collect(Collectors.toList()),
CalendarDateFormat.UTC_DATE_TIME_FORMAT);
}

/**
* Parses the specified string representation to create a list of periods.
*
Expand Down Expand Up @@ -264,6 +269,10 @@ public PeriodList<T> addAll(Collection<Period<T>> arg0) {
return new PeriodList<>(copy, dateFormat);
}

public List<Interval> toIntervalList() {
return periods.stream().map(p -> p.toInterval()).collect(Collectors.toList());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/net/fortuna/ical4j/model/component/VFreeBusy.java
Expand Up @@ -41,6 +41,7 @@
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
import java.util.*;
import java.util.stream.Collectors;

import static net.fortuna.ical4j.model.Property.*;
import static net.fortuna.ical4j.model.property.immutable.ImmutableMethod.*;
Expand Down Expand Up @@ -351,7 +352,7 @@ public VFreeBusy(final VFreeBusy request, final List<CalendarComponent> componen
.components(components).build();
}

if (!fb.getPeriods().isEmpty()) {
if (!fb.getIntervals().isEmpty()) {
add(fb);
}
}
Expand Down Expand Up @@ -387,10 +388,10 @@ public BusyTimeBuilder components(List<CalendarComponent> components) {

public FreeBusy build() {
// periods must be in UTC time for freebusy..
final List<Period<Instant>> periods = getConsumedTime(components, new Period<>(start, end));
final List<Interval> periods = getConsumedTime(components, new Period<>(start, end));
periods.removeIf(period -> {
// check if period outside bounds..
return !period.intersects(new Period<>(start, end));
return !period.overlaps(Interval.of(start, end));
});
return new FreeBusy(periods);
}
Expand Down Expand Up @@ -433,24 +434,23 @@ public FreeTimeBuilder components(List<CalendarComponent> components) {
}

public FreeBusy build() {
final List<Period<Instant>> periods = getConsumedTime(components, new Period<>(start, end));
final List<Interval> periods = getConsumedTime(components, new Period<>(start, end));
final Interval interval = Interval.of(start, end);
// Add final consumed time to avoid special-case end-of-list processing
periods.add(new Period<>(end, end));
periods.add(Interval.of(end, end));
Instant lastPeriodEnd = start;

List<Period<Instant>> freePeriods = new ArrayList<>();
List<Interval> freePeriods = new ArrayList<>();
// where no time is consumed set the last period end as the range start..
for (final Period<Instant> period : periods) {
for (final Interval period : periods) {
// check if period outside bounds.. or period intersects with the end of the range..
if (interval.encloses(period.toInterval()) ||
(interval.overlaps(period.toInterval())
if (interval.encloses(period) || (interval.overlaps(period)
&& Instant.from(period.getStart()).isAfter(Instant.from(interval.getStart())))) {

// calculate duration between this period start and last period end..
final Duration freeDuration = new Duration(lastPeriodEnd, period.getStart());
if (new TemporalAmountComparator().compare(freeDuration.getDuration(), duration) >= 0) {
freePeriods.add(new Period<>(lastPeriodEnd, freeDuration.getDuration()));
freePeriods.add(Interval.of(lastPeriodEnd, (java.time.Duration) freeDuration.getDuration()));
}
}

Expand All @@ -469,14 +469,14 @@ public FreeBusy build() {
* @param components
* @return
*/
private static <T extends Temporal> List<Period<T>> getConsumedTime(final List<CalendarComponent> components,
private static <T extends Temporal> List<Interval> getConsumedTime(final List<CalendarComponent> components,
final Period<T> range) {

List<Period<T>> periods = new ArrayList<>();
// only events consume time..
components.stream().filter(c -> c.getName().equals(Component.VEVENT)).forEach(
c -> periods.addAll(((VEvent) c).getConsumedTime(range, false)));
return new ArrayList<>(new PeriodList(periods).normalise().getPeriods());
return new PeriodList<>(periods).normalise().getPeriods().stream().map(Period::toInterval).collect(Collectors.toList());
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/net/fortuna/ical4j/model/property/FreeBusy.java
Expand Up @@ -35,8 +35,8 @@
import net.fortuna.ical4j.validate.PropertyValidator;
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.ValidationResult;
import org.threeten.extra.Interval;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -109,14 +109,14 @@ public class FreeBusy extends Property {

private static final long serialVersionUID = -6415954847619338567L;

private PeriodList<Instant> periods;
private List<Interval> intervals;

/**
* Default constructor.
*/
public FreeBusy() {
super(FREEBUSY);
periods = new PeriodList<>(CalendarDateFormat.UTC_DATE_TIME_FORMAT);
intervals = new ArrayList<>();
}

/**
Expand All @@ -139,18 +139,18 @@ public FreeBusy(final ParameterList aList, final String aValue) {
/**
* @param pList a list of periods
*/
public FreeBusy(final List<Period<Instant>> pList) {
public FreeBusy(final List<Interval> pList) {
super(FREEBUSY);
periods = new PeriodList<>(pList, CalendarDateFormat.UTC_DATE_TIME_FORMAT);
intervals = new ArrayList<>(pList);
}

/**
* @param aList a list of parameters for this component
* @param pList a list of periods
*/
public FreeBusy(final ParameterList aList, final List<Period<Instant>> pList) {
public FreeBusy(final ParameterList aList, final List<Interval> pList) {
super(FREEBUSY, aList);
periods = new PeriodList<>(pList, CalendarDateFormat.UTC_DATE_TIME_FORMAT);
intervals = new ArrayList<>(pList);
}

/**
Expand All @@ -164,24 +164,24 @@ public ValidationResult validate() throws ValidationException {
/**
* @return Returns the periods.
*/
public final List<Period<Instant>> getPeriods() {
return new ArrayList<>(periods.getPeriods());
public final List<Interval> getIntervals() {
return new ArrayList<>(intervals);
}

/**
* {@inheritDoc}
*/
@Override
public final void setValue(final String aValue) {
periods = PeriodList.parse(aValue, CalendarDateFormat.UTC_DATE_TIME_FORMAT);
intervals = PeriodList.parse(aValue, CalendarDateFormat.UTC_DATE_TIME_FORMAT).toIntervalList();
}

/**
* {@inheritDoc}
*/
@Override
public final String getValue() {
return periods.toString();
return new PeriodList<>(intervals.toArray(new Interval[0])).toString();
}

@Override
Expand Down

0 comments on commit 1a4349d

Please sign in to comment.