Skip to content

Commit 2d334d0

Browse files
committed
add support for sa pa amenities
1 parent a6a01fc commit 2d334d0

4 files changed

Lines changed: 214 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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.mapbox.api.directions.v5.DirectionsAdapterFactory;
10+
import com.mapbox.api.directions.v5.DirectionsCriteria;
11+
12+
/**
13+
* An object containing information about amenities available at rest stops along the route.
14+
* Only available on the {@link DirectionsCriteria#PROFILE_DRIVING} profile.
15+
*/
16+
@AutoValue
17+
public abstract class Amenities extends DirectionsJsonObject {
18+
19+
/**
20+
* The type of amenities such as gas, restaurants, shopping, bank, atm etc.
21+
* Note that adding new possible types is not considered a breaking change.
22+
*/
23+
@NonNull
24+
public abstract String type();
25+
26+
/**
27+
* The name of the amenity. Optionally included if data is available.
28+
*/
29+
@Nullable
30+
public abstract String name();
31+
32+
/**
33+
* The brand name of the amenity. Optionally included if data is available.
34+
*/
35+
@Nullable
36+
public abstract String brand();
37+
38+
/**
39+
* Create a new instance of this class by using the {@link Amenities.Builder} class.
40+
*
41+
* @return this classes {@link Amenities.Builder} for creating a new instance
42+
*/
43+
public static Amenities.Builder builder() {
44+
return new AutoValue_Amenities.Builder();
45+
}
46+
47+
/**
48+
* Convert the current {@link Amenities} to its builder holding the currently assigned
49+
* values. This allows you to modify a single property and then rebuild the object resulting in
50+
* an updated and modified {@link Amenities}.
51+
*
52+
* @return a {@link Amenities.Builder} with the same values set to match the ones
53+
* defined in this {@link Amenities}
54+
*/
55+
public abstract Amenities.Builder toBuilder();
56+
57+
/**
58+
* Gson type adapter for parsing Gson to this class.
59+
*
60+
* @param gson the built {@link Gson} object
61+
* @return the type adapter for this class
62+
*/
63+
public static TypeAdapter<Amenities> typeAdapter(Gson gson) {
64+
return new AutoValue_Amenities.GsonTypeAdapter(gson);
65+
}
66+
67+
/**
68+
* Create a new instance of this class by passing in a formatted valid JSON String.
69+
*
70+
* @param json a formatted valid JSON string defining an Incident
71+
* @return a new instance of this class defined by the values passed in the method
72+
*/
73+
public static Amenities fromJson(String json) {
74+
GsonBuilder gson = new GsonBuilder();
75+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
76+
return gson.create().fromJson(json, Amenities.class);
77+
}
78+
79+
/**
80+
* This builder can be used to set the values describing the {@link Amenities}.
81+
*/
82+
@AutoValue.Builder
83+
public abstract static class Builder extends DirectionsJsonObject.Builder<Amenities.Builder> {
84+
85+
/**
86+
* The type of amenity, includes amenities such as gas, restaurants etc.
87+
* Note that adding new possible types is not considered a breaking change.
88+
*
89+
* @param type amenity type
90+
*/
91+
@NonNull
92+
public abstract Builder type(@NonNull String type);
93+
94+
/**
95+
* The name of the amenity. Optionally included if data is available.
96+
*
97+
* @param name amenity name
98+
*/
99+
@NonNull
100+
public abstract Builder name(@Nullable String name);
101+
102+
/**
103+
* The brand name of the amenity. Optionally included if data is available.
104+
*
105+
* @param brand amenity brand name
106+
*/
107+
@NonNull
108+
public abstract Builder brand(@Nullable String brand);
109+
110+
/**
111+
* Build a new {@link Amenities} object.
112+
*
113+
* @return a new {@link Amenities} using the provided values in this builder
114+
*/
115+
@NonNull
116+
public abstract Amenities build();
117+
}
118+
}

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

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

3+
import androidx.annotation.NonNull;
34
import androidx.annotation.Nullable;
45

56
import com.google.auto.value.AutoValue;
@@ -9,6 +10,8 @@
910
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
1011
import com.mapbox.api.directions.v5.DirectionsCriteria;
1112

13+
import java.util.List;
14+
1215
/**
1316
* An object containing information about passing rest stops along the route.
1417
* Only available on the {@link DirectionsCriteria#PROFILE_DRIVING} profile.
@@ -30,6 +33,12 @@ public abstract class RestStop extends DirectionsJsonObject {
3033
@Nullable
3134
public abstract String name();
3235

36+
/**
37+
* The list of amenities at the rest stop if available.
38+
*/
39+
@Nullable
40+
public abstract List<Amenities> amenities();
41+
3342
/**
3443
* Create a new instance of this class by using the {@link Builder} class.
3544
*
@@ -84,20 +93,31 @@ public abstract static class Builder extends DirectionsJsonObject.Builder<Builde
8493
*
8594
* @param type rest stop type
8695
*/
96+
@NonNull
8797
public abstract Builder type(@Nullable String type);
8898

8999
/**
90100
* The name of the rest stop. Optionally included if data is available.
91101
*
92102
* @param name rest stop name
93103
*/
104+
@NonNull
94105
public abstract Builder name(@Nullable String name);
95106

107+
/**
108+
* The list of amenities at the rest stop if available.
109+
*
110+
* @param amenities list of amenities
111+
*/
112+
@NonNull
113+
public abstract Builder amenities(@Nullable List<Amenities> amenities);
114+
96115
/**
97116
* Build a new {@link RestStop} object.
98117
*
99118
* @return a new {@link RestStop} using the provided values in this builder
100119
*/
120+
@NonNull
101121
public abstract RestStop build();
102122
}
103123
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
import static org.junit.Assert.assertNull;
9+
10+
public class AmenitiesTest extends TestUtils {
11+
12+
private static final String DIRECTIONS_V5_AMENITIES_FIXTURE = "directions_v5_amenities.json";
13+
@Test
14+
public void sanity(){
15+
assertNotNull(getDefault());
16+
}
17+
18+
private Amenities getDefault() {
19+
return Amenities.builder()
20+
.type("restaurant")
21+
.name("starbucks")
22+
.build();
23+
}
24+
25+
@Test
26+
public void serializableObject() throws Exception {
27+
Amenities expected = Amenities
28+
.builder()
29+
.type("restaurant")
30+
.name("starbucks")
31+
.build();
32+
33+
Amenities actual = deserialize(
34+
TestUtils.serialize(expected),
35+
Amenities.class
36+
);
37+
38+
assertEquals(expected, actual);
39+
}
40+
41+
@Test
42+
public void toFromJson() {
43+
Amenities expected = Amenities
44+
.builder()
45+
.type("restaurant")
46+
.name("starbucks")
47+
.build();
48+
49+
Amenities actual = Amenities.fromJson(expected.toJson());
50+
51+
assertEquals(expected, actual);
52+
}
53+
54+
@Test
55+
public void fromJson_correctlyBuildsFromJson() throws Exception {
56+
String json = loadJsonFixture(DIRECTIONS_V5_AMENITIES_FIXTURE);
57+
DirectionsResponse response = DirectionsResponse.fromJson(json);
58+
59+
RestStop restStop = response.routes().get(0).legs().get(0).steps().get(1).intersections().get(0).restStop();
60+
Amenities amenities1 = restStop.amenities().get(0);
61+
Amenities amenities2 = restStop.amenities().get(1);
62+
63+
assertNotNull(amenities1);
64+
assertNotNull(amenities2);
65+
assertNull(amenities1.brand());
66+
assertEquals("restaurant", amenities1.type());
67+
assertEquals("Panda Express", amenities1.name());
68+
assertNull(amenities2.name());
69+
assertEquals("gas_station", amenities2.type());
70+
assertEquals("Shell", amenities2.brand());
71+
}
72+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public void sanity() throws Exception {
1717
RestStop restStop = RestStop.builder()
1818
.name("ABCD")
1919
.type("service_area")
20+
.amenities(Collections.<Amenities>emptyList())
2021
.build();
2122
assertNotNull(restStop);
2223
}
@@ -27,6 +28,7 @@ public void serializableObject() throws Exception {
2728
.builder()
2829
.name("ABCD")
2930
.type("service_area")
31+
.amenities(Collections.<Amenities>emptyList())
3032
.build();
3133

3234
RestStop actual = deserialize(
@@ -43,6 +45,7 @@ public void toFromJson() {
4345
.builder()
4446
.name("ABCD")
4547
.type("service_area")
48+
.amenities(Collections.<Amenities>emptyList())
4649
.build();
4750

4851
RestStop actual = RestStop.fromJson(expected.toJson());
@@ -58,6 +61,7 @@ public void fromJson_correctlyBuildsFromJson() throws Exception {
5861
RestStop restStop = response.routes().get(0).legs().get(0).steps().get(1).intersections().get(0).restStop();
5962

6063
assertNotNull(restStop);
64+
assertNotNull(restStop.amenities());
6165
assertEquals("SA", restStop.name());
6266
assertEquals("service_area", restStop.type());
6367
}

0 commit comments

Comments
 (0)