Skip to content

Commit

Permalink
Fixed bug that returns null results when sources size are greater tha…
Browse files Browse the repository at this point in the history
…n 59, and destinations are lower than 59, AND the result need multiple requests to be built. Added verification function after result is built
  • Loading branch information
Marcoshsc committed Jun 18, 2020
1 parent 5bc63e7 commit 5eacbcb
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 148 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.marcoshsc</groupId>
<artifactId>orsApiTools</artifactId>
<version>1.0</version>
<version>1.1</version>
<name>ORSApiTools</name>
<description>Java library that provides implementation of Open Route Service endpoints.</description>
<url>https://github.com/Marcoshsc/ORSApiTools</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public static void concatNewLine(MatrixResponse target, MatrixResponse toConcat)
*/
public static void concatNewColumns(MatrixResponse target, MatrixResponse toConcat) {
if(toConcat == null || target == null)
return;
throw new IllegalArgumentException(String.format("Target: %s, Matrix to concat: %s, one of them are null.",
target, toConcat));
int distancesInitialSize = -1;
int durationsInitialSize = -1;
if(toConcat.distances != null && target.distances != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.marcoshsc.orsApiTools.matrix;

import com.github.marcoshsc.orsApiTools.directions.enums.EnumMetrics;
import com.github.marcoshsc.orsApiTools.general.ORSJSONProcessor;
import com.github.marcoshsc.orsApiTools.general.enums.ORSEnum;
import com.github.marcoshsc.orsApiTools.general.exceptions.InvalidParameters;
Expand Down Expand Up @@ -80,13 +81,37 @@ private MatrixResponse getConfiguratedResponse() throws RequestException, Invali
List<Integer> destinations = parameters.getDestinations() != null ? new ArrayList<>(parameters.getDestinations().getTypedValue())
: UtilityFunctions.getIntegerList(0, locations.size());
MatrixResponse res = handleMultipleCoordinates();
boolean hasDistances = parameters.getMetrics().getTypedValue().contains(EnumMetrics.DISTANCE),
hasDurations = parameters.getMetrics().getTypedValue().contains(EnumMetrics.DURATION);
if(hasDistances)
verifyMatrix(res.getDistances(), sources, destinations);
if(hasDurations)
verifyMatrix(res.getDurations(), sources, destinations);
MatrixRequestOptions options = res.getOptions();
options.setLocations(locations);
options.setSources(sources);
options.setDestinations(destinations);
return res;
}

private void verifyMatrix(List<List<Double>> matrix, List<Integer> sources, List<Integer> destinations) throws RequestException {
int matrixRows = matrix.size();
int rows = sources.size();
int columns = destinations.size();
if(rows != matrixRows) {
throw new RequestException(String.format("ORS Api error. The size of the matrix does not match with " +
"the size of the given parameters. Matrix rows: %d, Source rows: %d.", matrixRows, rows));
}
for (List<Double> list : matrix) {
int matrixColumns = list.size();
if(columns != matrixColumns) {
throw new RequestException(String.format("ORS Api error. The size of the matrix does not match " +
"with the size of the given parameters. Matrix columns: %d, Destination columns: %d.",
matrixColumns, columns));
}
}
}

/**
* Makes possible to handle more than 3500 positions in the final matrix. It looks at the location coordinates, if
* its size is lower than 60, make a single request and return the response. Otherwise, it will call a separated
Expand Down Expand Up @@ -174,6 +199,12 @@ private MatrixResponse handleWithSourceAndDestination() throws RequestException,
private MatrixResponse getResponseFromLoop(List<Coordinate> destinations, MatrixResponse finalResponse,
int destinationFinalLength, List<Coordinate> outerList)
throws JSONException, InvalidParameters, UnsupportedEncodingException, RequestException {
if(destinationFinalLength == destinations.size()) {
configureParameters(outerList, destinations);
if(finalResponse == null) return makeSimpleRequest();
MatrixResponse.concatNewLine(finalResponse, makeSimpleRequest());
return finalResponse;
}
for (int j = 0; j < destinations.size() - destinationFinalLength; j += 59) {
List<Coordinate> innerList = destinations.subList(j, j + 59);
configureParameters(outerList, innerList);
Expand Down
Loading

0 comments on commit 5eacbcb

Please sign in to comment.