-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...ices-directions-models/src/main/java/com/mapbox/api/directions/v5/models/MergingArea.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...-directions-models/src/test/java/com/mapbox/api/directions/v5/models/MergingAreaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters