Skip to content

Commit e761e5b

Browse files
author
Łukasz Paczos
committed
removed request parameters verification in favor of letting the service respond with a correct error
1 parent 7df0330 commit e761e5b

7 files changed

Lines changed: 28 additions & 748 deletions

File tree

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/RouteOptions.java

Lines changed: 9 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
public abstract class RouteOptions extends DirectionsJsonObject {
3131

3232
/**
33-
* Build a new instance of this RouteOptions.
34-
*
35-
* @return {@link RouteOptions.Builder}
33+
* Build a new instance of {@link RouteOptions} and sets default values for:
34+
* <ul>
35+
* <li>{@link #baseUrl()} equal to {@link DirectionsCriteria#BASE_API_URL}.</li>
36+
* <li>{@link #user()} equal to {@link DirectionsCriteria#PROFILE_DEFAULT_USER}.</li>
37+
* <li>{@link #geometries()} equal to {@link DirectionsCriteria#GEOMETRY_POLYLINE6}.</li>
38+
* </ul>
3639
*/
3740
public static Builder builder() {
3841
return new AutoValue_RouteOptions.Builder()
@@ -1140,7 +1143,7 @@ public Builder annotationsList(@Nullable List<String> annotations) {
11401143
*/
11411144
@NonNull
11421145
public Builder approachesList(@Nullable List<String> approaches) {
1143-
String result = FormatUtils.formatApproaches(approaches);
1146+
String result = FormatUtils.join(";", approaches);
11441147
if (result != null) {
11451148
approaches(result);
11461149
}
@@ -1416,130 +1419,11 @@ public Builder snappingIncludeClosuresList(@Nullable List<Boolean> snappingClosu
14161419
public abstract Builder enableRefresh(@Nullable Boolean enableRefresh);
14171420

14181421
/**
1419-
* Package private used to build the object and verify.
1420-
*/
1421-
@NonNull
1422-
abstract RouteOptions autoBuild();
1423-
1424-
/**
1425-
* This uses the provided parameters set using the {@link Builder} and first checks that all
1426-
* values are valid, and creates a new {@link RouteOptions} object with the values provided.
1422+
* Builds the object.
14271423
*
14281424
* @return a new instance of {@link RouteOptions}
14291425
*/
14301426
@NonNull
1431-
public RouteOptions build() {
1432-
RouteOptions routeOptions = autoBuild();
1433-
1434-
List<Point> coordinates = routeOptions.coordinatesList();
1435-
if (coordinates.size() < 2) {
1436-
throw new RuntimeException(
1437-
"An origin and destination are required before making the directions API request."
1438-
);
1439-
}
1440-
1441-
List<Integer> waypointIndices = routeOptions.waypointIndicesList();
1442-
if (waypointIndices != null && !waypointIndices.isEmpty()) {
1443-
if (waypointIndices.size() < 2) {
1444-
throw new RuntimeException(
1445-
"Waypoints indices must be a list of at least two indexes, origin and destination."
1446-
);
1447-
}
1448-
if (waypointIndices.get(0) != 0 || waypointIndices.get(waypointIndices.size() - 1)
1449-
!= coordinates.size() - 1) {
1450-
throw new RuntimeException(
1451-
"First and last waypoints indices must match the origin and final destination."
1452-
);
1453-
}
1454-
for (int i = 1; i < waypointIndices.size() - 1; i++) {
1455-
if (waypointIndices.get(i) < 0
1456-
|| waypointIndices.get(i) >= coordinates.size()) {
1457-
throw new RuntimeException(
1458-
"Waypoints index out of bounds (no corresponding coordinate).");
1459-
}
1460-
}
1461-
}
1462-
1463-
checkSizeMatchingWaypoints(
1464-
"waypointTargets",
1465-
true,
1466-
routeOptions.waypointTargetsList(),
1467-
coordinates,
1468-
routeOptions.waypointIndicesList()
1469-
);
1470-
1471-
checkSizeMatchingWaypoints(
1472-
"approaches",
1473-
false,
1474-
routeOptions.approachesList(),
1475-
coordinates,
1476-
routeOptions.waypointIndicesList()
1477-
);
1478-
1479-
checkSizeMatchingWaypoints(
1480-
"snappingIncludeClosures",
1481-
false,
1482-
routeOptions.snappingIncludeClosuresList(),
1483-
coordinates,
1484-
routeOptions.waypointIndicesList()
1485-
);
1486-
1487-
checkSizeMatchingWaypoints(
1488-
"bearings",
1489-
false,
1490-
routeOptions.bearingsList(),
1491-
coordinates,
1492-
routeOptions.waypointIndicesList()
1493-
);
1494-
1495-
checkSizeMatchingWaypoints(
1496-
"radiuses",
1497-
false,
1498-
routeOptions.radiusesList(),
1499-
coordinates,
1500-
routeOptions.waypointIndicesList()
1501-
);
1502-
1503-
checkSizeMatchingWaypoints(
1504-
"waypointNames",
1505-
true,
1506-
routeOptions.waypointNamesList(),
1507-
coordinates,
1508-
routeOptions.waypointIndicesList()
1509-
);
1510-
1511-
return routeOptions;
1512-
}
1513-
1514-
private void checkSizeMatchingWaypoints(
1515-
String paramName,
1516-
boolean waypointIndicesDependent,
1517-
List<?> testedParam,
1518-
List<Point> coordinates,
1519-
List<Integer> waypointIndices
1520-
) {
1521-
if (testedParam != null && !testedParam.isEmpty()) {
1522-
boolean error = false;
1523-
if (waypointIndicesDependent && waypointIndices != null && !waypointIndices.isEmpty()) {
1524-
if (testedParam.size() != waypointIndices.size()) {
1525-
error = true;
1526-
}
1527-
} else if (testedParam.size() != coordinates.size()) {
1528-
error = true;
1529-
}
1530-
if (error) {
1531-
StringBuilder builder = new StringBuilder();
1532-
builder.append("Number of ");
1533-
builder.append(paramName);
1534-
builder.append(" must match the number of coordinates");
1535-
if (waypointIndicesDependent) {
1536-
builder.append(" or waypoints indices (if present)");
1537-
}
1538-
builder.append(".");
1539-
1540-
throw new RuntimeException(builder.toString());
1541-
}
1542-
}
1543-
}
1427+
public abstract RouteOptions build();
15441428
}
15451429
}

services-directions-models/src/main/java/com/mapbox/api/directions/v5/utils/FormatUtils.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ public static String formatRadiuses(@Nullable List<Double> radiuses) {
8383
for (Double radius : radiuses) {
8484
if (radius == null) {
8585
radiusesToJoin.add(null);
86-
} else if (radius <= 0) {
87-
throw new RuntimeException(
88-
"Radiuses need to be greater than 0 or Double.POSITIVE_INFINITY."
89-
);
9086
} else if (radius == Double.POSITIVE_INFINITY) {
9187
radiusesToJoin.add("unlimited");
9288
} else {
@@ -116,14 +112,6 @@ public static String formatBearings(@Nullable List<Bearing> bearings) {
116112
double angle = bearing.angle();
117113
double tolerance = bearing.degrees();
118114

119-
if (angle < 0 || angle > 360) {
120-
throw new RuntimeException("Angle has to be from 0 to 360.");
121-
}
122-
123-
if (tolerance < 0 || tolerance > 180) {
124-
throw new RuntimeException("Degrees has to be from 0 to 180.");
125-
}
126-
127115
bearingsToJoin.add(String.format(Locale.US, "%s,%s",
128116
formatDouble(angle),
129117
formatDouble(tolerance)));
@@ -157,29 +145,6 @@ public static String formatDistributions(@Nullable List<Integer[]> distributions
157145
return join(";", distributionsToJoin);
158146
}
159147

160-
/**
161-
* Converts String list with approaches values to a string ready for API consumption. An approach
162-
* could be unrestricted, curb or null.
163-
*
164-
* @param approaches a list representing approaches to each coordinate.
165-
* @return a formatted string.
166-
*/
167-
@Nullable
168-
public static String formatApproaches(@Nullable List<String> approaches) {
169-
if (approaches == null) {
170-
return null;
171-
}
172-
173-
for (String approach : approaches) {
174-
if (approach != null && !approach.equals("unrestricted") && !approach.equals("curb")) {
175-
throw new RuntimeException(
176-
"Approach should be one of unrestricted or curb"
177-
);
178-
}
179-
}
180-
return join(";", approaches);
181-
}
182-
183148
/**
184149
* Converts list of points to a string ready for API consumption.
185150
*

0 commit comments

Comments
 (0)