|
1 | 1 | package com.mapbox.services.api.directions.v5.models; |
2 | 2 |
|
| 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; |
3 | 11 | import java.util.List; |
4 | 12 |
|
5 | 13 | /** |
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. |
7 | 16 | * |
| 17 | + * @see <a href="https://www.mapbox.com/api-documentation/#directions-response-object">Direction |
| 18 | + * Response Object</a> |
8 | 19 | * @since 1.0.0 |
9 | 20 | */ |
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 { |
23 | 23 |
|
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(); |
35 | 26 | } |
36 | 27 |
|
37 | 28 | /** |
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> |
40 | 40 | * |
41 | | - * @return "Ok", "NoRoute", "ProfileNotFound", or "InvalidInput". |
| 41 | + * @return a string with one of the given values described in the list above |
42 | 42 | * @since 1.0.0 |
43 | 43 | */ |
44 | | - public String getCode() { |
45 | | - return code; |
46 | | - } |
| 44 | + @NonNull |
| 45 | + public abstract String code(); |
47 | 46 |
|
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 |
58 | 48 |
|
59 | 49 | /** |
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. |
62 | 53 | * |
63 | | - * @return List of {@link DirectionsWaypoint} objects. |
| 54 | + * @return list of {@link DirectionsWaypoint} objects ordered from start of route till the end |
64 | 55 | * @since 1.0.0 |
65 | 56 | */ |
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(); |
80 | 59 |
|
81 | 60 | /** |
82 | 61 | * List containing all the different route options. It's ordered by descending recommendation |
83 | 62 | * rank. In other words, object 0 in the List is the highest recommended route. if you don't |
84 | 63 | * 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. |
86 | 65 | * |
87 | | - * @return List of {@link DirectionsRoute} objects. |
| 66 | + * @return list of {@link DirectionsRoute} objects |
88 | 67 | * @since 1.0.0 |
89 | 68 | */ |
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); |
92 | 74 | } |
93 | 75 |
|
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(); |
105 | 86 | } |
106 | 87 | } |
0 commit comments