|
| 1 | +/* |
| 2 | + * Copyright 2024 MobilityData |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.mobilitydata.gtfsvalidator.validator; |
| 17 | + |
| 18 | +import static org.mobilitydata.gtfsvalidator.notice.SeverityLevel.ERROR; |
| 19 | + |
| 20 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice; |
| 21 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; |
| 22 | +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; |
| 23 | +import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; |
| 24 | +import org.mobilitydata.gtfsvalidator.table.GtfsStopTime; |
| 25 | +import org.mobilitydata.gtfsvalidator.type.GtfsTime; |
| 26 | + |
| 27 | +/** |
| 28 | + * Validates pickup and drop-off windows in the `stop_times.txt` file to ensure compliance with GTFS |
| 29 | + * rules. |
| 30 | + * |
| 31 | + * <p>This validator checks for: - Forbidden use of arrival or departure times when pickup or |
| 32 | + * drop-off windows are provided. - Missing start or end pickup/drop-off windows when one of them is |
| 33 | + * present. - Invalid pickup/drop-off windows where the end time is not strictly later than the |
| 34 | + * start time. |
| 35 | + * |
| 36 | + * <p>Generated notices include: - {@link ForbiddenArrivalOrDepartureTimeNotice} - {@link |
| 37 | + * MissingPickupOrDropOffWindowNotice} - {@link InvalidPickupDropOffWindowNotice} |
| 38 | + */ |
| 39 | +@GtfsValidator |
| 40 | +public class PickupDropOffWindowValidator extends SingleEntityValidator<GtfsStopTime> { |
| 41 | + |
| 42 | + @Override |
| 43 | + public void validate(GtfsStopTime stopTime, NoticeContainer noticeContainer) { |
| 44 | + // Skip validation if neither start nor end pickup/drop-off window is present |
| 45 | + if (!stopTime.hasStartPickupDropOffWindow() && !stopTime.hasEndPickupDropOffWindow()) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // Check for forbidden coexistence of arrival/departure times with pickup/drop-off windows |
| 50 | + if (stopTime.hasArrivalTime() || stopTime.hasDepartureTime()) { |
| 51 | + noticeContainer.addValidationNotice( |
| 52 | + new ForbiddenArrivalOrDepartureTimeNotice( |
| 53 | + stopTime.csvRowNumber(), |
| 54 | + stopTime.hasArrivalTime() ? stopTime.arrivalTime() : null, |
| 55 | + stopTime.hasDepartureTime() ? stopTime.departureTime() : null, |
| 56 | + stopTime.hasStartPickupDropOffWindow() ? stopTime.startPickupDropOffWindow() : null, |
| 57 | + stopTime.hasEndPickupDropOffWindow() ? stopTime.endPickupDropOffWindow() : null)); |
| 58 | + } |
| 59 | + |
| 60 | + // Check for missing start or end pickup/drop-off window |
| 61 | + if (!stopTime.hasStartPickupDropOffWindow() || !stopTime.hasEndPickupDropOffWindow()) { |
| 62 | + noticeContainer.addValidationNotice( |
| 63 | + new MissingPickupOrDropOffWindowNotice( |
| 64 | + stopTime.csvRowNumber(), |
| 65 | + stopTime.hasStartPickupDropOffWindow() ? stopTime.startPickupDropOffWindow() : null, |
| 66 | + stopTime.hasEndPickupDropOffWindow() ? stopTime.endPickupDropOffWindow() : null)); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Check for invalid pickup/drop-off window (start time must be strictly before end time) |
| 71 | + if (stopTime.startPickupDropOffWindow().isAfter(stopTime.endPickupDropOffWindow()) |
| 72 | + || stopTime.startPickupDropOffWindow().equals(stopTime.endPickupDropOffWindow())) { |
| 73 | + noticeContainer.addValidationNotice( |
| 74 | + new InvalidPickupDropOffWindowNotice( |
| 75 | + stopTime.csvRowNumber(), |
| 76 | + stopTime.startPickupDropOffWindow(), |
| 77 | + stopTime.endPickupDropOffWindow())); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean shouldCallValidate(ColumnInspector header) { |
| 83 | + // No point in validating if there is no start_pickup_drop_off_window column |
| 84 | + // and no end_pickup_drop_off_window column |
| 85 | + return header.hasColumn(GtfsStopTime.START_PICKUP_DROP_OFF_WINDOW_FIELD_NAME) |
| 86 | + || header.hasColumn(GtfsStopTime.END_PICKUP_DROP_OFF_WINDOW_FIELD_NAME); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * The arrival or departure times are provided alongside pickup or drop-off windows in |
| 91 | + * `stop_times.txt`. |
| 92 | + * |
| 93 | + * <p>This violates GTFS specification, as both cannot coexist for a single stop time record. |
| 94 | + */ |
| 95 | + @GtfsValidationNotice(severity = ERROR) |
| 96 | + public static class ForbiddenArrivalOrDepartureTimeNotice extends ValidationNotice { |
| 97 | + |
| 98 | + /** The row of the faulty record. */ |
| 99 | + private final int csvRowNumber; |
| 100 | + |
| 101 | + /** The arrival time of the faulty record. */ |
| 102 | + private final GtfsTime arrivalTime; |
| 103 | + |
| 104 | + /** The departure time of the faulty record. */ |
| 105 | + private final GtfsTime departureTime; |
| 106 | + |
| 107 | + /** The start pickup drop off window of the faulty record. */ |
| 108 | + private final GtfsTime startPickupDropOffWindow; |
| 109 | + |
| 110 | + /** The end pickup drop off window of the faulty record. */ |
| 111 | + private final GtfsTime endPickupDropOffWindow; |
| 112 | + |
| 113 | + public ForbiddenArrivalOrDepartureTimeNotice( |
| 114 | + int csvRowNumber, |
| 115 | + GtfsTime arrivalTime, |
| 116 | + GtfsTime departureTime, |
| 117 | + GtfsTime startPickupDropOffWindow, |
| 118 | + GtfsTime endPickupDropOffWindow) { |
| 119 | + this.csvRowNumber = csvRowNumber; |
| 120 | + this.arrivalTime = arrivalTime; |
| 121 | + this.departureTime = departureTime; |
| 122 | + this.startPickupDropOffWindow = startPickupDropOffWindow; |
| 123 | + this.endPickupDropOffWindow = endPickupDropOffWindow; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Either the start or end pickup/drop-off window is missing in `stop_times.txt`. |
| 129 | + * |
| 130 | + * <p>GTFS specification requires both the start and end pickup/drop-off windows to be provided |
| 131 | + * together, if used. |
| 132 | + */ |
| 133 | + @GtfsValidationNotice(severity = ERROR) |
| 134 | + public static class MissingPickupOrDropOffWindowNotice extends ValidationNotice { |
| 135 | + /** The row of the faulty record. */ |
| 136 | + private final int csvRowNumber; |
| 137 | + |
| 138 | + /** The start pickup drop off window of the faulty record. */ |
| 139 | + private final GtfsTime startPickupDropOffWindow; |
| 140 | + |
| 141 | + /** The end pickup drop off window of the faulty record. */ |
| 142 | + private final GtfsTime endPickupDropOffWindow; |
| 143 | + |
| 144 | + public MissingPickupOrDropOffWindowNotice( |
| 145 | + int csvRowNumber, GtfsTime startPickupDropOffWindow, GtfsTime endPickupDropOffWindow) { |
| 146 | + this.csvRowNumber = csvRowNumber; |
| 147 | + this.startPickupDropOffWindow = startPickupDropOffWindow; |
| 148 | + this.endPickupDropOffWindow = endPickupDropOffWindow; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * The pickup/drop-off window in `stop_times.txt` is invalid. |
| 154 | + * |
| 155 | + * <p>The `end_pickup_drop_off_window` must be strictly later than the |
| 156 | + * `start_pickup_drop_off_window`. |
| 157 | + */ |
| 158 | + @GtfsValidationNotice(severity = ERROR) |
| 159 | + public static class InvalidPickupDropOffWindowNotice extends ValidationNotice { |
| 160 | + /** The row of the faulty record. */ |
| 161 | + private final int csvRowNumber; |
| 162 | + |
| 163 | + /** The start pickup drop off window of the faulty record. */ |
| 164 | + private final GtfsTime startPickupDropOffWindow; |
| 165 | + |
| 166 | + /** The end pickup drop off window of the faulty record. */ |
| 167 | + private final GtfsTime endPickupDropOffWindow; |
| 168 | + |
| 169 | + public InvalidPickupDropOffWindowNotice( |
| 170 | + int csvRowNumber, GtfsTime startPickupDropOffWindow, GtfsTime endPickupDropOffWindow) { |
| 171 | + this.csvRowNumber = csvRowNumber; |
| 172 | + this.startPickupDropOffWindow = startPickupDropOffWindow; |
| 173 | + this.endPickupDropOffWindow = endPickupDropOffWindow; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments