Skip to content

Commit ce96443

Browse files
committed
Remove use of Sets.intersection
1 parent 11822c5 commit ce96443

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

main/src/main/java/org/mobilitydata/gtfsvalidator/reportsummary/model/ServiceWindow.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package org.mobilitydata.gtfsvalidator.reportsummary.model;
22

3-
import static java.util.Collections.emptySet;
43
import static java.util.stream.Collectors.*;
54

6-
import com.google.common.collect.Sets;
75
import java.time.LocalDate;
86
import java.util.*;
97
import org.mobilitydata.gtfsvalidator.table.GtfsCalendar;
108
import org.mobilitydata.gtfsvalidator.table.GtfsCalendarDate;
119
import org.mobilitydata.gtfsvalidator.table.GtfsCalendarDateExceptionType;
1210
import org.mobilitydata.gtfsvalidator.table.GtfsTrip;
11+
import org.mobilitydata.gtfsvalidator.util.FuncUtil;
1312

1413
record ServiceWindow(LocalDate startDate, LocalDate endDate) {
1514
static Optional<ServiceWindow> fromCalendars(
@@ -58,21 +57,19 @@ static Optional<ServiceWindow> fromCalendarsAndCalendarDates(
5857

5958
Set<String> serviceIds = new HashSet<>(trips.stream().map(GtfsTrip::serviceId).toList());
6059

61-
// We compute the set of days that are removed across all services in
62-
// order to shift the start and end dates.
63-
Set<LocalDate> removedDays =
60+
Map<String, Set<LocalDate>> removedDaysByServiceId =
6461
calendarDates.stream()
6562
.filter(
6663
d ->
67-
serviceIds.contains(d.serviceId())
68-
&& d.exceptionType() == GtfsCalendarDateExceptionType.SERVICE_REMOVED)
64+
d.exceptionType() == GtfsCalendarDateExceptionType.SERVICE_REMOVED
65+
&& serviceIds.contains(d.serviceId()))
6966
.collect(
7067
groupingBy(
71-
GtfsCalendarDate::serviceId, mapping(d -> d.date().getLocalDate(), toSet())))
72-
.values()
73-
.stream()
74-
.reduce(Sets::intersection)
75-
.orElse(emptySet());
68+
GtfsCalendarDate::serviceId, mapping(d -> d.date().getLocalDate(), toSet())));
69+
70+
// We compute the set of days that are removed across all services in
71+
// order to shift the start and end dates.
72+
Set<LocalDate> removedDays = FuncUtil.intersectAll(removedDaysByServiceId.values());
7673

7774
LocalDate startDate = serviceWindowFromCalendars.get().startDate();
7875
LocalDate endDate = serviceWindowFromCalendars.get().endDate();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.mobilitydata.gtfsvalidator.util;
2+
3+
import static java.util.Collections.emptySet;
4+
5+
import java.util.Collection;
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
public class FuncUtil {
10+
public static <T> Set<T> intersectAll(Collection<Set<T>> sets) {
11+
if (sets.isEmpty()) {
12+
return emptySet();
13+
}
14+
15+
Set<T> intersection = null;
16+
for (Set<T> set : sets) {
17+
if (intersection == null) {
18+
intersection = new HashSet<>(set);
19+
} else {
20+
intersection.retainAll(set);
21+
}
22+
}
23+
return intersection;
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.mobilitydata.gtfsvalidator.util;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
import static java.util.Collections.emptyList;
5+
import static java.util.Collections.emptySet;
6+
7+
import java.util.List;
8+
import java.util.Set;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.junit.runners.JUnit4;
12+
13+
@RunWith(JUnit4.class)
14+
public class FuncUtilTest {
15+
@Test
16+
public void testIntersectAll() {
17+
assertThat(
18+
FuncUtil.intersectAll(
19+
List.of(
20+
Set.of(1, 2, 3, 4, 5, 6),
21+
Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
22+
Set.of(2, 4, 6, 8, 10))))
23+
.isEqualTo(Set.of(2, 4, 6));
24+
25+
assertThat(FuncUtil.intersectAll(emptyList())).isEqualTo(emptySet());
26+
}
27+
}

0 commit comments

Comments
 (0)