Skip to content

Commit 406fbee

Browse files
committed
NAVAND-1400: add MergingArea
1 parent d69aa0e commit 406fbee

5 files changed

Lines changed: 285 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Mapbox welcomes participation and contributions from everyone.
44

55
### main
66

7+
- Added `StepIntersection.mergingArea`.
8+
79
### v6.12.0 - May 30, 2023
810

911
- No additional changes
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.mapbox.api.directions.v5.models;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
5+
import androidx.annotation.StringDef;
6+
import com.google.auto.value.AutoValue;
7+
import com.google.gson.Gson;
8+
import com.google.gson.GsonBuilder;
9+
import com.google.gson.TypeAdapter;
10+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
11+
12+
import java.lang.annotation.Retention;
13+
import java.lang.annotation.RetentionPolicy;
14+
15+
/**
16+
* Class containing information about merging area,
17+
* i.e. an area where traffic is being merged into the current road.
18+
*/
19+
@AutoValue
20+
public abstract class MergingArea extends DirectionsJsonObject {
21+
22+
/**
23+
* {@link Type} value meaning that traffic is being merged into current road from the left side.
24+
*/
25+
public static final String TYPE_FROM_LEFT = "from_left";
26+
27+
/**
28+
* {@link Type} value meaning that traffic is being merged into current road from the right side.
29+
*/
30+
public static final String TYPE_FROM_RIGHT = "from_right";
31+
32+
/**
33+
* {@link Type} value meaning that traffic is being merged into current road from both sides.
34+
*/
35+
public static final String TYPE_FROM_BOTH_SIDES = "from_both_sides";
36+
37+
/**
38+
* Merging Area type.
39+
*/
40+
@Retention(RetentionPolicy.CLASS)
41+
@StringDef({
42+
TYPE_FROM_LEFT,
43+
TYPE_FROM_RIGHT,
44+
TYPE_FROM_BOTH_SIDES
45+
})
46+
public @interface Type {
47+
}
48+
49+
/**
50+
* Type of the merging area. See {@link Type} for possible values.
51+
*
52+
* @return type of the merging area.
53+
*/
54+
@Nullable
55+
public abstract @Type String type();
56+
57+
/**
58+
* Create a new instance of this class by using the {@link Builder} class.
59+
*
60+
* @return this classes {@link Builder} for creating a new instance
61+
*/
62+
public static Builder builder() {
63+
return new AutoValue_MergingArea.Builder();
64+
}
65+
66+
/**
67+
* Convert the current {@link MergingArea} to its builder holding the currently assigned
68+
* values. This allows you to modify a single property and then rebuild the object resulting in
69+
* an updated and modified {@link MergingArea}.
70+
*
71+
* @return a {@link Builder} with the same values set to match the ones defined in this
72+
* {@link MergingArea}
73+
*/
74+
public abstract Builder toBuilder();
75+
76+
/**
77+
* Gson type adapter for parsing Gson to this class.
78+
*
79+
* @param gson the built {@link Gson} object
80+
* @return the type adapter for this class
81+
*/
82+
public static TypeAdapter<MergingArea> typeAdapter(Gson gson) {
83+
return new AutoValue_MergingArea.GsonTypeAdapter(gson);
84+
}
85+
86+
/**
87+
* Create a new instance of this class by passing in a formatted valid JSON String.
88+
*
89+
* @param json a formatted valid JSON string defining a Merging Area
90+
* @return a new instance of this class defined by the values passed in the method
91+
*/
92+
public static MergingArea fromJson(String json) {
93+
GsonBuilder gson = new GsonBuilder();
94+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
95+
return gson.create().fromJson(json, MergingArea.class);
96+
}
97+
98+
/**
99+
* This builder can be used to set the values describing the {@link MergingArea}.
100+
*
101+
* @since 3.0.0
102+
*/
103+
@AutoValue.Builder
104+
public abstract static class Builder extends DirectionsJsonObject.Builder<Builder> {
105+
106+
/**
107+
* Type of the merging area.
108+
*
109+
* @param type type, see {@link Type} for possible values.
110+
* @return this builder for chaining options together
111+
*/
112+
@NonNull
113+
public abstract Builder type(@Nullable @Type String type);
114+
115+
/**
116+
* Build a new {@link MergingArea} object.
117+
*
118+
* @return a new {@link MergingArea} using the provided values in this builder
119+
*/
120+
@NonNull
121+
public abstract MergingArea build();
122+
}
123+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ public Point location() {
265265
@SuppressWarnings("checkstyle:javadocmethod")
266266
public abstract Junction junction();
267267

268+
/**
269+
* Object containing information about merging area starting at intersection.
270+
*
271+
* @return MergingArea object if present, null otherwise.
272+
*/
273+
@Nullable
274+
@SerializedName("merging_area")
275+
public abstract MergingArea mergingArea();
276+
268277
/**
269278
* Convert the current {@link StepIntersection} to its builder holding the currently assigned
270279
* values. This allows you to modify a single property and then rebuild the object resulting in
@@ -542,6 +551,15 @@ public abstract static class Builder extends DirectionsJsonObject.Builder<Builde
542551
@NonNull
543552
public abstract Builder rawLocation(@NonNull double[] rawLocation);
544553

554+
/**
555+
* Object containing information about merging area starting at intersection.
556+
*
557+
* @param mergingArea MergingArea object
558+
* @return this builder for chaining options together
559+
*/
560+
@NonNull
561+
public abstract Builder mergingArea(@Nullable MergingArea mergingArea);
562+
545563
/**
546564
* Build a new {@link StepIntersection} object.
547565
*
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 junit.framework.TestCase.assertEquals;
10+
import static org.junit.Assert.assertNotNull;
11+
import static org.junit.Assert.assertNull;
12+
13+
public class MergingAreaTest extends TestUtils {
14+
15+
@Test
16+
public void sanity() throws Exception {
17+
MergingArea mergingArea = MergingArea.builder().build();
18+
assertNotNull(mergingArea);
19+
}
20+
21+
@Test
22+
public void testSerializableDefault() throws Exception {
23+
MergingArea intersection = MergingArea.builder().build();
24+
byte[] serialized = TestUtils.serialize(intersection);
25+
assertEquals(intersection, deserialize(serialized, MergingArea.class));
26+
}
27+
28+
@Test
29+
public void testSerializableFilled() throws Exception {
30+
MergingArea mergingArea = MergingArea.builder().type(MergingArea.TYPE_FROM_LEFT).build();
31+
byte[] serialized = TestUtils.serialize(mergingArea);
32+
assertEquals(mergingArea, deserialize(serialized, MergingArea.class));
33+
}
34+
35+
@Test
36+
public void testToFromJsonDefault() {
37+
MergingArea mergingArea = MergingArea.builder().build();
38+
39+
String jsonString = mergingArea.toJson();
40+
MergingArea mergingAreaFromJson = MergingArea.fromJson(jsonString);
41+
42+
assertEquals(mergingArea, mergingAreaFromJson);
43+
}
44+
45+
@Test
46+
public void testToFromJsonFilled() {
47+
MergingArea mergingArea = MergingArea.builder().type(MergingArea.TYPE_FROM_LEFT).build();
48+
49+
String jsonString = mergingArea.toJson();
50+
MergingArea mergingAreaFromJson = MergingArea.fromJson(jsonString);
51+
52+
assertEquals(mergingArea, mergingAreaFromJson);
53+
}
54+
55+
@Test
56+
public void testFromJsonDefault() {
57+
String mergingAreaJsonString = "{}";
58+
59+
MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);
60+
61+
assertNull(mergingArea.type());
62+
}
63+
64+
@Test
65+
public void testFromJsonFromLeft() {
66+
String mergingAreaJsonString = "{\"type\":\"from_left\"}";
67+
68+
MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);
69+
70+
assertEquals(MergingArea.TYPE_FROM_LEFT, mergingArea.type());
71+
}
72+
73+
@Test
74+
public void testFromJsonFromRight() {
75+
String mergingAreaJsonString = "{\"type\":\"from_right\"}";
76+
77+
MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);
78+
79+
assertEquals(MergingArea.TYPE_FROM_RIGHT, mergingArea.type());
80+
}
81+
82+
@Test
83+
public void testFromJsonFromBothSides() {
84+
String mergingAreaJsonString = "{\"type\":\"from_both_sides\"}";
85+
86+
MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);
87+
88+
assertEquals(MergingArea.TYPE_FROM_BOTH_SIDES, mergingArea.type());
89+
}
90+
91+
@Test
92+
public void testFromJsonUnknownType() {
93+
String mergingAreaJsonString = "{\"type\":\"unknown\"}";
94+
95+
MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);
96+
97+
assertEquals("unknown", mergingArea.type());
98+
}
99+
100+
@Test
101+
public void testFromJsonUnrecognizedProperties() {
102+
String mergingAreaJsonString = "{\"key\":\"value\"}";
103+
104+
MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);
105+
106+
assertEquals(
107+
Collections.singletonMap("key", new JsonPrimitive("value")),
108+
mergingArea.getUnrecognizedJsonProperties()
109+
);
110+
}
111+
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public void testToFromJson1() {
5656
.tunnelName("tunnel_name")
5757
.junction(Junction.builder().name("jct_name").build())
5858
.interchange(Interchange.builder().name("ic_name").build())
59+
.mergingArea(MergingArea.builder().type(MergingArea.TYPE_FROM_LEFT).build())
5960
.build();
6061

6162
String jsonString = intersection.toJson();
@@ -73,6 +74,7 @@ public void testToFromJson2() {
7374
.bearings(Arrays.asList(120, 210, 300))
7475
.rawLocation(new double[]{13.424671, 52.508812})
7576
.mapboxStreetsV8(MapboxStreetsV8.builder().roadClass("street").build())
77+
.mergingArea(MergingArea.builder().type(MergingArea.TYPE_FROM_LEFT).build())
7678
.tunnelName("tunnel_name")
7779
.build();
7880

@@ -96,6 +98,7 @@ public void testFromJson() {
9698
+ "\"railway_crossing\": true,"
9799
+ "\"traffic_signal\": true,"
98100
+ "\"stop_sign\": true,"
101+
+ "\"merging_area\": {\"type\": \"from_right\"},"
99102
+ "\"yield_sign\": true"
100103
+ "}";
101104

@@ -108,6 +111,7 @@ public void testFromJson() {
108111
assertTrue(stepIntersection.trafficSignal());
109112
assertTrue(stepIntersection.stopSign());
110113
assertTrue(stepIntersection.yieldSign());
114+
assertEquals(MergingArea.builder().type(MergingArea.TYPE_FROM_RIGHT).build(), stepIntersection.mergingArea());
111115

112116
Point location = stepIntersection.location();
113117
assertEquals(13.426579, location.longitude(), 0.0001);
@@ -215,4 +219,31 @@ public void testNullInterchange() {
215219
String jsonStr = stepIntersection.toJson();
216220
compareJson(stepIntersectionJsonString, jsonStr);
217221
}
222+
223+
@Test
224+
public void testMergingArea() {
225+
String stepIntersectionJsonString = "{"
226+
+ "\"location\": [ 13.426579, 52.508068 ],"
227+
+ "\"merging_area\": { \"type\": \"from_left\" }"
228+
+ "}";
229+
230+
StepIntersection stepIntersection = StepIntersection.fromJson(stepIntersectionJsonString);
231+
232+
Assert.assertEquals(MergingArea.builder().type(MergingArea.TYPE_FROM_LEFT).build(), stepIntersection.mergingArea());
233+
String jsonStr = stepIntersection.toJson();
234+
compareJson(stepIntersectionJsonString, jsonStr);
235+
}
236+
237+
@Test
238+
public void testNullMergingArea() {
239+
String stepIntersectionJsonString = "{"
240+
+ "\"location\": [ 13.426579, 52.508068 ]"
241+
+ "}";
242+
243+
StepIntersection stepIntersection = StepIntersection.fromJson(stepIntersectionJsonString);
244+
245+
Assert.assertNull(stepIntersection.mergingArea());
246+
String jsonStr = stepIntersection.toJson();
247+
compareJson(stepIntersectionJsonString, jsonStr);
248+
}
218249
}

0 commit comments

Comments
 (0)