Skip to content

Commit 484a082

Browse files
authored
Refresh Route: refactored (#1225)
1 parent c8103c5 commit 484a082

17 files changed

Lines changed: 462 additions & 35 deletions

File tree

samples/src/main/java/com/mapbox/samples/BasicDirectionsRefresh.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,57 @@
99

1010
import java.io.IOException;
1111

12+
import java.util.Arrays;
1213
import retrofit2.Call;
1314
import retrofit2.Callback;
1415
import retrofit2.Response;
1516

1617
import static com.mapbox.api.directions.v5.DirectionsCriteria.ANNOTATION_CONGESTION;
18+
import static com.mapbox.api.directions.v5.DirectionsCriteria.ANNOTATION_MAXSPEED;
1719
import static com.mapbox.api.directions.v5.DirectionsCriteria.OVERVIEW_FULL;
1820
import static com.mapbox.api.directions.v5.DirectionsCriteria.PROFILE_DRIVING_TRAFFIC;
1921

2022
public class BasicDirectionsRefresh {
2123
public static void main(String[] args) throws IOException {
22-
String requestId = simpleMapboxDirectionsRequest();
23-
simpleMapboxDirectionsRefreshRequest(requestId);
24+
String requestIdFirst = simpleMapboxDirectionsRequest(mapboxDirections(false));
25+
simpleMapboxDirectionsRefreshRequest(requestIdFirst, 0);
26+
27+
String requestIdSecond = simpleMapboxDirectionsRequest(mapboxDirections(true));
28+
simpleMapboxDirectionsRefreshRequest(requestIdSecond, 1);
29+
}
30+
31+
private static String simpleMapboxDirectionsRequest(MapboxDirections directions) throws IOException {
32+
Response<DirectionsResponse> response = directions.executeCall();
33+
System.out.println("Directions response: " + response);
34+
String requestId = response.body().routes().get(0).routeOptions().requestUuid();
35+
36+
return requestId;
2437
}
2538

26-
private static String simpleMapboxDirectionsRequest() throws IOException {
27-
MapboxDirections directions = MapboxDirections.builder()
39+
private static MapboxDirections mapboxDirections(Boolean addWaypoint) {
40+
MapboxDirections.Builder directions = MapboxDirections.builder()
2841
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
2942
.enableRefresh(true)
3043
.origin(Point.fromLngLat(-95.6332, 29.7890))
3144
.destination(Point.fromLngLat(-95.3591, 29.7576))
3245
.overview(OVERVIEW_FULL)
3346
.profile(PROFILE_DRIVING_TRAFFIC)
34-
.addAnnotation(ANNOTATION_CONGESTION).build();
47+
.annotations(Arrays.asList(ANNOTATION_CONGESTION,ANNOTATION_MAXSPEED));
3548

36-
Response<DirectionsResponse> response = directions.executeCall();
37-
System.out.println("Directions response: " + response);
38-
String requestId = response.body().routes().get(0).routeOptions().requestUuid();
49+
if (addWaypoint) {
50+
directions.addWaypoint(Point.fromLngLat(-95.5591, 29.7376));
51+
}
3952

40-
return requestId;
53+
return directions.build();
4154
}
4255

43-
private static void simpleMapboxDirectionsRefreshRequest(String requestId) {
56+
private static void simpleMapboxDirectionsRefreshRequest(String requestId, Integer legIndex) {
4457
MapboxDirectionsRefresh refresh =
4558
MapboxDirectionsRefresh.builder()
4659
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
4760
.requestId(requestId)
4861
.routeIndex(0)
49-
.legIndex(0)
62+
.legIndex(legIndex)
5063
.build();
5164

5265
refresh.enqueueCall(new Callback<DirectionsRefreshResponse>() {

services-directions-refresh-models/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ dependencies {
99
// AutoValue
1010
compileOnly dependenciesList.autoValue
1111
compileOnly dependenciesList.autoValueGson
12+
13+
// Test Dependencies
14+
testImplementation project(path: ':services-core', configuration: 'testOutput')
1215
}

services-directions-refresh-models/src/main/java/com/mapbox/api/directionsrefresh/v1/models/DirectionsRefreshAdapterFactory.java renamed to services-directions-refresh-models/src/main/java/com/mapbox/api/directionsrefresh/v1/DirectionsRefreshAdapterFactory.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
package com.mapbox.api.directionsrefresh.v1.models;
1+
package com.mapbox.api.directionsrefresh.v1;
22

33
import com.google.gson.TypeAdapterFactory;
44
import com.ryanharter.auto.value.gson.GsonTypeAdapterFactory;
55

66
/**
7-
* Required so that AutoValue can generate specific type adapters when needed inside the
8-
* directions refresh package.
9-
*
10-
* @since 4.4.0
7+
* Required so that AutoValue can generate specific type adapters when needed inside the direction
8+
* packages.
119
*/
1210
@GsonTypeAdapterFactory
1311
public abstract class DirectionsRefreshAdapterFactory implements TypeAdapterFactory {
1412

1513
/**
16-
* Creates a TypeAdapter that AutoValue uses to generate specific type adapters.
17-
*
18-
* @return a {@link TypeAdapterFactory} to deserialize {@link DirectionsRefreshResponse}
19-
* @since 4.4.0
14+
* Creates a TypeAdapter that AutoValues uses to generate specific type adapters when needed
15+
* inside the direction package classes.
2016
*/
2117
public static TypeAdapterFactory create() {
2218
return new AutoValueGson_DirectionsRefreshAdapterFactory();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.mapbox.api.directionsrefresh.v1.models;
2+
3+
import com.google.gson.GsonBuilder;
4+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
5+
import com.mapbox.api.directionsrefresh.v1.DirectionsRefreshAdapterFactory;
6+
import com.mapbox.geojson.Point;
7+
import com.mapbox.geojson.PointAsCoordinatesTypeAdapter;
8+
import java.io.Serializable;
9+
10+
/**
11+
* Provides a base class for Directions model classes.
12+
*/
13+
public class DirectionsRefreshJsonObject implements Serializable {
14+
15+
/**
16+
* This takes the currently defined values found inside this instance and converts it to a json
17+
* string.
18+
*
19+
* @return a JSON string which represents this DirectionsJsonObject
20+
*/
21+
public String toJson() {
22+
GsonBuilder gson = new GsonBuilder();
23+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
24+
gson.registerTypeAdapter(Point.class, new PointAsCoordinatesTypeAdapter());
25+
gson.registerTypeAdapterFactory(DirectionsRefreshAdapterFactory.create());
26+
return gson.create().toJson(this);
27+
}
28+
}

services-directions-refresh-models/src/main/java/com/mapbox/api/directionsrefresh/v1/models/DirectionsRefreshResponse.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
import com.google.gson.GsonBuilder;
99
import com.google.gson.TypeAdapter;
1010
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
11-
import com.mapbox.api.directions.v5.models.DirectionsJsonObject;
12-
import com.mapbox.api.directions.v5.models.DirectionsRoute;
13-
import com.mapbox.api.directions.v5.models.RouteLeg;
11+
import com.mapbox.api.directionsrefresh.v1.DirectionsRefreshAdapterFactory;
1412

1513
/**
1614
* Response object for Directions Refresh requests.
1715
*
1816
* @since 4.4.0
1917
*/
2018
@AutoValue
21-
public abstract class DirectionsRefreshResponse extends DirectionsJsonObject {
19+
public abstract class DirectionsRefreshResponse extends DirectionsRefreshJsonObject {
2220

2321
/**
2422
* String indicating the state of the response. This is a separate code than the HTTP status code.
@@ -49,15 +47,15 @@ public abstract class DirectionsRefreshResponse extends DirectionsJsonObject {
4947
public abstract String message();
5048

5149
/**
52-
* Barebones {@link DirectionsRoute} which only contains a list of
53-
* {@link RouteLeg}s, which only contain lists of the
50+
* Barebones {@link DirectionsRouteRefresh} which only contains a list of
51+
* {@link RouteLegRefresh}s, which only contain lists of the
5452
* refreshed annotations.
5553
*
5654
* @return barebones route with annotation data
5755
* @since 4.4.0
5856
*/
5957
@Nullable
60-
public abstract DirectionsRoute route();
58+
public abstract DirectionsRouteRefresh route();
6159

6260
/**
6361
* Create a new instance of this class by using the {@link Builder} class.
@@ -124,15 +122,15 @@ public abstract static class Builder {
124122
public abstract Builder message(String message);
125123

126124
/**
127-
* Barebones {@link DirectionsRoute} which only contains a list of
128-
* {@link RouteLeg}s, which only contain lists of the
125+
* Barebones {@link DirectionsRouteRefresh} which only contains a list of
126+
* {@link RouteLegRefresh}s, which only contain lists of the
129127
* refreshed annotations.
130128
*
131-
* @param directionsRoute route containing annotation data
129+
* @param directionsRouteRefresh route containing annotation data
132130
* @return this builder
133131
* @since 4.4.0
134132
*/
135-
public abstract Builder route(DirectionsRoute directionsRoute);
133+
public abstract Builder route(DirectionsRouteRefresh directionsRouteRefresh);
136134

137135
/**
138136
* Builds a new {@link DirectionsRefreshResponse} object.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.mapbox.api.directionsrefresh.v1.models;
2+
3+
import androidx.annotation.Nullable;
4+
import com.google.auto.value.AutoValue;
5+
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
7+
import com.google.gson.TypeAdapter;
8+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
9+
import com.mapbox.api.directions.v5.models.DirectionsWaypoint;
10+
import com.mapbox.api.directionsrefresh.v1.DirectionsRefreshAdapterFactory;
11+
import java.util.List;
12+
13+
/**
14+
* Detailed information about an individual route such as the duration, distance and geometry.
15+
*/
16+
@AutoValue
17+
public abstract class DirectionsRouteRefresh extends DirectionsRefreshJsonObject {
18+
19+
/**
20+
* Create a new instance of this class by using the {@link Builder} class.
21+
*
22+
* @return this classes {@link Builder} for creating a new instance
23+
*/
24+
public static Builder builder() {
25+
return new AutoValue_DirectionsRouteRefresh.Builder();
26+
}
27+
28+
/**
29+
* A Leg Refresh is an object contain refresh data between only two {@link DirectionsWaypoint}.
30+
*
31+
* @return list of {@link RouteLegRefresh} objects
32+
*/
33+
@Nullable
34+
public abstract List<RouteLegRefresh> legs();
35+
36+
/**
37+
* Convert the current {@link DirectionsRouteRefresh} to its builder holding the currently
38+
* assigned values. This allows you to modify a single property and then rebuild the object
39+
* resulting in an updated and modified {@link DirectionsRouteRefresh}.
40+
*
41+
* @return a {@link DirectionsRouteRefresh.Builder} with the same values set to match the ones
42+
* defined in this {@link DirectionsRouteRefresh}
43+
*/
44+
public abstract Builder toBuilder();
45+
46+
/**
47+
* Gson type adapter for parsing Gson to this class.
48+
*
49+
* @param gson the built {@link Gson} object
50+
* @return the type adapter for this class
51+
*/
52+
public static TypeAdapter<DirectionsRouteRefresh> typeAdapter(Gson gson) {
53+
return new AutoValue_DirectionsRouteRefresh.GsonTypeAdapter(gson);
54+
}
55+
56+
/**
57+
* Create a new instance of this class by passing in a formatted valid JSON String.
58+
*
59+
* @param json a formatted valid JSON string defining a GeoJson Directions Route
60+
* @return a new instance of this class defined by the values passed inside this static factory
61+
* method
62+
*/
63+
public static DirectionsRouteRefresh fromJson(String json) {
64+
GsonBuilder gson = new GsonBuilder();
65+
gson.registerTypeAdapterFactory(DirectionsRefreshAdapterFactory.create());
66+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
67+
return gson.create().fromJson(json, DirectionsRouteRefresh.class);
68+
}
69+
70+
/**
71+
* This builder can be used to set the values describing the {@link DirectionsRouteRefresh}.
72+
*/
73+
@AutoValue.Builder
74+
public abstract static class Builder {
75+
76+
/**
77+
* A Leg Refresh is an object contain refresh data between only two {@link DirectionsWaypoint}.
78+
*
79+
* @param legs list of {@link RouteLegRefresh} objects
80+
* @return this builder for chaining options together
81+
*/
82+
public abstract Builder legs(@Nullable List<RouteLegRefresh> legs);
83+
84+
/**
85+
* Build a new {@link DirectionsRouteRefresh} object.
86+
*
87+
* @return a new {@link DirectionsRouteRefresh} using the provided values in this builder
88+
*/
89+
public abstract DirectionsRouteRefresh build();
90+
}
91+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.mapbox.api.directionsrefresh.v1.models;
2+
3+
import androidx.annotation.Nullable;
4+
import com.google.auto.value.AutoValue;
5+
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
7+
import com.google.gson.TypeAdapter;
8+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
9+
import com.mapbox.api.directions.v5.models.DirectionsWaypoint;
10+
import com.mapbox.api.directions.v5.models.LegAnnotation;
11+
import com.mapbox.api.directionsrefresh.v1.DirectionsRefreshAdapterFactory;
12+
13+
/**
14+
* A route refresh data between only two {@link DirectionsWaypoint}.
15+
*/
16+
@AutoValue
17+
public abstract class RouteLegRefresh extends DirectionsRefreshJsonObject {
18+
19+
/**
20+
* Create a new instance of this class by using the {@link Builder} class.
21+
*
22+
* @return this classes {@link Builder} for creating a new instance
23+
*/
24+
public static Builder builder() {
25+
return new AutoValue_RouteLegRefresh.Builder();
26+
}
27+
28+
/**
29+
* A {@link LegAnnotation} that contains additional details about each line segment along the
30+
* route geometry. If you'd like to receiving this, you must request it inside your Directions
31+
* request before executing the call.
32+
*
33+
* @return a {@link LegAnnotation} object
34+
*/
35+
@Nullable
36+
public abstract LegAnnotation annotation();
37+
38+
/**
39+
* Convert the current {@link RouteLegRefresh} to its builder holding the currently assigned
40+
* values. This allows you to modify a single property and then rebuild the object resulting in
41+
* an updated and modified {@link RouteLegRefresh}.
42+
*
43+
* @return a {@link RouteLegRefresh.Builder} with the same values set to match the ones defined
44+
* in this {@link RouteLegRefresh}
45+
*/
46+
47+
public abstract Builder toBuilder();
48+
49+
/**
50+
* Gson type adapter for parsing Gson to this class.
51+
*
52+
* @param gson the built {@link Gson} object
53+
* @return the type adapter for this class
54+
*/
55+
public static TypeAdapter<RouteLegRefresh> typeAdapter(Gson gson) {
56+
return new AutoValue_RouteLegRefresh.GsonTypeAdapter(gson);
57+
}
58+
59+
/**
60+
* Create a new instance of this class by passing in a formatted valid JSON String.
61+
*
62+
* @param json a formatted valid JSON string defining a RouteLeg
63+
* @return a new instance of this class defined by the values passed inside this static factory
64+
* method
65+
*/
66+
public static RouteLegRefresh fromJson(String json) {
67+
GsonBuilder gson = new GsonBuilder();
68+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
69+
gson.registerTypeAdapterFactory(DirectionsRefreshAdapterFactory.create());
70+
return gson.create().fromJson(json, RouteLegRefresh.class);
71+
}
72+
73+
/**
74+
* This builder can be used to set the values describing the {@link RouteLegRefresh}.
75+
*/
76+
@AutoValue.Builder
77+
public abstract static class Builder {
78+
79+
/**
80+
* A {@link LegAnnotation} that contains additional details about each line segment along the
81+
* route geometry. If you'd like to receiving this, you must request it inside your Directions
82+
* request before executing the call.
83+
*
84+
* @param annotation a {@link LegAnnotation} object
85+
* @return this builder for chaining options together
86+
*/
87+
public abstract Builder annotation(@Nullable LegAnnotation annotation);
88+
89+
/**
90+
* Build a new {@link RouteLegRefresh} object.
91+
*
92+
* @return a new {@link RouteLegRefresh} using the provided values in this builder
93+
*/
94+
public abstract RouteLegRefresh build();
95+
}
96+
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/**
22
* Contains the model classes which represent the Directions Refresh API response.
3-
*
4-
* @since 4.4.0
53
*/
64
package com.mapbox.api.directionsrefresh.v1.models;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Contains classes for accessing the Directions Refresh API response.
3+
*/
4+
package com.mapbox.api.directionsrefresh.v1;

0 commit comments

Comments
 (0)