Skip to content

Commit 9830e09

Browse files
author
Siarhei Fiedartsou
authored
Add support of layer to RouteOptions (#1279)
1 parent 4e8941e commit 9830e09

8 files changed

Lines changed: 123 additions & 6 deletions

File tree

config/checkstyle/checkstyle.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
at least two characters long.
8686
-->
8787
<module name="MemberName">
88-
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
88+
<property name="format" value="^[a-z][a-z0-9]*[a-zA-Z0-9]*$"/>
8989
<property name="applyToPublic" value="false"/>
9090
<message key="name.invalidPattern"
9191
value="Member name ''{0}'' must start off lowercase and be at least two characters long."/>
@@ -96,7 +96,7 @@
9696
lower case letter.
9797
-->
9898
<module name="MethodName">
99-
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9_]*$"/>
99+
<property name="format" value="^[a-z][a-z0-9]*[a-zA-Z0-9_]*$"/>
100100
<message key="name.invalidPattern"
101101
value="Method name ''{0}'' must be at least two characters long."/>
102102
</module>
@@ -123,7 +123,7 @@
123123

124124
<!-- Require parameter names to be at least two characters long -->
125125
<module name="ParameterName">
126-
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
126+
<property name="format" value="^[a-z][a-z0-9]*[a-zA-Z0-9]*$"/>
127127
<message key="name.invalidPattern"
128128
value="Parameter name ''{0}'' must be at least two characters long."/>
129129
</module>
@@ -134,7 +134,7 @@
134134
-->
135135
<module name="LocalVariableName">
136136
<property name="tokens" value="VARIABLE_DEF"/>
137-
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
137+
<property name="format" value="^[a-z][a-z0-9]*[a-zA-Z0-9]*$"/>
138138
<property name="allowOneCharVarInForLoop" value="true"/>
139139
<message key="name.invalidPattern"
140140
value="Local variable name ''{0}'' must be at least two characters long."/>
@@ -173,7 +173,7 @@
173173
the format property.
174174
-->
175175
<module name="CatchParameterName">
176-
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
176+
<property name="format" value="^[a-z][a-z0-9]*[a-zA-Z0-9]*$"/>
177177
<message key="name.invalidPattern"
178178
value="Catch parameter name ''{0}'' must match pattern ''{1}''."/>
179179
</module>

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,31 @@ public List<Bearing> bearingsList() {
198198
return ParseUtils.parseBearings(bearings());
199199
}
200200

201+
/**
202+
* Influences layer of road from where route starts from a waypoint. Useful in ambiguous
203+
* cases when there are multiple roads at the same point and only layer allows
204+
* to differentiate them.
205+
* <p>
206+
* If provided, the list of layers must be the same length as the list of coordinates.
207+
* @return a string representing the layers with the ; separator. Each value may be negative
208+
or absent.
209+
*/
210+
@Nullable
211+
public abstract String layers();
212+
213+
/**
214+
* Influences layer of road from where route starts from a waypoint. Useful in ambiguous
215+
* cases when there are multiple roads at the same point and only layer allows
216+
* to differentiate them.
217+
* <p>
218+
* If provided, the list of layers must be the same length as the list of coordinates.
219+
* @return a List of values representing layers. Each value may be negative or null.
220+
*/
221+
@Nullable
222+
public List<Integer> layersList() {
223+
return ParseUtils.parseToIntegers(layers());
224+
}
225+
201226
/**
202227
* The allowed direction of travel when departing intermediate waypoints. If true, the route
203228
* will continue in the same direction of travel. If false, the route may continue in the opposite
@@ -958,6 +983,36 @@ public Builder bearingsList(@Nullable List<Bearing> bearings) {
958983
return this;
959984
}
960985

986+
/**
987+
* Influences layer of road from where route starts from a waypoint. Useful in ambiguous
988+
* cases when there are multiple roads at the same point and only layer allows
989+
* to differentiate them.
990+
* <p>
991+
* If provided, the list of layers must be the same length as the list of coordinates.
992+
* @param layers a string representing the layers with the ; separator.
993+
* @return this builder for chaining options together
994+
*/
995+
@NonNull
996+
public abstract Builder layers(@Nullable String layers);
997+
998+
/**
999+
* Influences layer of road from where route starts from a waypoint. Useful in ambiguous
1000+
* cases when there are multiple roads at the same point and only layer allows
1001+
* to differentiate them.
1002+
* <p>
1003+
* If provided, the list of layers must be the same length as the list of coordinates.
1004+
* @param layers a list of layers. For unknown layer use `null`.
1005+
* @return this builder for chaining options together
1006+
*/
1007+
@NonNull
1008+
public Builder layersList(@Nullable List<Integer> layers) {
1009+
String result = FormatUtils.formatIntegers(layers);
1010+
if (result != null) {
1011+
layers(result);
1012+
}
1013+
return this;
1014+
}
1015+
9611016
/**
9621017
* The allowed direction of travel when departing intermediate waypoints. If true, the route
9631018
* will continue in the same direction of travel. If false, the route may continue in

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,27 @@ public static String formatPointsList(@Nullable List<Point> points) {
169169
}
170170
return join(";", coordinatesToJoin);
171171
}
172+
173+
/**
174+
* Converts list of integers to a string ready for API consumption.
175+
*
176+
* @param integers a list of integers
177+
* @return a formatted string with semicolon separated integers
178+
*/
179+
@Nullable
180+
public static String formatIntegers(@Nullable List<Integer> integers) {
181+
if (integers == null) {
182+
return null;
183+
}
184+
185+
List<String> toJoin = new ArrayList<>();
186+
for (Integer integer : integers) {
187+
if (integer == null) {
188+
toJoin.add(null);
189+
} else {
190+
toJoin.add(Integer.toString(integer));
191+
}
192+
}
193+
return join(";", toJoin);
194+
}
172195
}

services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/RouteOptionsTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import com.mapbox.geojson.Point;
99
import java.io.IOException;
1010
import java.util.ArrayList;
11+
import java.util.Collections;
1112
import java.util.List;
1213
import org.junit.Test;
1314

1415
public class RouteOptionsTest extends TestUtils {
15-
1616
/**
1717
* Always update this file when new option is introduced.
1818
*/
@@ -104,6 +104,31 @@ public void bearingsListIsValid_fromJson() {
104104
assertNull(routeOptions.bearingsList().get(2));
105105
}
106106

107+
@Test
108+
public void layersAreValid_fromJson() {
109+
RouteOptions routeOptions = RouteOptions.fromJson(optionsJson);
110+
111+
assertEquals("-42;;0", routeOptions.layers());
112+
}
113+
114+
115+
@Test
116+
public void layersListIsValid_fromJson() {
117+
RouteOptions routeOptions = RouteOptions.fromJson(optionsJson);
118+
119+
List<Integer> expected = new ArrayList<Integer>();
120+
Collections.addAll(expected, -42, null, 0);
121+
122+
assertEquals(routeOptions.layersList().size(), expected.size());
123+
for (int i = 0; i < expected.size(); ++i) {
124+
if (expected.get(i) != null) {
125+
assertEquals(expected.get(i), routeOptions.layersList().get(i));
126+
} else {
127+
assertNull(routeOptions.layersList().get(i));
128+
}
129+
}
130+
}
131+
107132
@Test
108133
public void continueStraightIsValid_fromJson() {
109134
RouteOptions routeOptions = RouteOptions.fromJson(optionsJson);
@@ -302,6 +327,7 @@ private RouteOptions routeOptions() {
302327
.alternatives(false)
303328
.annotations("congestion,distance,duration")
304329
.bearings("0,90;90,0;")
330+
.layers("-42;;0")
305331
.continueStraight(false)
306332
.exclude(DirectionsCriteria.EXCLUDE_TOLL)
307333
.geometries(DirectionsCriteria.GEOMETRY_POLYLINE6)
@@ -355,6 +381,11 @@ private RouteOptions routeOptionsList() {
355381
add(Bearing.builder().angle(90.0).degrees(0.0).build());
356382
add(null);
357383
}})
384+
.layersList(new ArrayList<Integer>() {{
385+
add(-42);
386+
add(null);
387+
add(0);
388+
}})
358389
.continueStraight(false)
359390
.exclude(DirectionsCriteria.EXCLUDE_TOLL)
360391
.geometries(DirectionsCriteria.GEOMETRY_POLYLINE6)

services-directions-models/src/test/resources/route_options_v5.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"language": "ru",
88
"radiuses": ";unlimited;5.1",
99
"bearings": "0,90;90,0;",
10+
"layers": "-42;;0",
1011
"continue_straight": false,
1112
"roundabout_exits": false,
1213
"geometries": "polyline6",

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface DirectionsService {
3131
* @param radiuses {@link RouteOptions#radiuses()}
3232
* @param steps {@link RouteOptions#steps()}
3333
* @param bearings {@link RouteOptions#bearings()}
34+
* @param layers {@link RouteOptions#layers()}
3435
* @param continueStraight {@link RouteOptions#continueStraight()}
3536
* @param annotations {@link RouteOptions#annotations()}
3637
* @param language {@link RouteOptions#language()}
@@ -68,6 +69,7 @@ Call<DirectionsResponse> getCall(
6869
@Query("radiuses") String radiuses,
6970
@Query("steps") Boolean steps,
7071
@Query("bearings") String bearings,
72+
@Query("layers") String layers,
7173
@Query("continue_straight") Boolean continueStraight,
7274
@Query("annotations") String annotations,
7375
@Query("language") String language,
@@ -106,6 +108,7 @@ Call<DirectionsResponse> getCall(
106108
* @param radiuses {@link RouteOptions#radiuses()}
107109
* @param steps {@link RouteOptions#steps()}
108110
* @param bearings {@link RouteOptions#bearings()}
111+
* @param layers {@link RouteOptions#layers()}
109112
* @param continueStraight {@link RouteOptions#continueStraight()}
110113
* @param annotations {@link RouteOptions#annotations()}
111114
* @param language {@link RouteOptions#language()}
@@ -144,6 +147,7 @@ Call<DirectionsResponse> postCall(
144147
@Field("radiuses") String radiuses,
145148
@Field("steps") Boolean steps,
146149
@Field("bearings") String bearings,
150+
@Field("layers") String layers,
147151
@Field("continue_straight") Boolean continueStraight,
148152
@Field("annotations") String annotations,
149153
@Field("language") String language,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ private Call<DirectionsResponse> get() {
8686
routeOptions().radiuses(),
8787
routeOptions().steps(),
8888
routeOptions().bearings(),
89+
routeOptions().layers(),
8990
routeOptions().continueStraight(),
9091
routeOptions().annotations(),
9192
routeOptions().language(),
@@ -124,6 +125,7 @@ private Call<DirectionsResponse> post() {
124125
routeOptions().radiuses(),
125126
routeOptions().steps(),
126127
routeOptions().bearings(),
128+
routeOptions().layers(),
127129
routeOptions().continueStraight(),
128130
routeOptions().annotations(),
129131
routeOptions().language(),

services-directions/src/test/resources/route_options_v5.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"language": "ru",
88
"radiuses": ";unlimited;5.1",
99
"bearings": "0,90;90,0;",
10+
"layers": "-42;;0",
1011
"continue_straight": false,
1112
"roundabout_exits": false,
1213
"geometries": "polyline6",

0 commit comments

Comments
 (0)