Skip to content

Commit

Permalink
Use more Result types
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Sep 2, 2022
1 parent 5f5f19b commit 8e24059
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static org.opentripplanner.model.UpdateError.UpdateErrorType.NO_START_DATE;
import static org.opentripplanner.model.UpdateError.UpdateErrorType.NO_UPDATES;
import static org.opentripplanner.model.UpdateError.UpdateErrorType.TRIP_NOT_FOUND_IN_PATTERN;
import static org.opentripplanner.model.UpdateError.UpdateErrorType.UNKNOWN;

import com.google.common.base.Preconditions;
import java.time.LocalDate;
Expand Down Expand Up @@ -890,23 +889,23 @@ private List<UpdateError> handleModifiedTrip(

if (exactPattern != null) {
Timetable currentTimetable = getCurrentTimetable(exactPattern, serviceDate);
TripTimes exactUpdatedTripTimes = createUpdatedTripTimes(
var updateResult = createUpdatedTripTimes(
currentTimetable,
estimatedVehicleJourney,
tripMatchedByServiceJourneyId.getId(),
transitModel::getStopLocationById,
timeZone,
transitModel.getDeduplicator()
);
if (exactUpdatedTripTimes != null) {
times.add(exactUpdatedTripTimes);
if (updateResult.isSuccess()) {
times.add(updateResult.successValue());
patterns.add(exactPattern);
} else {
LOG.info(
"Failed to update TripTimes for trip found by exact match {}",
tripMatchedByServiceJourneyId.getId()
);
return List.of(new UpdateError(tripMatchedByServiceJourneyId.getId(), UNKNOWN));
return List.of(updateResult.failureValue());
}
}
} else {
Expand Down Expand Up @@ -942,18 +941,18 @@ private List<UpdateError> handleModifiedTrip(
TripPattern pattern = getPatternForTrip(matchingTrip, estimatedVehicleJourney);
if (pattern != null) {
Timetable currentTimetable = getCurrentTimetable(pattern, serviceDate);
TripTimes updatedTripTimes = createUpdatedTripTimes(
var updateResult = createUpdatedTripTimes(
currentTimetable,
estimatedVehicleJourney,
matchingTrip.getId(),
transitModel::getStopLocationById,
timeZone,
transitModel.getDeduplicator()
);
if (updatedTripTimes != null) {
updateResult.ifSuccess(tripTimes -> {
patterns.add(pattern);
times.add(updatedTripTimes);
}
times.add(tripTimes);
});
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/ext/java/org/opentripplanner/ext/siri/TimetableHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.opentripplanner.model.PickDrop.SCHEDULED;
import static org.opentripplanner.model.UpdateError.UpdateErrorType.INVALID_INPUT_STRUCTURE;
import static org.opentripplanner.model.UpdateError.UpdateErrorType.NON_INCREASING_TRIP_TIMES;
import static org.opentripplanner.model.UpdateError.UpdateErrorType.TOO_FEW_STOPS;
import static org.opentripplanner.model.UpdateError.UpdateErrorType.TRIP_NOT_FOUND_IN_PATTERN;
import static org.opentripplanner.model.UpdateError.UpdateErrorType.UNKNOWN;

Expand Down Expand Up @@ -63,7 +64,7 @@ public class TimetableHelper {
* @return new copy of updated TripTimes after TripUpdate has been applied on TripTimes of trip
* with the id specified in the trip descriptor of the TripUpdate; null if something went wrong
*/
public static TripTimes createUpdatedTripTimes(
public static Result<TripTimes, UpdateError> createUpdatedTripTimes(
Timetable timetable,
EstimatedVehicleJourney journey,
FeedScopedId tripId,
Expand All @@ -78,14 +79,14 @@ public static TripTimes createUpdatedTripTimes(
final TripTimes existingTripTimes = timetable.getTripTimes(tripId);
if (existingTripTimes == null) {
LOG.debug("tripId {} not found in pattern.", tripId);
return null;
return UpdateError.result(tripId, TRIP_NOT_FOUND_IN_PATTERN);
}

TripTimes oldTimes = new TripTimes(existingTripTimes);

if (journey.isCancellation() != null && journey.isCancellation()) {
oldTimes.cancelTrip();
return oldTimes;
return Result.success(oldTimes);
}

List<EstimatedCall> estimatedCalls = getEstimatedCalls(journey);
Expand All @@ -109,7 +110,7 @@ public static TripTimes createUpdatedTripTimes(
getStopById
);
if (modifiedStopTimes == null) {
return null;
return UpdateError.result(tripId, UNKNOWN);
}
TripTimes newTimes = new TripTimes(oldTimes.getTrip(), modifiedStopTimes, deduplicator);

Expand Down Expand Up @@ -387,15 +388,15 @@ public static TripTimes createUpdatedTripTimes(
tripId,
invalidStopIndex.getAsInt()
);
return null;
return UpdateError.result(tripId, NON_INCREASING_TRIP_TIMES);
}

if (newTimes.getNumStops() != pattern.numberOfStops()) {
return null;
return UpdateError.result(tripId, TOO_FEW_STOPS);
}

LOG.debug("A valid TripUpdate object was applied using the Timetable class update method.");
return newTimes;
return Result.success(newTimes);
}

private static int calculateDayOffset(TripTimes oldTimes) {
Expand Down

0 comments on commit 8e24059

Please sign in to comment.