Skip to content

Commit

Permalink
Update Javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Aug 30, 2022
1 parent ac2bc4b commit 16e962e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,10 @@ public static List<StopTime> 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<UpdateError, TripTimes> createUpdatedTripTimes(
public static Result<TripTimes, UpdateError> createUpdatedTripTimes(
Timetable timetable,
VehicleActivityStructure activity,
FeedScopedId tripId,
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/opentripplanner/common/model/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* <p>
* It's very similar to the Either or Validation type found in functional programming languages.
*/
public abstract class Result<E, T> {
public abstract sealed class Result<T, E> {

private Result() {}

public static <E, T> Result<E, T> failure(@Nonnull E failure) {
public static <T, E> Result<T, E> failure(@Nonnull E failure) {
return new Failure<>(failure);
}

public static <E, T> Result<E, T> success(@Nonnull T success) {
public static <T, E> Result<T, E> success(@Nonnull T success) {
return new Success<>(success);
}

Expand Down Expand Up @@ -83,7 +83,7 @@ public T successValue() {
}
}

private static class Failure<E, T> extends Result<E, T> {
private static final class Failure<T, E> extends Result<T, E> {

private final E failure;

Expand All @@ -110,7 +110,7 @@ public void ifFailure(Consumer<E> func) {
}
}

private static class Success<E, T> extends Result<E, T> {
private static final class Success<T, E> extends Result<T, E> {

private final T success;

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/opentripplanner/model/Timetable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<TripTimesPatch, UpdateError>} 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
* <p>
* 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<UpdateError, TripTimesPatch> createUpdatedTripTimes(
public Result<TripTimesPatch, UpdateError> createUpdatedTripTimes(
TripUpdate tripUpdate,
ZoneId timeZone,
LocalDate updateServiceDate,
BackwardsDelayPropagationType backwardsDelayPropagationType
) {
Result<UpdateError, TripTimesPatch> generalError = Result.failure(
Result<TripTimesPatch, UpdateError> generalError = Result.failure(
UpdateError.noTripId(UNKNOWN)
);
if (tripUpdate == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ private Optional<UpdateError> 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<UpdateError> validateAndHandleAddedTrip(
final TripUpdate tripUpdate,
Expand Down Expand Up @@ -621,7 +621,7 @@ private List<StopLocation> 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<UpdateError> handleAddedTrip(
final TripUpdate tripUpdate,
Expand Down Expand Up @@ -716,7 +716,7 @@ private Optional<UpdateError> 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<UpdateError> addTripToGraphAndBuffer(
final Trip trip,
Expand Down Expand Up @@ -906,7 +906,7 @@ private Optional<UpdateError> 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<UpdateError> validateAndHandleModifiedTrip(
final TripUpdate tripUpdate,
Expand Down Expand Up @@ -974,7 +974,7 @@ private Optional<UpdateError> 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<UpdateError> handleModifiedTrip(
final Trip trip,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ResultTest {

@Test
public void success() {
Result<Exception, String> res = Result.success("hello");
Result<String, Exception> res = Result.success("hello");
assertTrue(res.isSuccess());
assertFalse(res.isFailure());

Expand All @@ -33,7 +33,7 @@ public void success() {
@Test
public void failure() {
var msg = "An error happened";
Result<Exception, String> res = Result.failure(new RuntimeException(msg));
Result<String, Exception> res = Result.failure(new RuntimeException(msg));

assertFalse(res.isSuccess());
assertTrue(res.isFailure());
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/org/opentripplanner/model/TimetableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void tripNotFoundInPattern() {
stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.NO_DATA);
var tripUpdate = tripUpdateBuilder.build();

Result<UpdateError, TripTimesPatch> result = timetable.createUpdatedTripTimes(
Result<TripTimesPatch, UpdateError> result = timetable.createUpdatedTripTimes(
tripUpdate,
timeZone,
serviceDate,
Expand Down Expand Up @@ -105,6 +106,8 @@ public void badData() {
BackwardsDelayPropagationType.REQUIRED_NO_DATA
);
assertTrue(result.isFailure());

result.ifFailure(e -> assertEquals(UNKNOWN, e.errorType()));
}

@Test
Expand Down

0 comments on commit 16e962e

Please sign in to comment.