Skip to content

Commit 6c9bab0

Browse files
author
Langston Smith
authored
Adding flexibility to Matrix API builder to set custom max coordinate list size limit (#1133)
* adding max coordinate setting for matrix builder * message adjustment
1 parent 0628180 commit 6c9bab0

3 files changed

Lines changed: 99 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public static Builder builder() {
382382
*/
383383
@AutoValue.Builder
384384
public abstract static class Builder {
385-
//TODO change List<Double> to a custom model or to a Pair
385+
// TODO: change List<Double> to a custom model or to a Pair
386386
private List<List<Double>> bearings = new ArrayList<>();
387387
private List<Point> coordinates = new ArrayList<>();
388388
private List<String> annotations = new ArrayList<>();

services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public abstract static class Builder {
138138
private String[] approaches;
139139
private Integer[] destinations;
140140
private Integer[] sources;
141+
private Integer coordinateListSizeLimit;
141142

142143
/**
143144
* The username for the account that the directions engine runs on. In most cases, this should
@@ -293,6 +294,25 @@ public Builder sources(@Nullable Integer... sources) {
293294
*/
294295
public abstract Builder baseUrl(@NonNull String baseUrl);
295296

297+
/**
298+
* Override the standard maximum coordinate list size of 25 so that you can
299+
* make a Matrix API call with a list of coordinates as large as the value you give to
300+
* this method.
301+
*
302+
* You should only use this method if the Mapbox team has enabled your Mapbox
303+
* account to be able to request Matrix API information with a list of more than 25
304+
* coordinates.
305+
*
306+
* @param coordinateListSizeLimit the max limit of coordinates used by a single call
307+
*
308+
* @return this builder for chaining options together
309+
* @since 5.1.0
310+
*/
311+
public Builder coordinateListSizeLimit(@NonNull Integer coordinateListSizeLimit) {
312+
this.coordinateListSizeLimit = coordinateListSizeLimit;
313+
return this;
314+
}
315+
296316
abstract MapboxMatrix autoBuild();
297317

298318
/**
@@ -307,8 +327,19 @@ public MapboxMatrix build() {
307327
if (coordinates == null || coordinates.size() < 2) {
308328
throw new ServicesException("At least two coordinates must be provided with your API"
309329
+ " request.");
310-
} else if (coordinates.size() > 25) {
311-
throw new ServicesException("Maximum of 25 coordinates are allowed for this API.");
330+
} else if (coordinateListSizeLimit != null && coordinateListSizeLimit < 0) {
331+
throw new ServicesException("If you're going to use the coordinateListSizeLimit() method, "
332+
+ "please pass through a number that's greater than zero.");
333+
} else if (coordinateListSizeLimit == null && coordinates.size() > 25) {
334+
throw new ServicesException("A maximum of 25 coordinates is the default "
335+
+ " allowed for this API. If your Mapbox account has been enabled by the"
336+
+ " Mapbox team to make a request with more than 25 coordinates, please use"
337+
+ " the builder's coordinateListSizeLimit() method and pass through your account"
338+
+ "-specific maximum.");
339+
} else if (coordinateListSizeLimit != null && coordinateListSizeLimit < coordinates.size()) {
340+
throw new ServicesException("If you're going to use the coordinateListSizeLimit() method,"
341+
+ " please pass through a number that's equal to or greater than the size of"
342+
+ " your coordinate list.");
312343
}
313344

314345
coordinates(formatCoordinates(coordinates));

services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import static com.mapbox.api.directions.v5.DirectionsCriteria.APPROACH_CURB;
2828
import static org.hamcrest.core.StringStartsWith.startsWith;
2929
import static org.junit.Assert.assertEquals;
30+
import static org.junit.Assert.assertFalse;
3031
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertNull;
3133
import static org.junit.Assert.assertTrue;
3234

3335
public class MapboxMatrixTest extends TestUtils {
@@ -149,7 +151,13 @@ public void validCoordinatesTotal() throws ServicesException {
149151
}
150152

151153
thrown.expect(ServicesException.class);
152-
thrown.expectMessage(startsWith("Maximum of 25 coordinates are allowed for this API."));
154+
thrown.expectMessage(startsWith(
155+
"A maximum of 25 coordinates is the default " +
156+
" allowed for this API. If your Mapbox account has been enabled by the" +
157+
" Mapbox team to make a request with more than 25 coordinates, please use" +
158+
" the builder's coordinateListSizeLimit() method and pass through your account" +
159+
"-specific maximum."
160+
));
153161
MapboxMatrix.builder()
154162
.accessToken(ACCESS_TOKEN)
155163
.profile(DirectionsCriteria.PROFILE_DRIVING)
@@ -222,4 +230,60 @@ public void annotationsAndApproaches() throws ServicesException, IOException {
222230
assertEquals(27192.3, response.body().distances().get(0)[2], DELTA);
223231
assertEquals("McAllister Street", response.body().destinations().get(0).name());
224232
}
233+
234+
@Test
235+
public void destinationListSizeLimitCheckThatCallWorks() throws ServicesException, IOException {
236+
int total = 35;
237+
ArrayList<Point> positions = new ArrayList<>();
238+
for (int i = 0; i < total; i++) {
239+
positions.add(Point.fromLngLat(0.0, 0.0));
240+
}
241+
242+
MapboxMatrix client = MapboxMatrix.builder()
243+
.accessToken(ACCESS_TOKEN)
244+
.profile(DirectionsCriteria.PROFILE_DRIVING)
245+
.coordinateListSizeLimit(50)
246+
.coordinates(positions)
247+
.build();
248+
249+
Response<MatrixResponse> response = client.executeCall();
250+
assertFalse(response.isSuccessful());
251+
assertNull(response.body());
252+
}
253+
254+
@Test
255+
public void build_invalidCoordinateAndDefaultMaxExceptionThrown() throws Exception {
256+
int total = 35;
257+
ArrayList<Point> positions = new ArrayList<>();
258+
for (int i = 0; i < total; i++) {
259+
positions.add(Point.fromLngLat(0.0, 0.0));
260+
}
261+
262+
thrown.expect(ServicesException.class);
263+
thrown.expectMessage("If you're going to use the coordinateListSizeLimit() method,"
264+
+ " please pass through a number that's equal to or greater than the size of"
265+
+ " your coordinate list.");
266+
MapboxMatrix.builder()
267+
.accessToken(ACCESS_TOKEN)
268+
.coordinateListSizeLimit(20)
269+
.coordinates(positions)
270+
.build();
271+
}
272+
273+
@Test
274+
public void build_invalidLessThanZeroDefaultMaxExceptionThrown() throws Exception {
275+
int total = 20;
276+
ArrayList<Point> positions = new ArrayList<>();
277+
for (int i = 0; i < total; i++) {
278+
positions.add(Point.fromLngLat(0.0, 0.0));
279+
}
280+
thrown.expect(ServicesException.class);
281+
thrown.expectMessage("If you're going to use the coordinateListSizeLimit() method, " +
282+
"please pass through a number that's greater than zero.");
283+
MapboxMatrix.builder()
284+
.accessToken(ACCESS_TOKEN)
285+
.coordinates(positions)
286+
.coordinateListSizeLimit(-3)
287+
.build();
288+
}
225289
}

0 commit comments

Comments
 (0)