This repository was archived by the owner on Mar 3, 2025. It is now read-only.
forked from HSLdevcom/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDefaultTransitService.java
726 lines (632 loc) · 22.6 KB
/
DefaultTransitService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
package org.opentripplanner.transit.service;
import gnu.trove.set.TIntSet;
import gnu.trove.set.hash.TIntHashSet;
import jakarta.inject.Inject;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.locationtech.jts.geom.Envelope;
import org.opentripplanner.ext.flex.FlexIndex;
import org.opentripplanner.framework.application.OTPRequestTimeoutException;
import org.opentripplanner.model.FeedInfo;
import org.opentripplanner.model.PathTransfer;
import org.opentripplanner.model.StopTimesInPattern;
import org.opentripplanner.model.Timetable;
import org.opentripplanner.model.TimetableSnapshot;
import org.opentripplanner.model.TripTimeOnDate;
import org.opentripplanner.model.calendar.CalendarService;
import org.opentripplanner.model.transfer.TransferService;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.TransitLayer;
import org.opentripplanner.routing.services.TransitAlertService;
import org.opentripplanner.routing.stoptimes.ArrivalDeparture;
import org.opentripplanner.routing.stoptimes.StopTimesHelper;
import org.opentripplanner.transit.model.basic.Notice;
import org.opentripplanner.transit.model.basic.TransitMode;
import org.opentripplanner.transit.model.framework.AbstractTransitEntity;
import org.opentripplanner.transit.model.framework.Deduplicator;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.network.GroupOfRoutes;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.organization.Agency;
import org.opentripplanner.transit.model.organization.Operator;
import org.opentripplanner.transit.model.site.AreaStop;
import org.opentripplanner.transit.model.site.GroupStop;
import org.opentripplanner.transit.model.site.MultiModalStation;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.Station;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.site.StopLocationsGroup;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripIdAndServiceDate;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.updater.GraphUpdaterStatus;
/**
* Default implementation of the Transit Service and Transit Editor Service.
* A new instance of this class should be created for each request.
* This ensures that the same TimetableSnapshot is used for the
* duration of the request (which may involve several method calls).
*/
public class DefaultTransitService implements TransitEditorService {
private final TransitModel transitModel;
private final TransitModelIndex transitModelIndex;
/**
* This should only be accessed through the getTimetableSnapshot method.
*/
private TimetableSnapshot timetableSnapshot;
@Inject
public DefaultTransitService(TransitModel transitModel) {
this.transitModel = transitModel;
this.transitModelIndex = transitModel.getTransitModelIndex();
}
public DefaultTransitService(
TransitModel transitModel,
TimetableSnapshot timetableSnapshotBuffer
) {
this(transitModel);
this.timetableSnapshot = timetableSnapshotBuffer;
}
@Override
public Collection<String> getFeedIds() {
return this.transitModel.getFeedIds();
}
@Override
public Collection<Agency> getAgencies() {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModel.getAgencies();
}
@Override
public Optional<Agency> findAgencyById(FeedScopedId id) {
return this.transitModel.findAgencyById(id);
}
@Override
public FeedInfo getFeedInfo(String feedId) {
return this.transitModel.getFeedInfo(feedId);
}
@Override
public void addAgency(Agency agency) {
this.transitModel.addAgency(agency);
}
@Override
public void addFeedInfo(FeedInfo info) {
this.transitModel.addFeedInfo(info);
}
@Override
public Collection<Operator> getOperators() {
return this.transitModel.getOperators();
}
@Override
public Collection<Notice> getNoticesByEntity(AbstractTransitEntity<?, ?> entity) {
return this.transitModel.getNoticesByElement().get(entity);
}
@Override
public TripPattern getTripPatternForId(FeedScopedId id) {
return this.transitModel.getTripPatternForId(id);
}
@Override
public Collection<TripPattern> getAllTripPatterns() {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModel.getAllTripPatterns();
}
@Override
public Collection<Notice> getNotices() {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModel.getNoticesByElement().values();
}
@Override
public Station getStationById(FeedScopedId id) {
return this.transitModel.getStopModel().getStationById(id);
}
@Override
public MultiModalStation getMultiModalStation(FeedScopedId id) {
return this.transitModel.getStopModel().getMultiModalStation(id);
}
@Override
public Collection<Station> getStations() {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModel.getStopModel().listStations();
}
@Override
public Integer getServiceCodeForId(FeedScopedId id) {
return this.transitModel.getServiceCodes().get(id);
}
@Override
public TIntSet getServiceCodesRunningForDate(LocalDate serviceDate) {
return transitModelIndex
.getServiceCodesRunningForDate()
.getOrDefault(serviceDate, new TIntHashSet());
}
@Override
public AreaStop getAreaStop(FeedScopedId id) {
return this.transitModel.getStopModel().getAreaStop(id);
}
@Override
public Agency getAgencyForId(FeedScopedId id) {
return this.transitModelIndex.getAgencyForId(id);
}
@Override
public RegularStop getRegularStop(FeedScopedId id) {
return this.transitModel.getStopModel().getRegularStop(id);
}
@Override
public Route getRouteForId(FeedScopedId id) {
return this.transitModelIndex.getRouteForId(id);
}
/**
* TODO OTP2 - This is NOT THREAD-SAFE and is used in the real-time updaters, we need to fix
* this when doing the issue #3030.
*/
@Override
public void addRoutes(Route route) {
this.transitModelIndex.addRoutes(route);
}
@Override
public Set<Route> getRoutesForStop(StopLocation stop) {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModelIndex.getRoutesForStop(stop);
}
@Override
public Collection<TripPattern> getPatternsForStop(StopLocation stop) {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModelIndex.getPatternsForStop(stop);
}
@Override
public Collection<Trip> getTripsForStop(StopLocation stop) {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModelIndex.getTripsForStop(stop);
}
@Override
public Collection<Operator> getAllOperators() {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModelIndex.getAllOperators();
}
@Override
public Operator getOperatorForId(FeedScopedId id) {
return this.transitModelIndex.getOperatorForId().get(id);
}
@Override
public Collection<StopLocation> listStopLocations() {
OTPRequestTimeoutException.checkForTimeout();
return transitModel.getStopModel().listStopLocations();
}
@Override
public Collection<RegularStop> listRegularStops() {
OTPRequestTimeoutException.checkForTimeout();
return transitModel.getStopModel().listRegularStops();
}
@Override
public Collection<GroupStop> listGroupStops() {
OTPRequestTimeoutException.checkForTimeout();
return transitModel.getStopModel().listGroupStops();
}
@Override
public StopLocation getStopLocation(FeedScopedId id) {
return transitModel.getStopModel().getStopLocation(id);
}
@Override
public Collection<StopLocation> getStopOrChildStops(FeedScopedId id) {
return transitModel.getStopModel().findStopOrChildStops(id);
}
@Override
public Collection<StopLocationsGroup> listStopLocationGroups() {
OTPRequestTimeoutException.checkForTimeout();
return transitModel.getStopModel().listStopLocationGroups();
}
@Override
public StopLocationsGroup getStopLocationsGroup(FeedScopedId id) {
return transitModel.getStopModel().getStopLocationsGroup(id);
}
@Override
public Trip getTripForId(FeedScopedId id) {
return this.transitModelIndex.getTripForId().get(id);
}
/**
* TODO OTP2 - This is NOT THREAD-SAFE and is used in the real-time updaters, we need to fix
* this when doing the issue #3030.
*/
@Override
public void addTripForId(FeedScopedId tripId, Trip trip) {
transitModelIndex.getTripForId().put(tripId, trip);
}
@Override
public Collection<Trip> getAllTrips() {
OTPRequestTimeoutException.checkForTimeout();
return transitModelIndex.getTripForId().values();
}
@Override
public Collection<Route> getAllRoutes() {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModelIndex.getAllRoutes();
}
@Override
public TripPattern getPatternForTrip(Trip trip) {
return this.transitModelIndex.getPatternForTrip().get(trip);
}
/**
* TODO OTP2 - This is NOT THREAD-SAFE and is used in the real-time updaters, we need to fix
* this when doing the issue #3030.
*/
@Override
public void addPatternForTrip(Trip trip, TripPattern pattern) {
transitModelIndex.getPatternForTrip().put(trip, pattern);
}
@Override
public TripPattern getPatternForTrip(Trip trip, LocalDate serviceDate) {
TripPattern realtimePattern = getRealtimeAddedTripPattern(trip.getId(), serviceDate);
if (realtimePattern != null) {
return realtimePattern;
}
return getPatternForTrip(trip);
}
@Override
public Collection<TripPattern> getPatternsForRoute(Route route) {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModelIndex.getPatternsForRoute().get(route);
}
/**
* TODO OTP2 - This is NOT THREAD-SAFE and is used in the real-time updaters, we need to fix
* this when doing the issue #3030.
*/
@Override
public void addPatternsForRoute(Route route, TripPattern pattern) {
transitModelIndex.getPatternsForRoute().put(route, pattern);
}
@Override
public MultiModalStation getMultiModalStationForStation(Station station) {
return this.transitModel.getStopModel().getMultiModalStationForStation(station);
}
/**
* Fetch upcoming vehicle departures from a stop. It goes though all patterns passing the stop for
* the previous, current and next service date. It uses a priority queue to keep track of the next
* departures. The queue is shared between all dates, as services from the previous service date
* can visit the stop later than the current service date's services. This happens eg. with
* sleeper trains.
* <p>
* TODO: Add frequency based trips
*
* @param stop Stop object to perform the search for
* @param startTime Start time for the search.
* @param timeRange Searches forward for timeRange from startTime
* @param numberOfDepartures Number of departures to fetch per pattern
* @param arrivalDeparture Filter by arrivals, departures, or both
* @param includeCancelledTrips If true, cancelled trips will also be included in result.
*/
@Override
public List<StopTimesInPattern> stopTimesForStop(
StopLocation stop,
Instant startTime,
Duration timeRange,
int numberOfDepartures,
ArrivalDeparture arrivalDeparture,
boolean includeCancelledTrips
) {
OTPRequestTimeoutException.checkForTimeout();
return StopTimesHelper.stopTimesForStop(
this,
stop,
startTime,
timeRange,
numberOfDepartures,
arrivalDeparture,
includeCancelledTrips
);
}
/**
* Get a list of all trips that pass through a stop during a single ServiceDate. Useful when
* creating complete stop timetables for a single day.
*
* @param stop Stop object to perform the search for
* @param serviceDate Return all departures for the specified date
*/
@Override
public List<StopTimesInPattern> getStopTimesForStop(
StopLocation stop,
LocalDate serviceDate,
ArrivalDeparture arrivalDeparture,
boolean includeCancellations
) {
OTPRequestTimeoutException.checkForTimeout();
return StopTimesHelper.stopTimesForStop(
this,
stop,
serviceDate,
arrivalDeparture,
includeCancellations
);
}
/**
* Fetch upcoming vehicle departures from a stop for a specific pattern, passing the stop for the
* previous, current and next service date. It uses a priority queue to keep track of the next
* departures. The queue is shared between all dates, as services from the previous service date
* can visit the stop later than the current service date's services.
* <p>
* TODO: Add frequency based trips
*
* @param stop Stop object to perform the search for
* @param pattern Pattern object to perform the search for
* @param startTime Start time for the search.
* @param timeRange Searches forward for timeRange from startTime
* @param numberOfDepartures Number of departures to fetch per pattern
* @param arrivalDeparture Filter by arrivals, departures, or both
*/
@Override
public List<TripTimeOnDate> stopTimesForPatternAtStop(
StopLocation stop,
TripPattern pattern,
Instant startTime,
Duration timeRange,
int numberOfDepartures,
ArrivalDeparture arrivalDeparture,
boolean includeCancellations
) {
OTPRequestTimeoutException.checkForTimeout();
return StopTimesHelper.stopTimesForPatternAtStop(
this,
stop,
pattern,
startTime,
timeRange,
numberOfDepartures,
arrivalDeparture,
includeCancellations
);
}
/**
* Returns all the patterns for a specific stop. If includeRealtimeUpdates is set, new patterns
* added by realtime updates are added to the collection.
* A set is used here because trip patterns
* that were updated by realtime data is both part of the TransitModelIndex and the TimetableSnapshot
*/
@Override
public Collection<TripPattern> getPatternsForStop(
StopLocation stop,
boolean includeRealtimeUpdates
) {
Set<TripPattern> tripPatterns = new HashSet<>(getPatternsForStop(stop));
if (includeRealtimeUpdates) {
TimetableSnapshot currentSnapshot = lazyGetTimeTableSnapShot();
if (currentSnapshot != null) {
tripPatterns.addAll(currentSnapshot.getPatternsForStop(stop));
}
}
return tripPatterns;
}
@Override
public Collection<GroupOfRoutes> getGroupsOfRoutes() {
OTPRequestTimeoutException.checkForTimeout();
return transitModelIndex.getRoutesForGroupOfRoutes().keySet();
}
@Override
public Collection<Route> getRoutesForGroupOfRoutes(GroupOfRoutes groupOfRoutes) {
OTPRequestTimeoutException.checkForTimeout();
return transitModelIndex.getRoutesForGroupOfRoutes().get(groupOfRoutes);
}
@Override
public GroupOfRoutes getGroupOfRoutesForId(FeedScopedId id) {
return transitModelIndex.getGroupOfRoutesForId().get(id);
}
/**
* Get the most up-to-date timetable for the given TripPattern, as of right now. There should
* probably be a less awkward way to do this that just gets the latest entry from the resolver
* without making a fake routing request.
*/
@Override
public Timetable getTimetableForTripPattern(TripPattern tripPattern, LocalDate serviceDate) {
OTPRequestTimeoutException.checkForTimeout();
TimetableSnapshot currentSnapshot = lazyGetTimeTableSnapShot();
return currentSnapshot != null
? currentSnapshot.resolve(tripPattern, serviceDate)
: tripPattern.getScheduledTimetable();
}
@Override
public TripPattern getRealtimeAddedTripPattern(FeedScopedId tripId, LocalDate serviceDate) {
TimetableSnapshot currentSnapshot = lazyGetTimeTableSnapShot();
if (currentSnapshot == null) {
return null;
}
return currentSnapshot.getRealtimeAddedTripPattern(tripId, serviceDate);
}
@Override
public boolean hasRealtimeAddedTripPatterns() {
TimetableSnapshot currentSnapshot = lazyGetTimeTableSnapShot();
if (currentSnapshot == null) {
return false;
}
return currentSnapshot.hasRealtimeAddedTripPatterns();
}
/**
* Lazy-initialization of TimetableSnapshot
*
* @return The same TimetableSnapshot is returned throughout the lifecycle of this object.
*/
@Nullable
private TimetableSnapshot lazyGetTimeTableSnapShot() {
if (this.timetableSnapshot == null) {
timetableSnapshot = transitModel.getTimetableSnapshot();
}
return this.timetableSnapshot;
}
@Override
public TripOnServiceDate getTripOnServiceDateById(FeedScopedId datedServiceJourneyId) {
return transitModelIndex.getTripOnServiceDateById().get(datedServiceJourneyId);
}
/**
* TODO OTP2 - This is NOT THREAD-SAFE and is used in the real-time updaters, we need to fix
* this when doing the issue #3030.
*/
@Override
public void addTripOnServiceDateById(FeedScopedId id, TripOnServiceDate tripOnServiceDate) {
transitModelIndex.getTripOnServiceDateById().put(id, tripOnServiceDate);
}
@Override
public Collection<TripOnServiceDate> getAllTripOnServiceDates() {
return transitModelIndex.getTripOnServiceDateForTripAndDay().values();
}
@Override
public TripOnServiceDate getTripOnServiceDateForTripAndDay(
TripIdAndServiceDate tripIdAndServiceDate
) {
return transitModelIndex.getTripOnServiceDateForTripAndDay().get(tripIdAndServiceDate);
}
/**
* TODO OTP2 - This is NOT THREAD-SAFE and is used in the real-time updaters, we need to fix
* this when doing the issue #3030.
*/
@Override
public void addTripOnServiceDateForTripAndDay(
TripIdAndServiceDate tripIdAndServiceDate,
TripOnServiceDate tripOnServiceDate
) {
transitModelIndex
.getTripOnServiceDateForTripAndDay()
.put(tripIdAndServiceDate, tripOnServiceDate);
}
/**
* TODO OTP2 - This is NOT THREAD-SAFE and is used in the real-time updaters, we need to fix
* this when doing the issue #3030.
*/
@Override
public FeedScopedId getOrCreateServiceIdForDate(LocalDate serviceDate) {
return transitModel.getOrCreateServiceIdForDate(serviceDate);
}
@Override
public void addTransitMode(TransitMode mode) {
this.transitModel.addTransitMode(mode);
}
@Override
public Set<TransitMode> getTransitModes() {
return this.transitModel.getTransitModes();
}
@Override
public Collection<PathTransfer> getTransfersByStop(StopLocation stop) {
return this.transitModel.getTransfersByStop(stop);
}
@Override
public TransitLayer getTransitLayer() {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModel.getTransitLayer();
}
@Override
public TransitLayer getRealtimeTransitLayer() {
OTPRequestTimeoutException.checkForTimeout();
return this.transitModel.getRealtimeTransitLayer();
}
@Override
public void setTransitLayer(TransitLayer transitLayer) {
this.transitModel.setTransitLayer(transitLayer);
}
@Override
public void setRealtimeTransitLayer(TransitLayer realtimeTransitLayer) {
transitModel.setRealtimeTransitLayer(realtimeTransitLayer);
}
@Override
public boolean hasRealtimeTransitLayer() {
return transitModel.hasRealtimeTransitLayer();
}
@Override
public CalendarService getCalendarService() {
return this.transitModel.getCalendarService();
}
@Override
public ZoneId getTimeZone() {
return this.transitModel.getTimeZone();
}
@Override
public TransitAlertService getTransitAlertService() {
return this.transitModel.getTransitAlertService();
}
@Override
public FlexIndex getFlexIndex() {
return this.transitModelIndex.getFlexIndex();
}
@Override
public ZonedDateTime getTransitServiceEnds() {
return transitModel.getTransitServiceEnds();
}
@Override
public ZonedDateTime getTransitServiceStarts() {
return transitModel.getTransitServiceStarts();
}
@Override
public Collection<RegularStop> findRegularStops(Envelope envelope) {
OTPRequestTimeoutException.checkForTimeout();
return transitModel.getStopModel().findRegularStops(envelope);
}
@Override
public Collection<AreaStop> findAreaStops(Envelope envelope) {
OTPRequestTimeoutException.checkForTimeout();
return transitModel.getStopModel().findAreaStops(envelope);
}
@Override
public GraphUpdaterStatus getUpdaterStatus() {
return transitModel.getUpdaterManager();
}
@Override
public List<TransitMode> getModesOfStopLocationsGroup(StopLocationsGroup station) {
return sortByOccurrenceAndReduce(
station.getChildStops().stream().flatMap(this::getPatternModesOfStop)
)
.toList();
}
@Override
public List<TransitMode> getModesOfStopLocation(StopLocation stop) {
return sortByOccurrenceAndReduce(getPatternModesOfStop(stop)).toList();
}
@Override
public Deduplicator getDeduplicator() {
return transitModel.getDeduplicator();
}
@Override
public Set<LocalDate> getAllServiceCodes() {
return Collections.unmodifiableSet(transitModelIndex.getServiceCodesRunningForDate().keySet());
}
@Override
public Map<LocalDate, TIntSet> getServiceCodesRunningForDate() {
return Collections.unmodifiableMap(transitModelIndex.getServiceCodesRunningForDate());
}
/**
* For each pattern visiting this {@link StopLocation} return its {@link TransitMode}
*/
private Stream<TransitMode> getPatternModesOfStop(StopLocation stop) {
if (stop.getGtfsVehicleType() != null) {
return Stream.of(stop.getGtfsVehicleType());
} else {
return getPatternsForStop(stop).stream().map(TripPattern::getMode);
}
}
@Override
public TransferService getTransferService() {
return transitModel.getTransferService();
}
@Override
public boolean transitFeedCovers(Instant dateTime) {
return transitModel.transitFeedCovers(dateTime);
}
/**
* Take a stream of T, count the occurrences of each value and return it in order of frequency
* from high to low.
* <p>
* Example: [a,b,b,c,c,c] will return [c,b,a]
*/
private static <T> Stream<T> sortByOccurrenceAndReduce(Stream<T> input) {
return input
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.sorted(Map.Entry.<T, Long>comparingByValue().reversed())
.map(Map.Entry::getKey);
}
}