Skip to content

Commit ddc7d5e

Browse files
authored
NAVAND-1440: add Incident#trafficCodes (#1558)
1 parent c1d4896 commit ddc7d5e

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Mapbox welcomes participation and contributions from everyone.
44

55
### main
6+
- Added `Incident#trafficCodes`.
67

78
### v6.13.0-beta.1 - July 11, 2023
89

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ public abstract class Incident extends DirectionsJsonObject {
213213
@SerializedName("alertc_codes")
214214
public abstract List<Integer> alertcCodes();
215215

216+
/**
217+
* Traffic codes. See {@link TrafficCodes}.
218+
*
219+
* @return traffic codes
220+
*/
221+
@Nullable
222+
@SerializedName("traffic_codes")
223+
public abstract TrafficCodes trafficCodes();
224+
216225
/**
217226
* Incident's leg-wise geometry index start point.
218227
*/
@@ -418,6 +427,14 @@ public abstract static class Builder extends DirectionsJsonObject.Builder<Builde
418427
@NonNull
419428
public abstract Builder alertcCodes(@Nullable List<Integer> alertcCodes);
420429

430+
/**
431+
* Traffic codes.
432+
*
433+
* @param trafficCodes traffic cods. See {@link TrafficCodes}.
434+
*/
435+
@NonNull
436+
public abstract Builder trafficCodes(@Nullable TrafficCodes trafficCodes);
437+
421438
/**
422439
* Incident's leg-wise geometry index start point.
423440
*
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.mapbox.api.directions.v5.models;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
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+
* Holds information about traffic codes. See {@link Incident#trafficCodes()}.
14+
*/
15+
@AutoValue
16+
public abstract class TrafficCodes extends DirectionsJsonObject {
17+
18+
/**
19+
* Jartic cause code value.
20+
*
21+
* @return jartic code cause code value
22+
*/
23+
@Nullable
24+
@SerializedName("jartic_cause_code")
25+
public abstract Integer jarticCauseCode();
26+
27+
/**
28+
* Jartic regulation code value.
29+
*
30+
* @return jartic regulation regulation code value
31+
*/
32+
@Nullable
33+
@SerializedName("jartic_regulation_code")
34+
public abstract Integer jarticRegulationCode();
35+
36+
/**
37+
* Create a new instance of this class by using the {@link Builder} class.
38+
*
39+
* @return this class's {@link Builder} for creating a new instance
40+
*/
41+
public static Builder builder() {
42+
return new AutoValue_TrafficCodes.Builder();
43+
}
44+
45+
/**
46+
* Convert the current {@link TrafficCodes} to its builder holding the currently assigned
47+
* values. This allows you to modify a single property and then rebuild the object resulting in
48+
* an updated and modified {@link TrafficCodes}.
49+
*
50+
* @return a {@link Builder} with the same values set to match the ones defined in this
51+
* {@link TrafficCodes}
52+
*/
53+
public abstract Builder toBuilder();
54+
55+
/**
56+
* Gson type adapter for parsing Gson to this class.
57+
*
58+
* @param gson the built {@link Gson} object
59+
* @return the type adapter for this class
60+
*/
61+
public static TypeAdapter<TrafficCodes> typeAdapter(Gson gson) {
62+
return new AutoValue_TrafficCodes.GsonTypeAdapter(gson);
63+
}
64+
65+
/**
66+
* Create a new instance of this class by passing in a formatted valid JSON String.
67+
*
68+
* @param json a formatted valid JSON string defining a TrafficCodes
69+
* @return a new instance of this class defined by the values passed in the method
70+
*/
71+
public static TrafficCodes fromJson(String json) {
72+
GsonBuilder gson = new GsonBuilder();
73+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
74+
return gson.create().fromJson(json, TrafficCodes.class);
75+
}
76+
77+
/**
78+
* This builder can be used to set the values describing the {@link TrafficCodes}.
79+
*/
80+
@AutoValue.Builder
81+
public abstract static class Builder extends DirectionsJsonObject.Builder<Builder> {
82+
83+
/**
84+
* Sets jartic cause code value.
85+
*
86+
* @param value jartic cause code value
87+
*/
88+
@NonNull
89+
public abstract Builder jarticCauseCode(@Nullable Integer value);
90+
91+
/**
92+
* Sets jartic regulation code value.
93+
*
94+
* @param value jartic regulation code value
95+
*/
96+
@NonNull
97+
public abstract Builder jarticRegulationCode(@Nullable Integer value);
98+
99+
/**
100+
* Build a new instance of {@link TrafficCodes}.
101+
*
102+
* @return a new instance of {@link TrafficCodes}.
103+
*/
104+
@NonNull
105+
public abstract TrafficCodes build();
106+
}
107+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.mapbox.api.directions.v5.models;
22

33
import com.google.common.collect.Lists;
4+
import com.google.gson.JsonPrimitive;
45
import com.mapbox.core.TestUtils;
56
import org.junit.Test;
67

78
import java.util.ArrayList;
9+
import java.util.Collections;
810

911
import static org.junit.Assert.*;
1012

@@ -51,6 +53,11 @@ public void deserializeFromJson(){
5153
"501," +
5254
"803" +
5355
"]," +
56+
"\"traffic_codes\": {" +
57+
"\"jartic_cause_code\": 400," +
58+
"\"jartic_regulation_code\": 600," +
59+
"\"unknown_code\": 700" +
60+
"}," +
5461
"\"lanes_blocked\": []," +
5562
"\"num_lanes_blocked\": null," +
5663
"\"iso_3166_1_alpha2\": US," +
@@ -75,6 +82,9 @@ public void deserializeFromJson(){
7582
assertEquals(fromJson.subType(), "CONSTRUCTION");
7683
assertEquals(fromJson.subTypeDescription(), "construction");
7784
assertEquals(fromJson.alertcCodes().size(), 2);
85+
assertEquals(400, (int) fromJson.trafficCodes().jarticCauseCode());
86+
assertEquals(600, (int) fromJson.trafficCodes().jarticRegulationCode());
87+
assertEquals(700, fromJson.trafficCodes().getUnrecognizedJsonProperties().get("unknown_code").getAsInt());
7888
assertEquals(fromJson.geometryIndexStart().longValue(), 805L);
7989
assertEquals(fromJson.geometryIndexEnd().longValue(), 896L);
8090
assertEquals(fromJson.countryCodeAlpha2(), "US");
@@ -94,6 +104,15 @@ private Incident getFilledIncident() {
94104
return Incident.builder()
95105
.id("some_id")
96106
.alertcCodes(Lists.newArrayList(431, 2123, 934))
107+
.trafficCodes(
108+
TrafficCodes.builder()
109+
.jarticCauseCode(400)
110+
.jarticRegulationCode(600)
111+
.unrecognizedJsonProperties(
112+
Collections.singletonMap("unknown_code", new JsonPrimitive((700)))
113+
)
114+
.build()
115+
)
97116
.closed(true)
98117
.congestion(Congestion.builder().value(10).build())
99118
.creationTime("2020-11-18T11:34:14Z")
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.mapbox.api.directions.v5.models;
2+
3+
import com.google.gson.JsonPrimitive;
4+
import com.mapbox.core.TestUtils;
5+
import org.junit.Test;
6+
7+
import java.util.Collections;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertNotNull;
11+
import static org.junit.Assert.assertNull;
12+
13+
public class TrafficCodesTest extends TestUtils {
14+
15+
@Test
16+
public void sanity() {
17+
assertNotNull(getDefault());
18+
}
19+
20+
@Test
21+
public void serialize() throws Exception {
22+
TrafficCodes trafficCodes = getFilledTrafficCodes();
23+
byte[] serialized = TestUtils.serialize(trafficCodes);
24+
assertEquals(trafficCodes, deserialize(serialized, TrafficCodes.class));
25+
}
26+
27+
@Test
28+
public void toAndFromJson() {
29+
TrafficCodes source = getFilledTrafficCodes();
30+
31+
String json = source.toJson();
32+
TrafficCodes serialized = TrafficCodes.fromJson(json);
33+
34+
assertEquals(source, serialized);
35+
}
36+
37+
@Test
38+
public void deserializeFromJson() {
39+
String json = "{" +
40+
"\"jartic_cause_code\": 400," +
41+
"\"jartic_regulation_code\": 600," +
42+
"\"unknown_code\": 700" +
43+
"}";
44+
45+
TrafficCodes fromJson = TrafficCodes.fromJson(json);
46+
47+
assertNotNull(fromJson);
48+
assertEquals(400, (int) fromJson.jarticCauseCode());
49+
assertEquals(600, (int) fromJson.jarticRegulationCode());
50+
assertEquals(700, fromJson.getUnrecognizedJsonProperties().get("unknown_code").getAsInt());
51+
}
52+
53+
@Test
54+
public void deserializeFromEmptyJson() {
55+
TrafficCodes fromJson = TrafficCodes.fromJson("{}");
56+
57+
assertNotNull(fromJson);
58+
assertNull(fromJson.jarticCauseCode());
59+
assertNull(fromJson.jarticRegulationCode());
60+
assertNull(fromJson.getUnrecognizedJsonProperties());
61+
}
62+
63+
private TrafficCodes getDefault() {
64+
return TrafficCodes.builder().build();
65+
}
66+
67+
private TrafficCodes getFilledTrafficCodes() {
68+
return TrafficCodes.builder()
69+
.jarticCauseCode(400)
70+
.jarticRegulationCode(600)
71+
.unrecognizedJsonProperties(
72+
Collections.singletonMap("unknown_code", new JsonPrimitive((700)))
73+
)
74+
.build();
75+
}
76+
}

0 commit comments

Comments
 (0)