Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jul 17, 2020
1 parent 694d8ee commit 11ceae4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
24 changes: 10 additions & 14 deletions src/main/java/net/fortuna/ical4j/model/Component.java
Expand Up @@ -378,20 +378,16 @@ public final <T extends Temporal> Set<Period<T>> calculateRecurrenceSet(final Pe

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

List<Period<T>> calculated = rDates.stream().filter(p -> p.getParameters().getFirst(Parameter.VALUE).get() == Value.DATE_TIME)
.map(p -> ((DateListProperty<T>) p).getDates())
.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.getParameters().getFirst(Parameter.VALUE).get() == Value.DATE)
.map(p -> ((DateListProperty<T>) p).getDates())
.flatMap(List<T>::stream).filter(period::includes)
.map(rdateDate -> new Period<>(rdateDate, rDuration)).collect(Collectors.toList()));
for (Property p : rDates) {
Optional<Value> value = p.getParameters().getFirst(Parameter.VALUE);
if (value.equals(Optional.of(Value.PERIOD))) {
recurrenceSet.addAll(((RDate<T>) p).getPeriods().orElse(Collections.emptySet()).stream()
.filter(period::intersects).collect(Collectors.toList()));
} else {
recurrenceSet.addAll(((DateListProperty<T>) p).getDates().stream()
.filter(period::includes).map(d -> new Period<>(d, rDuration)).collect(Collectors.toList()));
}
}

// allow for recurrence rules that start prior to the specified period
// but still intersect with it..
Expand Down
Expand Up @@ -39,7 +39,6 @@

import java.time.ZoneId;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -110,7 +109,7 @@ public DateListProperty(final String name, final ParameterList parameters, final
* @return Returns the dates.
*/
public final List<T> getDates() {
return new ArrayList<>(dates.getDates());
return dates.getDates();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/fortuna/ical4j/model/property/RDate.java
Expand Up @@ -38,9 +38,9 @@
import net.fortuna.ical4j.validate.ValidationException;

import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
* $Id$
Expand Down Expand Up @@ -224,9 +224,9 @@ public final void validate() throws ValidationException {
/**
* @return Returns the period list.
*/
public final Optional<List<Period<T>>> getPeriods() {
public final Optional<Set<Period<T>>> getPeriods() {
if (periods != null) {
return Optional.of(new ArrayList<>(periods.getPeriods()));
return Optional.of(periods.getPeriods());
} else {
return Optional.empty();
}
Expand Down
23 changes: 9 additions & 14 deletions src/test/groovy/net/fortuna/ical4j/model/property/RDateSpec.groovy
Expand Up @@ -32,7 +32,6 @@
package net.fortuna.ical4j.model.property


import net.fortuna.ical4j.model.Period
import net.fortuna.ical4j.model.TemporalAdapter
import spock.lang.Specification

Expand All @@ -44,26 +43,22 @@ class RDateSpec extends Specification {
setup: 'create new date-time'
Temporal date = TemporalAdapter.parse('20110319T140400').temporal

and: 'add date-time to rdate'
when: 'add date-time to rdate'
RDate rdate = new RDate()
rdate.dates.add(date)

expect: 'rdate list contains date-time'
rdate.dates == []
}

def 'should throw exception when trying to add period value to default rdate instance'() {
setup: 'create new period'
Period period = Period.parse('20110319T140400/20110319T180400')

when: 'add period to rdate'
RDate rdate = new RDate([period])
rdate.periods.get().add(period)

then: 'exception is thrown'
thrown(UnsupportedOperationException)
}

def 'should not be able to add periods to default rdate instance'() {
setup: 'create new rdate instance'
RDate rdate = new RDate()

expect: 'periods not available'
!rdate.periods.isPresent()
}

def 'should throw exception when trying to add date value to period rdate instance'() {
setup: 'create new date-time'
Temporal date = TemporalAdapter.parse('20110319T140400').temporal
Expand Down

0 comments on commit 11ceae4

Please sign in to comment.