Skip to content

Commit

Permalink
Add validation of routeSortOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Mar 22, 2024
1 parent ccc95e6 commit aa8efd2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/opentripplanner/framework/lang/IntUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ public static int requireInRange(int value, int minInclusive, int maxInclusive)
return requireInRange(value, minInclusive, maxInclusive, null);
}

/**
* Check if the given {@code value} is null or a non-negative integer.
*
* @throws IllegalArgumentException
*/
public static Integer requireNullOrNotNegative(Integer value, String field) {
if (value == null) {
return null;
} else {
return requireNotNegative(value, field);
}
}

public static int requireNotNegative(int value, String field) {
if (value < 0) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opentripplanner.framework.i18n.I18NString;
import org.opentripplanner.framework.lang.IntUtils;
import org.opentripplanner.transit.model.basic.SubMode;
import org.opentripplanner.transit.model.basic.TransitMode;
import org.opentripplanner.transit.model.framework.AbstractTransitEntity;
Expand Down Expand Up @@ -55,7 +56,7 @@ public final class Route extends AbstractTransitEntity<Route, RouteBuilder> impl
this.branding = builder.getBranding();
this.groupsOfRoutes = listOfNullSafe(builder.getGroupsOfRoutes());
this.gtfsType = builder.getGtfsType();
this.gtfsSortOrder = builder.getGtfsSortOrder();
this.gtfsSortOrder = IntUtils.requireNullOrNotNegative(builder.getGtfsSortOrder(), "sortOrder");
this.netexSubmode = SubMode.getOrBuildAndCacheForever(builder.getNetexSubmode());
this.flexibleLineType = builder.getFlexibleLineType();
this.description = builder.getDescription();
Expand Down Expand Up @@ -148,6 +149,12 @@ public Integer getGtfsType() {
return gtfsType;
}

/**
* The visual sort priority of this route when displayed in a graphical interface.
* A lower number means that the route has a higher priority.
* <p>
* Pass-through information from GTFS. This information is not used by OTP.
*/
@Nullable
public Integer getGtfsSortOrder() {
return gtfsSortOrder;
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/opentripplanner/framework/lang/IntUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.opentripplanner.framework.lang.IntUtils.concat;
import static org.opentripplanner.framework.lang.IntUtils.intArray;
import static org.opentripplanner.framework.lang.IntUtils.intArrayToString;
import static org.opentripplanner.framework.lang.IntUtils.intToString;
import static org.opentripplanner.framework.lang.IntUtils.requireInRange;
import static org.opentripplanner.framework.lang.IntUtils.requireNotNegative;
import static org.opentripplanner.framework.lang.IntUtils.requireNullOrNotNegative;
import static org.opentripplanner.framework.lang.IntUtils.shiftArray;
import static org.opentripplanner.framework.lang.IntUtils.standardDeviation;

Expand Down Expand Up @@ -102,6 +104,14 @@ void testRequireNotNegative() {
assertEquals("Negative value not expected for value: -1", ex.getMessage());
}

@Test
void testRequireNotNegativeOrNull() {
assertNull(requireNullOrNotNegative(null, "ok"));
assertEquals(5, requireNullOrNotNegative(5, "ok"));
assertEquals(0, requireNullOrNotNegative(0, "ok"));
assertThrows(IllegalArgumentException.class, () -> requireNullOrNotNegative(-5, "ok"));
}

@Test
void testRequireInRange() {
// OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void sameAs() {
assertFalse(subject.sameAs(subject.copy().withColor("X").build()));
assertFalse(subject.sameAs(subject.copy().withTextColor("X").build()));
assertFalse(subject.sameAs(subject.copy().withGtfsType(-1).build()));
assertFalse(subject.sameAs(subject.copy().withGtfsSortOrder(-1).build()));
assertFalse(subject.sameAs(subject.copy().withGtfsSortOrder(99).build()));
assertFalse(subject.sameAs(subject.copy().withFlexibleLineType("X").build()));
assertFalse(subject.sameAs(subject.copy().withUrl("X").build()));
}
Expand Down

0 comments on commit aa8efd2

Please sign in to comment.