Skip to content

Commit

Permalink
Simplified code
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Feb 27, 2020
1 parent 9e9609e commit 582ac85
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
10 changes: 1 addition & 9 deletions src/main/java/net/fortuna/ical4j/model/Component.java
Expand Up @@ -38,11 +38,9 @@
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.time.ZoneId;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
import java.util.*;
Expand Down Expand Up @@ -283,12 +281,6 @@ public int hashCode() {
* @return a list of periods representing component occurrences within the specified boundary
*/
public final <T extends Temporal> List<Period<T>> calculateRecurrenceSet(final Period<T> period) {
return calculateRecurrenceSet(period, ZoneId.systemDefault());
}

public final <T extends Temporal> List<Period<T>> calculateRecurrenceSet(final Period<T> period, ZoneId zoneId) {

// validate();

final Set<Period<T>> recurrenceSet = new TreeSet<>();

Expand Down Expand Up @@ -345,7 +337,7 @@ public final <T extends Temporal> List<Period<T>> calculateRecurrenceSet(final P
.map(rruleDate -> new Period<T>(rruleDate, rDuration)).collect(Collectors.toList()));
} else {
// add initial instance if intersection with the specified period..
Period startPeriod;
Period<T> startPeriod;
if (end != null) {
startPeriod = new Period<>(start.getDate(), end.getDate());
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/fortuna/ical4j/model/component/VFreeBusy.java
Expand Up @@ -390,7 +390,7 @@ public BusyTimeBuilder components(ComponentList<CalendarComponent> components) {

public FreeBusy build() {
// periods must be in UTC time for freebusy..
final List<Period<Instant>> periods = getConsumedTime(components, start, end);
final List<Period<Instant>> periods = getConsumedTime(components, new Period<>(start, end));
periods.removeIf(period -> {
// check if period outside bounds..
return !period.intersects(new Period<>(start, end));
Expand Down Expand Up @@ -438,7 +438,7 @@ public FreeTimeBuilder components(ComponentList<CalendarComponent> components) {
public FreeBusy build() {
final FreeBusy fb = new FreeBusy();
fb.getParameters().add(FbType.FREE);
final List<Period<Instant>> periods = getConsumedTime(components, start, end);
final List<Period<Instant>> 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));
Expand Down Expand Up @@ -470,13 +470,13 @@ public FreeBusy build() {
* @param components
* @return
*/
private <T extends Temporal> List<Period<T>> getConsumedTime(final ComponentList<CalendarComponent> components, final T rangeStart,
final T rangeEnd) {
private <T extends Temporal> List<Period<T>> getConsumedTime(final ComponentList<CalendarComponent> components,
final Period<T> range) {

final PeriodList<T> periods = new PeriodList<>();
// only events consume time..
for (final Component event : components.getComponents(Component.VEVENT)) {
periods.addAll(((VEvent) event).getConsumedTime(rangeStart, rangeEnd, false));
periods.addAll(((VEvent) event).getConsumedTime(range, false));
}
return new ArrayList<>(periods.normalise().getPeriods());
}
Expand Down
23 changes: 12 additions & 11 deletions src/test/java/net/fortuna/ical4j/model/component/VEventTest.java
Expand Up @@ -86,7 +86,7 @@ public VEventTest(String testMethod) {

/**
* @param testMethod
* @param component
* @param event
*/
public VEventTest(String testMethod, VEvent event) {
super(testMethod, event);
Expand Down Expand Up @@ -137,7 +137,7 @@ protected void tearDown() throws Exception {
}

/**
* @param filename
* @param resourceString
* @return
*/
private net.fortuna.ical4j.model.Calendar loadCalendar(String resourceString)
Expand Down Expand Up @@ -225,7 +225,7 @@ public final void testGetConsumedTime() throws Exception {

// Test Null Dates
try {
event.getConsumedTime(null, null);
event.getConsumedTime(null);
fail("Should've thrown an exception.");
} catch (RuntimeException re) {
log.info("Expecting an exception here.");
Expand All @@ -248,7 +248,7 @@ public final void testGetConsumedTime() throws Exception {
// This range is monday to friday every three weeks, starting from
// March 7th 2005, which means for our query dates we need
// April 18th through to the 22nd.
List<Period<ZonedDateTime>> weeklyPeriods = event.getConsumedTime(queryStart, queryEnd);
List<Period<ZonedDateTime>> weeklyPeriods = event.getConsumedTime(new Period<>(queryStart, queryEnd));
// PeriodList dailyPeriods = dailyWeekdayEvents.getConsumedTime(queryStart, queryEnd);
// week1EndDate.getTime());
// dailyPeriods.addAll(dailyWeekdayEvents.getConsumedTime(week4Start, queryEnd));
Expand Down Expand Up @@ -296,7 +296,7 @@ public final void testGetConsumedTimeDaily() throws Exception {
// PeriodList weeklyPeriods =
// event.getConsumedTime(new DateTime(queryStartDate.getTime()),
// new DateTime(queryEndDate.getTime()));
List<Period<ZonedDateTime>> dailyPeriods = event.getConsumedTime(queryStartDate, queryEndDate);
List<Period<ZonedDateTime>> dailyPeriods = event.getConsumedTime(new Period<>(queryStartDate, queryEndDate));

ZonedDateTime expectedStartOfFirstRange = ZonedDateTime.of(2005, 4, 4,
9, 0, 0, 0, ZoneId.systemDefault());
Expand Down Expand Up @@ -339,7 +339,7 @@ public final void testGetConsumedTimeMonthly() throws Exception {
// This range is Monday to Friday every month (which has a multiplying
// effect), starting from March 7th 2005. Our query dates are
// April 3rd through to the 17th.
List<Period<ZonedDateTime>> monthlyPeriods = event.getConsumedTime(queryStartDate, queryEndDate);
List<Period<ZonedDateTime>> monthlyPeriods = event.getConsumedTime(new Period<>(queryStartDate, queryEndDate));
// PeriodList dailyPeriods =
// dailyWeekdayEvents.getConsumedTime(new DateTime(queryStartDate.getTime()),
// new DateTime(queryEndDate.getTime()));
Expand Down Expand Up @@ -368,7 +368,8 @@ public final void testGetConsumedTime2() throws Exception {
//Date end = new Date(start.getTime() + (1000 * 60 * 60 * 24 * 7 * 4));
calendar.getComponents().forEach(calendarComponent -> {
if (calendarComponent instanceof VEvent) {
List<Period<LocalDate>> consumed = ((VEvent) calendarComponent).getConsumedTime(start, end);
List<Period<LocalDate>> consumed = ((VEvent) calendarComponent).getConsumedTime(
new Period<>(start, end));

log.debug("Event [" + calendarComponent + "]");
log.debug("Consumed time [" + consumed + "]");
Expand All @@ -386,7 +387,7 @@ public final void testGetConsumedTime3() throws Exception {
LocalDate start = (LocalDate) vev.getStartDate().getDate();
LocalDate latest = LocalDate.now().plusYears(1);

List<Period<LocalDate>> pl = vev.getConsumedTime(start, latest);
List<Period<LocalDate>> pl = vev.getConsumedTime(new Period<>(start, latest));
assertTrue(!pl.isEmpty());
}

Expand All @@ -411,7 +412,7 @@ public void testGetConsumedTimeByCount() {
ZonedDateTime rangeStart = ZonedDateTime.now();
ZonedDateTime rangeEnd = rangeStart.plusWeeks(4);

log.info(event.getConsumedTime(rangeStart, rangeEnd).toString());
log.info(event.getConsumedTime(new Period<Temporal>(rangeStart, rangeEnd)).toString());
}

/**
Expand Down Expand Up @@ -462,7 +463,7 @@ public void testGetConsumedTimeWithExDate() throws ParseException {

TemporalAdapter start = TemporalAdapter.parse("20050106");
TemporalAdapter end = TemporalAdapter.parse("20050107");
List<Period<Temporal>> list = event1.getConsumedTime(start.getTemporal(), end.getTemporal());
List<Period<Temporal>> list = event1.getConsumedTime(new Period<>(start.getTemporal(), end.getTemporal()));
assertTrue(list.isEmpty());
}

Expand All @@ -480,7 +481,7 @@ public void testGetConsumedTimeWithExDate2() throws IOException, ParserException
LocalDate start = LocalDate.now().withYear(1997).withMonth(8).withDayOfMonth(2);
LocalDate end = start.withDayOfMonth(4);

List<Period<LocalDate>> periods = event.getConsumedTime(start, end);
List<Period<LocalDate>> periods = event.getConsumedTime(new Period<>(start, end));
assertTrue(periods.isEmpty());
}

Expand Down

0 comments on commit 582ac85

Please sign in to comment.