From 16e962e38ba86f8eac80158dc0da3cf2bff1e9fa Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Tue, 30 Aug 2022 14:07:37 +0200 Subject: [PATCH] Update Javadoc --- .../opentripplanner/ext/siri/TimetableHelper.java | 6 +++--- .../org/opentripplanner/common/model/Result.java | 10 +++++----- .../java/org/opentripplanner/model/Timetable.java | 12 ++++++------ .../updater/stoptime/TimetableSnapshotSource.java | 10 +++++----- .../org/opentripplanner/common/model/ResultTest.java | 4 ++-- .../org/opentripplanner/model/TimetableTest.java | 5 ++++- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/ext/java/org/opentripplanner/ext/siri/TimetableHelper.java b/src/ext/java/org/opentripplanner/ext/siri/TimetableHelper.java index c7807314f09..64af0de0654 100644 --- a/src/ext/java/org/opentripplanner/ext/siri/TimetableHelper.java +++ b/src/ext/java/org/opentripplanner/ext/siri/TimetableHelper.java @@ -688,10 +688,10 @@ public static List createModifiedStopTimes( * all trips in a timetable are from the same feed, which should always be the case. * * @param activity SIRI-VM VehicleActivity - * @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 + * @return a Result with a copy of updated TripTimes after TripUpdate has been applied on TripTimes of trip + * with the id specified in the trip descriptor of the TripUpdate; a failed Result if something went wrong */ - public static Result createUpdatedTripTimes( + public static Result createUpdatedTripTimes( Timetable timetable, VehicleActivityStructure activity, FeedScopedId tripId, diff --git a/src/main/java/org/opentripplanner/common/model/Result.java b/src/main/java/org/opentripplanner/common/model/Result.java index 284ab55ccb5..dcbb5cf7e35 100644 --- a/src/main/java/org/opentripplanner/common/model/Result.java +++ b/src/main/java/org/opentripplanner/common/model/Result.java @@ -9,15 +9,15 @@ *

* It's very similar to the Either or Validation type found in functional programming languages. */ -public abstract class Result { +public abstract sealed class Result { private Result() {} - public static Result failure(@Nonnull E failure) { + public static Result failure(@Nonnull E failure) { return new Failure<>(failure); } - public static Result success(@Nonnull T success) { + public static Result success(@Nonnull T success) { return new Success<>(success); } @@ -83,7 +83,7 @@ public T successValue() { } } - private static class Failure extends Result { + private static final class Failure extends Result { private final E failure; @@ -110,7 +110,7 @@ public void ifFailure(Consumer func) { } } - private static class Success extends Result { + private static final class Success extends Result { private final T success; diff --git a/src/main/java/org/opentripplanner/model/Timetable.java b/src/main/java/org/opentripplanner/model/Timetable.java index e34fa32ac99..3f83511f519 100644 --- a/src/main/java/org/opentripplanner/model/Timetable.java +++ b/src/main/java/org/opentripplanner/model/Timetable.java @@ -146,22 +146,22 @@ public TripTimes setTripTimes(int tripIndex, TripTimes tt) { * @param updateServiceDate service date of trip update * @param backwardsDelayPropagationType Defines when delays are propagated to previous stops and * if these stops are given the NO_DATA flag - * @return {@link TripTimesPatch} that contains a 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 and a list of stop indices that have been skipped with the realtime update; null if - * something went wrong + * @return {@link Result} contains either a 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 and a list of stop indices that have been skipped with the + * realtime update; or an error if something went wrong *

* TODO OTP2 - This method depend on GTFS RealTime classes. Refactor this so GTFS RT can do * - its job without sending in GTFS specific classes. A generic update would support * - other RealTime updats, not just from GTFS. */ - public Result createUpdatedTripTimes( + public Result createUpdatedTripTimes( TripUpdate tripUpdate, ZoneId timeZone, LocalDate updateServiceDate, BackwardsDelayPropagationType backwardsDelayPropagationType ) { - Result generalError = Result.failure( + Result generalError = Result.failure( UpdateError.noTripId(UNKNOWN) ); if (tripUpdate == null) { diff --git a/src/main/java/org/opentripplanner/updater/stoptime/TimetableSnapshotSource.java b/src/main/java/org/opentripplanner/updater/stoptime/TimetableSnapshotSource.java index 819668611f1..5ae3c3d55d7 100644 --- a/src/main/java/org/opentripplanner/updater/stoptime/TimetableSnapshotSource.java +++ b/src/main/java/org/opentripplanner/updater/stoptime/TimetableSnapshotSource.java @@ -472,7 +472,7 @@ private Optional handleScheduledTrip( * * @param tripUpdate GTFS-RT TripUpdate message * @param tripDescriptor GTFS-RT TripDescriptor - * @return true if successful + * @return empty Optional if successful or one containing en error */ private Optional validateAndHandleAddedTrip( final TripUpdate tripUpdate, @@ -621,7 +621,7 @@ private List checkNewStopTimeUpdatesAndFindStops( * @param tripDescriptor GTFS-RT TripDescriptor * @param stops the stops of each StopTimeUpdate in the TripUpdate message * @param serviceDate service date for added trip - * @return true if successful + * @return empty Optional if successful or one containing en error */ private Optional handleAddedTrip( final TripUpdate tripUpdate, @@ -716,7 +716,7 @@ private Optional handleAddedTrip( * @param stops list of stops corresponding to stop time updates * @param serviceDate service date of trip * @param realTimeState real-time state of new trip - * @return true if successful + * @return empty Optional if successful or one containing en error */ private Optional addTripToGraphAndBuffer( final Trip trip, @@ -906,7 +906,7 @@ private Optional handleUnscheduledTrip(FeedScopedId tripId) { * * @param tripUpdate GTFS-RT TripUpdate message * @param tripDescriptor GTFS-RT TripDescriptor - * @return true if successful + * @return empty Optional if successful or one containing en error */ private Optional validateAndHandleModifiedTrip( final TripUpdate tripUpdate, @@ -974,7 +974,7 @@ private Optional validateAndHandleModifiedTrip( * @param tripUpdate GTFS-RT TripUpdate message * @param stops the stops of each StopTimeUpdate in the TripUpdate message * @param serviceDate service date for modified trip - * @return true if successful + * @return empty Optional if successful or one containing en error */ private Optional handleModifiedTrip( final Trip trip, diff --git a/src/test/java/org/opentripplanner/common/model/ResultTest.java b/src/test/java/org/opentripplanner/common/model/ResultTest.java index 6e7b075bcf3..ca38ba3673f 100644 --- a/src/test/java/org/opentripplanner/common/model/ResultTest.java +++ b/src/test/java/org/opentripplanner/common/model/ResultTest.java @@ -9,7 +9,7 @@ class ResultTest { @Test public void success() { - Result res = Result.success("hello"); + Result res = Result.success("hello"); assertTrue(res.isSuccess()); assertFalse(res.isFailure()); @@ -33,7 +33,7 @@ public void success() { @Test public void failure() { var msg = "An error happened"; - Result res = Result.failure(new RuntimeException(msg)); + Result res = Result.failure(new RuntimeException(msg)); assertFalse(res.isSuccess()); assertTrue(res.isFailure()); diff --git a/src/test/java/org/opentripplanner/model/TimetableTest.java b/src/test/java/org/opentripplanner/model/TimetableTest.java index 0019f47075f..f2052af35b1 100644 --- a/src/test/java/org/opentripplanner/model/TimetableTest.java +++ b/src/test/java/org/opentripplanner/model/TimetableTest.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.opentripplanner.model.UpdateError.UpdateErrorType.NON_INCREASING_TRIP_TIMES; import static org.opentripplanner.model.UpdateError.UpdateErrorType.TRIP_NOT_FOUND_IN_PATTERN; +import static org.opentripplanner.model.UpdateError.UpdateErrorType.UNKNOWN; import static org.opentripplanner.util.TestUtils.AUGUST; import com.google.transit.realtime.GtfsRealtime.TripDescriptor; @@ -69,7 +70,7 @@ public void tripNotFoundInPattern() { stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.NO_DATA); var tripUpdate = tripUpdateBuilder.build(); - Result result = timetable.createUpdatedTripTimes( + Result result = timetable.createUpdatedTripTimes( tripUpdate, timeZone, serviceDate, @@ -105,6 +106,8 @@ public void badData() { BackwardsDelayPropagationType.REQUIRED_NO_DATA ); assertTrue(result.isFailure()); + + result.ifFailure(e -> assertEquals(UNKNOWN, e.errorType())); } @Test