Skip to content

Commit 0285600

Browse files
author
Cameron Mace
authored
Adds fix for origin and destination overriding list (#420)
* adds fix for origin and destination overriding list * fixed test method name * fixed up test
1 parent 41e6f77 commit 0285600

2 files changed

Lines changed: 52 additions & 50 deletions

File tree

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public static class Builder<T extends Builder> extends MapboxBuilder {
143143
private double[][] bearings = null;
144144
private Boolean steps = null;
145145
private Boolean continueStraight = null;
146+
private Position origin = null;
147+
private Position destination = null;
146148

147149
/**
148150
* Constructor
@@ -219,16 +221,7 @@ public T setCoordinates(List<Position> coordinates) {
219221
* @since 1.0.0
220222
*/
221223
public T setOrigin(Position origin) {
222-
if (coordinates == null) {
223-
coordinates = new ArrayList<>();
224-
}
225-
226-
// The default behavior of ArrayList is to inserts the specified element at the
227-
// specified position in this list (beginning) and to shift the element currently at
228-
// that position (if any) and any subsequent elements to the right (adds one to
229-
// their indices)
230-
coordinates.add(0, origin);
231-
224+
this.origin = origin;
232225
return (T) this;
233226
}
234227

@@ -242,14 +235,7 @@ public T setOrigin(Position origin) {
242235
* @since 1.0.0
243236
*/
244237
public T setDestination(Position destination) {
245-
if (coordinates == null) {
246-
coordinates = new ArrayList<>();
247-
}
248-
249-
// The default behavior for ArrayList is to appends the specified element
250-
// to the end of this list.
251-
coordinates.add(destination);
252-
238+
this.destination = destination;
253239
return (T) this;
254240
}
255241

@@ -392,10 +378,24 @@ public String getProfile() {
392378
*/
393379
public String getCoordinates() {
394380
List<String> coordinatesFormatted = new ArrayList<>();
395-
for (Position coordinate : coordinates) {
381+
// Insert origin at beginning of list if one is provided.
382+
if (origin != null) {
383+
coordinatesFormatted.add(String.format(Locale.US, "%f,%f",
384+
origin.getLongitude(),
385+
origin.getLatitude()));
386+
}
387+
if (coordinates != null) {
388+
for (Position coordinate : coordinates) {
389+
coordinatesFormatted.add(String.format(Locale.US, "%f,%f",
390+
coordinate.getLongitude(),
391+
coordinate.getLatitude()));
392+
}
393+
}
394+
// Insert destination at end of list if one is provided.
395+
if (destination != null) {
396396
coordinatesFormatted.add(String.format(Locale.US, "%f,%f",
397-
coordinate.getLongitude(),
398-
coordinate.getLatitude()));
397+
destination.getLongitude(),
398+
destination.getLatitude()));
399399
}
400400

401401
return TextUtils.join(";", coordinatesFormatted.toArray());

mapbox/libjava-services/src/test/java/com/mapbox/services/api/directions/v5/MapboxDirectionsTest.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040

4141
public class MapboxDirectionsTest {
4242

43-
public static final String DIRECTIONS_V5_FIXTURE = "src/test/fixtures/directions_v5.json";
44-
public static final String DIRECTIONS_V5_PRECISION6_FIXTURE = "src/test/fixtures/directions_v5_precision_6.json";
45-
public static final String DIRECTIONS_TRAFFIC_FIXTURE = "src/test/fixtures/directions_v5_traffic.json";
46-
public static final String DIRECTIONS_ROTARY_FIXTURE = "src/test/fixtures/directions_v5_fixtures_rotary.json";
43+
private static final String DIRECTIONS_V5_FIXTURE = "src/test/fixtures/directions_v5.json";
44+
private static final String DIRECTIONS_V5_PRECISION6_FIXTURE = "src/test/fixtures/directions_v5_precision_6.json";
45+
private static final String DIRECTIONS_TRAFFIC_FIXTURE = "src/test/fixtures/directions_v5_traffic.json";
46+
private static final String DIRECTIONS_ROTARY_FIXTURE = "src/test/fixtures/directions_v5_fixtures_rotary.json";
4747
private static final double DELTA = 1E-10;
4848

4949
private MockWebServer server;
@@ -108,11 +108,11 @@ public void requiredAccessToken() throws ServicesException {
108108
@Test
109109
public void callFactoryNonNull() throws ServicesException, IOException {
110110
MapboxDirections client = new MapboxDirections.Builder()
111-
.setAccessToken("pk.XXX")
112-
.setCoordinates(positions)
113-
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
114-
.setBaseUrl(mockUrl.toString())
115-
.build();
111+
.setAccessToken("pk.XXX")
112+
.setCoordinates(positions)
113+
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
114+
.setBaseUrl(mockUrl.toString())
115+
.build();
116116

117117
// Setting a null call factory doesn't make the request fail
118118
// (the default OkHttp client is used)
@@ -499,7 +499,7 @@ public void testRotaryLegStepAndStepManeuver() throws ServicesException, IOExcep
499499
assertEquals(maneuver.getModifier(), "slight right");
500500
assertEquals(maneuver.getInstruction(),
501501
"Enter Dupont Circle Northwest and take the 3rd exit onto P Street Northwest");
502-
assertEquals(maneuver.getExit(), new Integer(3));
502+
assertEquals(maneuver.getExit(), Integer.valueOf(3));
503503

504504

505505
LegStep step = response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(1);
@@ -510,7 +510,7 @@ public void testRotaryLegStepAndStepManeuver() throws ServicesException, IOExcep
510510

511511
@Test
512512
public void testSetCoordinates() {
513-
ArrayList test = new ArrayList<>();
513+
List<Position> test = new ArrayList<>();
514514
test.add(Position.fromCoordinates(2.1, 2.2));
515515
test.add(Position.fromCoordinates(3.1, 3.2));
516516

@@ -531,7 +531,7 @@ public void setOriginDestination() {
531531

532532
@Test
533533
public void testSetCoordinatesMixed() {
534-
ArrayList test = new ArrayList<>();
534+
List<Position> test = new ArrayList<>();
535535
test.add(Position.fromCoordinates(2.1, 2.2));
536536
test.add(Position.fromCoordinates(3.1, 3.2));
537537

@@ -544,24 +544,9 @@ public void testSetCoordinatesMixed() {
544544
assertEquals(coordinates, "1.100000,1.200000;2.100000,2.200000;3.100000,3.200000;4.100000,4.200000");
545545
}
546546

547-
@Test
548-
public void testSetCoordinatesMixedHidden() {
549-
ArrayList test = new ArrayList<>();
550-
test.add(Position.fromCoordinates(2.1, 2.2));
551-
test.add(Position.fromCoordinates(3.1, 3.2));
552-
553-
// The order matters
554-
String coordinates = new MapboxDirections.Builder()
555-
.setOrigin(Position.fromCoordinates(1.1, 1.2))
556-
.setDestination(Position.fromCoordinates(4.1, 4.2))
557-
.setCoordinates(test)
558-
.getCoordinates();
559-
assertEquals(coordinates, "2.100000,2.200000;3.100000,3.200000");
560-
}
561-
562547
@Test
563548
public void testLocale() {
564-
ArrayList test = new ArrayList<>();
549+
List<Position> test = new ArrayList<>();
565550
test.add(Position.fromCoordinates(2.1, 2.2));
566551
test.add(Position.fromCoordinates(3.1, 3.2));
567552

@@ -572,7 +557,7 @@ public void testLocale() {
572557
.setDestination(Position.fromCoordinates(4.1, 4.2))
573558
.setCoordinates(test)
574559
.getCoordinates();
575-
assertEquals(coordinates, "2.100000,2.200000;3.100000,3.200000");
560+
assertEquals(coordinates, "1.100000,1.200000;2.100000,2.200000;3.100000,3.200000;4.100000,4.200000");
576561
}
577562

578563
@Test
@@ -586,4 +571,21 @@ public void testUserAgent() throws ServicesException, IOException {
586571
.build();
587572
assertTrue(service.executeCall().raw().request().header("User-Agent").contains("APP"));
588573
}
574+
575+
@Test
576+
public void originDestinationCoordinatesListCorrectOrder() throws ServicesException, IOException {
577+
MapboxDirections client = new MapboxDirections.Builder()
578+
.setAccessToken("pk.XXX")
579+
.setOrigin(Position.fromCoordinates(-122.4313, 37.7789))
580+
.setCoordinates(positions)
581+
.setDestination(Position.fromCoordinates(-121.8001, 37.2275))
582+
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
583+
.setBaseUrl(mockUrl.toString())
584+
.build();
585+
586+
String callUrl = client.executeCall().raw().request().url().toString();
587+
assertTrue(
588+
callUrl.contains("-122.431300,37.778900;-122.416667,37.783333;-121.900000,37.333333;-121.800100,37.227500")
589+
);
590+
}
589591
}

0 commit comments

Comments
 (0)