Skip to content

Commit 2cb7a21

Browse files
author
Cameron Mace
committed
converted direction api models
1 parent 4b25370 commit 2cb7a21

10 files changed

Lines changed: 583 additions & 975 deletions

File tree

mapbox/libjava-services/src/main/java/com/mapbox/services/api/directions/v5/MapboxDirections.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.mapbox.services.api.directions.v5;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
35
import com.mapbox.services.api.MapboxBuilder;
46
import com.mapbox.services.api.MapboxService;
57
import com.mapbox.services.api.ServicesException;
8+
import com.mapbox.services.api.directions.v5.gson.DirectionsAdapterFactory;
69
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
10+
import com.mapbox.services.api.geocoding.v5.gson.CarmenGeometryDeserializer;
11+
import com.mapbox.services.commons.geojson.Geometry;
712
import com.mapbox.services.commons.models.Position;
813
import com.mapbox.services.commons.utils.TextUtils;
914

@@ -19,22 +24,43 @@
1924
import retrofit2.converter.gson.GsonConverterFactory;
2025

2126
/**
22-
* The Directions API allows the calculation of routes between coordinates. The fastest route
23-
* is returned with geometries, and turn-by-turn instructions. The Mapbox Directions API supports
24-
* routing for driving cars, riding bicycles and walking.
27+
* The Directions API allows the calculation of routes between coordinates. The fastest route can be
28+
* returned with geometries, turn-by-turn instructions, and much more. The Mapbox Directions API
29+
* supports routing for driving cars (including live traffic), riding bicycles and walking.
30+
* Requested routes can include as much as 25 coordinates anywhere on earth (except the traffic
31+
* profile).
32+
* <p>
33+
* Requesting a route at a bare minimal must include, a Mapbox access token, destination, and an
34+
* origin.
2535
*
36+
* @see <a href="https://www.mapbox.com/android-docs/mapbox-services/overview/directions/">Android
37+
* Directions documentation</a>
38+
* @see <a href="https://www.mapbox.com/api-documentation/#directions">Directions API
39+
* documentation</a>
2640
* @since 1.0.0
2741
*/
2842
public class MapboxDirections extends MapboxService<DirectionsResponse> {
2943

30-
protected Builder builder = null;
31-
private DirectionsService service = null;
32-
private Call<DirectionsResponse> call = null;
44+
protected Builder builder;
45+
private DirectionsService service;
46+
private Call<DirectionsResponse> call;
47+
private Gson gson;
48+
3349

3450
protected MapboxDirections(Builder builder) {
3551
this.builder = builder;
3652
}
3753

54+
protected Gson getGson() {
55+
// Gson instance with type adapters
56+
if (gson == null) {
57+
gson = new GsonBuilder()
58+
.registerTypeAdapterFactory(DirectionsAdapterFactory.create())
59+
.create();
60+
}
61+
return gson;
62+
}
63+
3864
private DirectionsService getService() {
3965
// No need to recreate it
4066
if (service != null) {
@@ -44,7 +70,7 @@ private DirectionsService getService() {
4470
// Retrofit instance
4571
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
4672
.baseUrl(builder.getBaseUrl())
47-
.addConverterFactory(GsonConverterFactory.create());
73+
.addConverterFactory(GsonConverterFactory.create(getGson()));
4874
if (getCallFactory() != null) {
4975
retrofitBuilder.callFactory(getCallFactory());
5076
} else {
@@ -131,7 +157,7 @@ public Call<DirectionsResponse> cloneCall() {
131157
*
132158
* @since 1.0.0
133159
*/
134-
public static class Builder<T extends Builder> extends MapboxBuilder {
160+
public abstract static class Builder<T extends Builder> extends MapboxBuilder {
135161

136162
// We use `Boolean` instead of `boolean` to allow unset (null) values.
137163
private String user = null;
Lines changed: 54 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,87 @@
11
package com.mapbox.services.api.directions.v5.models;
22

3+
import android.support.annotation.Nullable;
4+
import com.google.auto.value.AutoValue;
5+
import com.google.gson.Gson;
6+
import com.google.gson.TypeAdapter;
7+
import android.support.annotation.NonNull;
8+
9+
10+
import java.io.Serializable;
311
import java.util.List;
412

513
/**
6-
* The response to a directions request.
14+
* This is the root Mapbox directions API response. Inside this class are several nested classes
15+
* chained together to make up a similar structure to the original APIs JSON response.
716
*
17+
* @see <a href="https://www.mapbox.com/api-documentation/#directions-response-object">Direction
18+
* Response Object</a>
819
* @since 1.0.0
920
*/
10-
public class DirectionsResponse {
11-
12-
private String code;
13-
private List<DirectionsRoute> routes;
14-
private List<DirectionsWaypoint> waypoints;
15-
16-
/**
17-
* Empty constructor
18-
*
19-
* @since 2.1.0
20-
*/
21-
public DirectionsResponse() {
22-
}
21+
@AutoValue
22+
public abstract class DirectionsResponse implements Serializable {
2323

24-
/**
25-
* Constructor taking in both a list of {@link DirectionsRoute} and a list of {@link DirectionsWaypoint}s.
26-
*
27-
* @param routes list of routes you can pass in while building this object.
28-
* @param waypoints list of waypoints you can pass in while building this object. Ideally these should match what was
29-
* used to crate the route.
30-
* @since 2.0.0
31-
*/
32-
public DirectionsResponse(List<DirectionsRoute> routes, List<DirectionsWaypoint> waypoints) {
33-
this.routes = routes;
34-
this.waypoints = waypoints;
24+
public static Builder builder() {
25+
return new AutoValue_DirectionsResponse.Builder();
3526
}
3627

3728
/**
38-
* String indicating the state of the response. This is a separate code than the HTTP
39-
* status code.
29+
* String indicating the state of the response. This is a separate code than the HTTP status code.
30+
* On normal valid responses, the value will be Ok. The possible responses are listed below:
31+
* <ul>
32+
* <li><strong>Ok</strong>:200 Normal success case</li>
33+
* <li><strong>NoRoute</strong>: 200 There was no route found for the given coordinates. Check
34+
* for impossible routes (e.g. routes over oceans without ferry connections).</li>
35+
* <li><strong>NoSegment</strong>: 200 No road segment could be matched for coordinates. Check for
36+
* coordinates too far away from a road.</li>
37+
* <li><strong>ProfileNotFound</strong>: 404 Use a valid profile as described above</li>
38+
* <li><strong>InvalidInput</strong>: 422</li>
39+
* </ul>
4040
*
41-
* @return "Ok", "NoRoute", "ProfileNotFound", or "InvalidInput".
41+
* @return a string with one of the given values described in the list above
4242
* @since 1.0.0
4343
*/
44-
public String getCode() {
45-
return code;
46-
}
44+
@NonNull
45+
public abstract String code();
4746

48-
/**
49-
* String indicating the state of the response. This is a separate code than the HTTP
50-
* status code.
51-
*
52-
* @param code "Ok", "NoRoute", "ProfileNotFound", or "InvalidInput".
53-
* @since 2.1.0
54-
*/
55-
public void setCode(String code) {
56-
this.code = code;
57-
}
47+
// TODO test that waypoints appear in correct order in list, see javadoc below
5848

5949
/**
60-
* List with Waypoints of locations snapped to the road and path network and appear in the List
61-
* in the order of the input coordinates.
50+
* List of {@link DirectionsWaypoint} objects. Each {@code waypoint} is an input coordinate
51+
* snapped to the road and path network. The {@code waypoint} appear in the list in the order of
52+
* the input coordinates.
6253
*
63-
* @return List of {@link DirectionsWaypoint} objects.
54+
* @return list of {@link DirectionsWaypoint} objects ordered from start of route till the end
6455
* @since 1.0.0
6556
*/
66-
public List<DirectionsWaypoint> getWaypoints() {
67-
return waypoints;
68-
}
69-
70-
/**
71-
* List with Waypoints of locations snapped to the road and path network and should appear in the List
72-
* in the order of the input coordinates.
73-
*
74-
* @param waypoints List of {@link DirectionsWaypoint} objects.
75-
* @since 2.1.0
76-
*/
77-
public void setWaypoints(List<DirectionsWaypoint> waypoints) {
78-
this.waypoints = waypoints;
79-
}
57+
@Nullable
58+
public abstract List<DirectionsWaypoint> waypoints();
8059

8160
/**
8261
* List containing all the different route options. It's ordered by descending recommendation
8362
* rank. In other words, object 0 in the List is the highest recommended route. if you don't
8463
* setAlternatives to true (default is false) in your builder this should always be a List of
85-
* size 1.
64+
* size 1. At most this will return 2 {@link DirectionsRoute} objects.
8665
*
87-
* @return List of {@link DirectionsRoute} objects.
66+
* @return list of {@link DirectionsRoute} objects
8867
* @since 1.0.0
8968
*/
90-
public List<DirectionsRoute> getRoutes() {
91-
return routes;
69+
@Nullable
70+
public abstract List<DirectionsRoute> routes();
71+
72+
public static TypeAdapter<DirectionsResponse> typeAdapter(Gson gson) {
73+
return new AutoValue_DirectionsResponse.GsonTypeAdapter(gson);
9274
}
9375

94-
/**
95-
* List containing all the different route options. It should be ordered by descending recommendation
96-
* rank. In other words, object 0 in the List is the highest recommended route. if you don't
97-
* setAlternatives to true (default is false) in your builder this should always be a List of
98-
* size 1.
99-
*
100-
* @param routes List of {@link DirectionsRoute} objects.
101-
* @since 2.1.0
102-
*/
103-
public void setRoutes(List<DirectionsRoute> routes) {
104-
this.routes = routes;
76+
@AutoValue.Builder
77+
public abstract static class Builder {
78+
79+
public abstract Builder code(@NonNull String code);
80+
81+
public abstract Builder waypoints(@Nullable List<DirectionsWaypoint> waypoints);
82+
83+
public abstract Builder routes(@Nullable List<DirectionsRoute> routes);
84+
85+
public abstract DirectionsResponse build();
10586
}
10687
}

0 commit comments

Comments
 (0)