2
2
3
3
import com .conveyal .gtfs .model .Calendar ;
4
4
import com .conveyal .gtfs .model .CalendarDate ;
5
+ import com .google .common .collect .HashMultimap ;
6
+ import com .google .common .collect .Multimap ;
7
+ import com .google .common .collect .Sets ;
5
8
import org .apache .commons .dbutils .DbUtils ;
6
9
import org .slf4j .Logger ;
7
10
import org .slf4j .LoggerFactory ;
13
16
import java .sql .SQLException ;
14
17
import java .sql .Statement ;
15
18
import java .time .format .DateTimeFormatter ;
16
- import java .util .ArrayList ;
17
19
import java .util .Arrays ;
18
20
import java .util .HashMap ;
19
- import java .util .List ;
20
21
import java .util .Map ;
21
22
22
23
import static com .conveyal .gtfs .util .Util .randomIdString ;
@@ -209,21 +210,16 @@ private TableLoadResult createScheduleExceptionsTable() {
209
210
);
210
211
Iterable <CalendarDate > calendarDates = calendarDatesReader .getAll ();
211
212
212
- // keep track of calendars by service id in case we need to add dummy calendar entries
213
+ // Keep track of calendars by service id in case we need to add dummy calendar entries.
213
214
Map <String , Calendar > calendarsByServiceId = new HashMap <>();
214
215
215
- // iterate through calendar dates to build up to get list of dates with exceptions
216
- HashMap <String , List <String >> removedServiceDays = new HashMap <>();
217
- HashMap <String , List <String >> addedServiceDays = new HashMap <>();
218
- List <String > datesWithExceptions = new ArrayList <>();
216
+ // Iterate through calendar dates to build up to get maps from exceptions to their dates.
217
+ Multimap <String , String > removedServiceForDate = HashMultimap .create ();
218
+ Multimap <String , String > addedServiceForDate = HashMultimap .create ();
219
219
for (CalendarDate calendarDate : calendarDates ) {
220
220
String date = calendarDate .date .format (DateTimeFormatter .BASIC_ISO_DATE );
221
- datesWithExceptions .add (date );
222
221
if (calendarDate .exception_type == 1 ) {
223
- List <String > dateAddedServices = addedServiceDays .getOrDefault (date , new ArrayList <>());
224
- dateAddedServices .add (calendarDate .service_id );
225
- addedServiceDays .put (date , dateAddedServices );
226
-
222
+ addedServiceForDate .put (date , calendarDate .service_id );
227
223
// create (if needed) and extend range of dummy calendar that would need to be created if we are
228
224
// copying from a feed that doesn't have the calendar.txt file
229
225
Calendar calendar = calendarsByServiceId .getOrDefault (calendarDate .service_id , new Calendar ());
@@ -236,33 +232,24 @@ private TableLoadResult createScheduleExceptionsTable() {
236
232
}
237
233
calendarsByServiceId .put (calendarDate .service_id , calendar );
238
234
} else {
239
- List <String > dateRemovedServices = removedServiceDays .getOrDefault (date , new ArrayList <>());
240
- dateRemovedServices .add (calendarDate .service_id );
241
- removedServiceDays .put (date , dateRemovedServices );
235
+ removedServiceForDate .put (date , calendarDate .service_id );
242
236
}
243
237
}
244
-
245
- // iterate through days and add to database
246
- // for usability and simplicity of code, don't attempt to find all dates with similar
247
- // added and removed services, but simply create an entry for each found date
248
- for (String dateWithException : datesWithExceptions ) {
249
- scheduleExceptionsStatement .setString (1 , dateWithException );
250
- String [] dates = {dateWithException };
238
+ // Iterate through dates with added or removed service and add to database.
239
+ // For usability and simplicity of code, don't attempt to find all dates with similar
240
+ // added and removed services, but simply create an entry for each found date.
241
+ for (String date : Sets .union (removedServiceForDate .keySet (), addedServiceForDate .keySet ())) {
242
+ scheduleExceptionsStatement .setString (1 , date );
243
+ String [] dates = {date };
251
244
scheduleExceptionsStatement .setArray (2 , connection .createArrayOf ("text" , dates ));
252
245
scheduleExceptionsStatement .setInt (3 , 9 ); // FIXME use better static type
253
246
scheduleExceptionsStatement .setArray (
254
247
4 ,
255
- connection .createArrayOf (
256
- "text" ,
257
- addedServiceDays .getOrDefault (dateWithException , new ArrayList <>()).toArray ()
258
- )
248
+ connection .createArrayOf ("text" , addedServiceForDate .get (date ).toArray ())
259
249
);
260
250
scheduleExceptionsStatement .setArray (
261
251
5 ,
262
- connection .createArrayOf (
263
- "text" ,
264
- removedServiceDays .getOrDefault (dateWithException , new ArrayList <>()).toArray ()
265
- )
252
+ connection .createArrayOf ("text" , removedServiceForDate .get (date ).toArray ())
266
253
);
267
254
scheduleExceptionsTracker .addBatch ();
268
255
}
0 commit comments