Skip to content

Commit 2c95ad9

Browse files
Seth BourgetSeth Bourget
andauthored
Adding closures to route leg. (#1250)
Co-authored-by: Seth Bourget <seth@cafesilencio.net>
1 parent 82988fb commit 2c95ad9

5 files changed

Lines changed: 509 additions & 2 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.mapbox.api.directions.v5.models;
2+
3+
import androidx.annotation.Nullable;
4+
5+
import com.google.auto.value.AutoValue;
6+
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
8+
import com.google.gson.TypeAdapter;
9+
import com.google.gson.annotations.SerializedName;
10+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
11+
12+
/**
13+
* An object indicating the geometry indexes defining a road closure.
14+
*/
15+
@AutoValue
16+
public abstract class Closure extends DirectionsJsonObject {
17+
18+
/**
19+
* Closure's geometry index start point.
20+
*/
21+
@Nullable
22+
@SerializedName("geometry_index_start")
23+
public abstract Integer geometryIndexStart();
24+
25+
/**
26+
* Closure's geometry index end point.
27+
*/
28+
@Nullable
29+
@SerializedName("geometry_index_end")
30+
public abstract Integer geometryIndexEnd();
31+
32+
/**
33+
* Create a new instance of this class by using the {@link Closure.Builder} class.
34+
*
35+
* @return this classes {@link Closure.Builder} for creating a new instance
36+
*/
37+
public static Closure.Builder builder() {
38+
return new AutoValue_Closure.Builder();
39+
}
40+
41+
/**
42+
* Convert the current {@link Closure} to its builder holding the currently assigned
43+
* values. This allows you to modify a single property and then rebuild the object resulting in
44+
* an updated and modified {@link Closure}.
45+
*
46+
* @return a {@link Closure.Builder} with the same values set to match the ones
47+
* defined in this {@link Closure}
48+
*/
49+
public abstract Closure.Builder toBuilder();
50+
51+
/**
52+
* Gson type adapter for parsing Gson to this class.
53+
*
54+
* @param gson the built {@link Gson} object
55+
* @return the type adapter for this class
56+
*/
57+
public static TypeAdapter<Closure> typeAdapter(Gson gson) {
58+
return new AutoValue_Closure.GsonTypeAdapter(gson);
59+
}
60+
61+
/**
62+
* Create a new instance of this class by passing in a formatted valid JSON String.
63+
*
64+
* @param json a formatted valid JSON string defining an Incident
65+
* @return a new instance of this class defined by the values passed in the method
66+
*/
67+
public static Closure fromJson(String json) {
68+
GsonBuilder gson = new GsonBuilder();
69+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
70+
return gson.create().fromJson(json, Closure.class);
71+
}
72+
73+
/**
74+
* This builder can be used to set the values describing the {@link Closure}.
75+
*/
76+
@AutoValue.Builder
77+
public abstract static class Builder {
78+
79+
/**
80+
* Closure's geometry index start point.
81+
*
82+
* @param geometryIndexStart start index
83+
*/
84+
public abstract Builder geometryIndexStart(@Nullable Integer geometryIndexStart);
85+
86+
/**
87+
* Closure's geometry index end point.
88+
*
89+
* @param geometryIndexEnd end index
90+
*/
91+
public abstract Builder geometryIndexEnd(@Nullable Integer geometryIndexEnd);
92+
93+
/**
94+
* Build a new {@link Closure} object.
95+
*
96+
* @return a new {@link Closure} using the provided values in this builder
97+
*/
98+
public abstract Closure build();
99+
}
100+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ public static Builder builder() {
104104
@Nullable
105105
public abstract LegAnnotation annotation();
106106

107+
/**
108+
* A list of closures that occur on this leg.
109+
*
110+
* @return a list of {@link Incident}
111+
*/
112+
@Nullable
113+
public abstract List<Closure> closures();
114+
107115
/**
108116
* Convert the current {@link RouteLeg} to its builder holding the currently assigned
109117
* values. This allows you to modify a single property and then rebuild the object resulting in
@@ -226,6 +234,14 @@ public abstract static class Builder {
226234
*/
227235
public abstract Builder annotation(@Nullable LegAnnotation annotation);
228236

237+
/**
238+
* A list of closures that occur on this leg.
239+
*
240+
* @param closures a list of {@link Closure}
241+
* @return this builder for chaining options together
242+
*/
243+
public abstract Builder closures(@Nullable List<Closure> closures);
244+
229245
/**
230246
* Build a new {@link RouteLeg} object.
231247
*
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.mapbox.api.directions.v5.models;
2+
3+
import com.mapbox.core.TestUtils;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNotNull;
8+
9+
public class ClosureTest extends TestUtils {
10+
11+
@Test
12+
public void sanity(){
13+
assertNotNull(getDefault());
14+
}
15+
16+
private Closure getDefault() {
17+
return Closure.builder()
18+
.geometryIndexStart(3)
19+
.geometryIndexEnd(5)
20+
.build();
21+
}
22+
23+
@Test
24+
public void testSerializableObject() throws Exception {
25+
Closure closure = Closure.builder()
26+
.geometryIndexStart(3)
27+
.geometryIndexEnd(5)
28+
.build();
29+
byte[] serialized = TestUtils.serialize(closure);
30+
assertEquals(closure, deserialize(serialized, Closure.class));
31+
}
32+
33+
@Test
34+
public void testSerializableFromJson(){
35+
String json = "{\"geometry_index_start\":3,\"geometry_index_end\":5}";
36+
37+
Closure fromJson = Closure.fromJson(json);
38+
39+
assertNotNull(fromJson);
40+
assertEquals(3L, fromJson.geometryIndexStart().longValue());
41+
assertEquals(5L, fromJson.geometryIndexEnd().longValue());
42+
}
43+
44+
@Test
45+
public void testFullRoute() throws Exception {
46+
String json = loadJsonFixture("directions_v5-with-closure_precision_6.json");
47+
DirectionsRoute response = DirectionsRoute.fromJson(json);
48+
49+
assertEquals(13L, response.legs().get(0).closures().get(0).geometryIndexStart().longValue());
50+
assertEquals(21L, response.legs().get(0).closures().get(0).geometryIndexEnd().longValue());
51+
}
52+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public void testToFromJson1() {
7575

7676
List<Incident> incidents = new ArrayList<>();
7777

78+
List<Closure> closures = new ArrayList<>();
79+
7880
LegAnnotation annotation = LegAnnotation.builder()
7981
.congestion(new ArrayList<String>())
8082
.distance(distanceList)
@@ -89,6 +91,7 @@ public void testToFromJson1() {
8991
.duration(14.3)
9092
.steps(steps)
9193
.incidents(incidents)
94+
.closures(closures)
9295
.summary("")
9396
.build();
9497

@@ -105,20 +108,24 @@ public void testFromJson() {
105108
+ "\"duration\": 14.3,"
106109
+ "\"summary\": \"route summary\","
107110
+ "\"admins\": [{ \"iso_3166_1_alpha3\": \"USA\", \"iso_3166_1\": \"US\" }],"
108-
+ "\"incidents\": [{ \"id\": \"15985415522454461962\" }]"
109-
+ "}";
111+
+ "\"incidents\": [{ \"id\": \"15985415522454461962\" }],"
112+
+ "\"closures\": [{ \"geometry_index_start\": 1,\"geometry_index_end\": 4}]}";
110113
List<Admin> admins = new ArrayList<>();
111114
admins.add(Admin.builder().countryCode("US").countryCodeAlpha3("USA").build());
112115

113116
List<Incident> incidents = new ArrayList<>();
114117
incidents.add(Incident.builder().id("15985415522454461962").build());
115118

119+
List<Closure> closures = new ArrayList<>();
120+
closures.add(Closure.builder().geometryIndexStart(1).geometryIndexEnd(4).build());
121+
116122
RouteLeg routeLeg = RouteLeg.builder()
117123
.distance(53.4)
118124
.duration(14.3)
119125
.summary("route summary")
120126
.admins(admins)
121127
.incidents(incidents)
128+
.closures(closures)
122129
.build();
123130

124131
RouteLeg routeLegFromJson = RouteLeg.fromJson(routeLegJsonString);

0 commit comments

Comments
 (0)