Skip to content

Commit

Permalink
NAVAND-1400: add MergingArea
Browse files Browse the repository at this point in the history
  • Loading branch information
dzinad committed Jul 11, 2023
1 parent d69aa0e commit 406fbee
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Mapbox welcomes participation and contributions from everyone.

### main

- Added `StepIntersection.mergingArea`.

### v6.12.0 - May 30, 2023

- No additional changes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.mapbox.api.directions.v5.models;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringDef;
import com.google.auto.value.AutoValue;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* Class containing information about merging area,
* i.e. an area where traffic is being merged into the current road.
*/
@AutoValue
public abstract class MergingArea extends DirectionsJsonObject {

/**
* {@link Type} value meaning that traffic is being merged into current road from the left side.
*/
public static final String TYPE_FROM_LEFT = "from_left";

/**
* {@link Type} value meaning that traffic is being merged into current road from the right side.
*/
public static final String TYPE_FROM_RIGHT = "from_right";

/**
* {@link Type} value meaning that traffic is being merged into current road from both sides.
*/
public static final String TYPE_FROM_BOTH_SIDES = "from_both_sides";

/**
* Merging Area type.
*/
@Retention(RetentionPolicy.CLASS)
@StringDef({
TYPE_FROM_LEFT,
TYPE_FROM_RIGHT,
TYPE_FROM_BOTH_SIDES
})
public @interface Type {
}

/**
* Type of the merging area. See {@link Type} for possible values.
*
* @return type of the merging area.
*/
@Nullable
public abstract @Type String type();

/**
* Create a new instance of this class by using the {@link Builder} class.
*
* @return this classes {@link Builder} for creating a new instance
*/
public static Builder builder() {
return new AutoValue_MergingArea.Builder();
}

/**
* Convert the current {@link MergingArea} to its builder holding the currently assigned
* values. This allows you to modify a single property and then rebuild the object resulting in
* an updated and modified {@link MergingArea}.
*
* @return a {@link Builder} with the same values set to match the ones defined in this
* {@link MergingArea}
*/
public abstract Builder toBuilder();

/**
* Gson type adapter for parsing Gson to this class.
*
* @param gson the built {@link Gson} object
* @return the type adapter for this class
*/
public static TypeAdapter<MergingArea> typeAdapter(Gson gson) {
return new AutoValue_MergingArea.GsonTypeAdapter(gson);
}

/**
* Create a new instance of this class by passing in a formatted valid JSON String.
*
* @param json a formatted valid JSON string defining a Merging Area
* @return a new instance of this class defined by the values passed in the method
*/
public static MergingArea fromJson(String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().fromJson(json, MergingArea.class);
}

/**
* This builder can be used to set the values describing the {@link MergingArea}.
*
* @since 3.0.0
*/
@AutoValue.Builder
public abstract static class Builder extends DirectionsJsonObject.Builder<Builder> {

/**
* Type of the merging area.
*
* @param type type, see {@link Type} for possible values.
* @return this builder for chaining options together
*/
@NonNull
public abstract Builder type(@Nullable @Type String type);

/**
* Build a new {@link MergingArea} object.
*
* @return a new {@link MergingArea} using the provided values in this builder
*/
@NonNull
public abstract MergingArea build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ public Point location() {
@SuppressWarnings("checkstyle:javadocmethod")
public abstract Junction junction();

/**
* Object containing information about merging area starting at intersection.
*
* @return MergingArea object if present, null otherwise.
*/
@Nullable
@SerializedName("merging_area")
public abstract MergingArea mergingArea();

/**
* Convert the current {@link StepIntersection} to its builder holding the currently assigned
* values. This allows you to modify a single property and then rebuild the object resulting in
Expand Down Expand Up @@ -542,6 +551,15 @@ public abstract static class Builder extends DirectionsJsonObject.Builder<Builde
@NonNull
public abstract Builder rawLocation(@NonNull double[] rawLocation);

/**
* Object containing information about merging area starting at intersection.
*
* @param mergingArea MergingArea object
* @return this builder for chaining options together
*/
@NonNull
public abstract Builder mergingArea(@Nullable MergingArea mergingArea);

/**
* Build a new {@link StepIntersection} object.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.mapbox.api.directions.v5.models;

import com.google.gson.JsonPrimitive;
import com.mapbox.core.TestUtils;
import org.junit.Test;

import java.util.Collections;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class MergingAreaTest extends TestUtils {

@Test
public void sanity() throws Exception {
MergingArea mergingArea = MergingArea.builder().build();
assertNotNull(mergingArea);
}

@Test
public void testSerializableDefault() throws Exception {
MergingArea intersection = MergingArea.builder().build();
byte[] serialized = TestUtils.serialize(intersection);
assertEquals(intersection, deserialize(serialized, MergingArea.class));
}

@Test
public void testSerializableFilled() throws Exception {
MergingArea mergingArea = MergingArea.builder().type(MergingArea.TYPE_FROM_LEFT).build();
byte[] serialized = TestUtils.serialize(mergingArea);
assertEquals(mergingArea, deserialize(serialized, MergingArea.class));
}

@Test
public void testToFromJsonDefault() {
MergingArea mergingArea = MergingArea.builder().build();

String jsonString = mergingArea.toJson();
MergingArea mergingAreaFromJson = MergingArea.fromJson(jsonString);

assertEquals(mergingArea, mergingAreaFromJson);
}

@Test
public void testToFromJsonFilled() {
MergingArea mergingArea = MergingArea.builder().type(MergingArea.TYPE_FROM_LEFT).build();

String jsonString = mergingArea.toJson();
MergingArea mergingAreaFromJson = MergingArea.fromJson(jsonString);

assertEquals(mergingArea, mergingAreaFromJson);
}

@Test
public void testFromJsonDefault() {
String mergingAreaJsonString = "{}";

MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);

assertNull(mergingArea.type());
}

@Test
public void testFromJsonFromLeft() {
String mergingAreaJsonString = "{\"type\":\"from_left\"}";

MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);

assertEquals(MergingArea.TYPE_FROM_LEFT, mergingArea.type());
}

@Test
public void testFromJsonFromRight() {
String mergingAreaJsonString = "{\"type\":\"from_right\"}";

MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);

assertEquals(MergingArea.TYPE_FROM_RIGHT, mergingArea.type());
}

@Test
public void testFromJsonFromBothSides() {
String mergingAreaJsonString = "{\"type\":\"from_both_sides\"}";

MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);

assertEquals(MergingArea.TYPE_FROM_BOTH_SIDES, mergingArea.type());
}

@Test
public void testFromJsonUnknownType() {
String mergingAreaJsonString = "{\"type\":\"unknown\"}";

MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);

assertEquals("unknown", mergingArea.type());
}

@Test
public void testFromJsonUnrecognizedProperties() {
String mergingAreaJsonString = "{\"key\":\"value\"}";

MergingArea mergingArea = MergingArea.fromJson(mergingAreaJsonString);

assertEquals(
Collections.singletonMap("key", new JsonPrimitive("value")),
mergingArea.getUnrecognizedJsonProperties()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void testToFromJson1() {
.tunnelName("tunnel_name")
.junction(Junction.builder().name("jct_name").build())
.interchange(Interchange.builder().name("ic_name").build())
.mergingArea(MergingArea.builder().type(MergingArea.TYPE_FROM_LEFT).build())
.build();

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

Expand All @@ -96,6 +98,7 @@ public void testFromJson() {
+ "\"railway_crossing\": true,"
+ "\"traffic_signal\": true,"
+ "\"stop_sign\": true,"
+ "\"merging_area\": {\"type\": \"from_right\"},"
+ "\"yield_sign\": true"
+ "}";

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

Point location = stepIntersection.location();
assertEquals(13.426579, location.longitude(), 0.0001);
Expand Down Expand Up @@ -215,4 +219,31 @@ public void testNullInterchange() {
String jsonStr = stepIntersection.toJson();
compareJson(stepIntersectionJsonString, jsonStr);
}

@Test
public void testMergingArea() {
String stepIntersectionJsonString = "{"
+ "\"location\": [ 13.426579, 52.508068 ],"
+ "\"merging_area\": { \"type\": \"from_left\" }"
+ "}";

StepIntersection stepIntersection = StepIntersection.fromJson(stepIntersectionJsonString);

Assert.assertEquals(MergingArea.builder().type(MergingArea.TYPE_FROM_LEFT).build(), stepIntersection.mergingArea());
String jsonStr = stepIntersection.toJson();
compareJson(stepIntersectionJsonString, jsonStr);
}

@Test
public void testNullMergingArea() {
String stepIntersectionJsonString = "{"
+ "\"location\": [ 13.426579, 52.508068 ]"
+ "}";

StepIntersection stepIntersection = StepIntersection.fromJson(stepIntersectionJsonString);

Assert.assertNull(stepIntersection.mergingArea());
String jsonStr = stepIntersection.toJson();
compareJson(stepIntersectionJsonString, jsonStr);
}
}

0 comments on commit 406fbee

Please sign in to comment.