Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public static Builder builder() {
*/
@AutoValue.Builder
public abstract static class Builder {
//TODO change List<Double> to a custom model or to a Pair
// TODO: change List<Double> to a custom model or to a Pair
private List<List<Double>> bearings = new ArrayList<>();
private List<Point> coordinates = new ArrayList<>();
private List<String> annotations = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public abstract static class Builder {
private String[] approaches;
private Integer[] destinations;
private Integer[] sources;
private Integer coordinateListSizeLimit;

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

/**
* Override the standard maximum coordinate list size of 25 so that you can
* make a Matrix API call with a list of coordinates as large as the value you give to
* this method.
*
* You should only use this method if the Mapbox team has enabled your Mapbox
* account to be able to request Matrix API information with a list of more than 25
* coordinates.
*
* @param coordinateListSizeLimit the max limit of coordinates used by a single call
*
* @return this builder for chaining options together
* @since 5.1.0
*/
public Builder coordinateListSizeLimit(@NonNull Integer coordinateListSizeLimit) {
this.coordinateListSizeLimit = coordinateListSizeLimit;
return this;
}

abstract MapboxMatrix autoBuild();

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

coordinates(formatCoordinates(coordinates));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import static com.mapbox.api.directions.v5.DirectionsCriteria.APPROACH_CURB;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class MapboxMatrixTest extends TestUtils {
Expand Down Expand Up @@ -149,7 +151,13 @@ public void validCoordinatesTotal() throws ServicesException {
}

thrown.expect(ServicesException.class);
thrown.expectMessage(startsWith("Maximum of 25 coordinates are allowed for this API."));
thrown.expectMessage(startsWith(
"A maximum of 25 coordinates is the default " +
" allowed for this API. If your Mapbox account has been enabled by the" +
" Mapbox team to make a request with more than 25 coordinates, please use" +
" the builder's coordinateListSizeLimit() method and pass through your account" +
"-specific maximum."
));
MapboxMatrix.builder()
.accessToken(ACCESS_TOKEN)
.profile(DirectionsCriteria.PROFILE_DRIVING)
Expand Down Expand Up @@ -222,4 +230,60 @@ public void annotationsAndApproaches() throws ServicesException, IOException {
assertEquals(27192.3, response.body().distances().get(0)[2], DELTA);
assertEquals("McAllister Street", response.body().destinations().get(0).name());
}

@Test
public void destinationListSizeLimitCheckThatCallWorks() throws ServicesException, IOException {
int total = 35;
ArrayList<Point> positions = new ArrayList<>();
for (int i = 0; i < total; i++) {
positions.add(Point.fromLngLat(0.0, 0.0));
}

MapboxMatrix client = MapboxMatrix.builder()
.accessToken(ACCESS_TOKEN)
.profile(DirectionsCriteria.PROFILE_DRIVING)
.coordinateListSizeLimit(50)
.coordinates(positions)
.build();

Response<MatrixResponse> response = client.executeCall();
assertFalse(response.isSuccessful());
assertNull(response.body());
}

@Test
public void build_invalidCoordinateAndDefaultMaxExceptionThrown() throws Exception {
int total = 35;
ArrayList<Point> positions = new ArrayList<>();
for (int i = 0; i < total; i++) {
positions.add(Point.fromLngLat(0.0, 0.0));
}

thrown.expect(ServicesException.class);
thrown.expectMessage("If you're going to use the coordinateListSizeLimit() method,"
+ " please pass through a number that's equal to or greater than the size of"
+ " your coordinate list.");
MapboxMatrix.builder()
.accessToken(ACCESS_TOKEN)
.coordinateListSizeLimit(20)
.coordinates(positions)
.build();
}

@Test
public void build_invalidLessThanZeroDefaultMaxExceptionThrown() throws Exception {
int total = 20;
ArrayList<Point> positions = new ArrayList<>();
for (int i = 0; i < total; i++) {
positions.add(Point.fromLngLat(0.0, 0.0));
}
thrown.expect(ServicesException.class);
thrown.expectMessage("If you're going to use the coordinateListSizeLimit() method, " +
"please pass through a number that's greater than zero.");
MapboxMatrix.builder()
.accessToken(ACCESS_TOKEN)
.coordinates(positions)
.coordinateListSizeLimit(-3)
.build();
}
}