Skip to content

Commit ab23c5e

Browse files
authored
Add MaxSpeed Annotation to Directions API (#772)
* Initial annotation work * Finish maxspeed, add docs, adjust constant * Update javadoc * Update type adapter * Add builder * Add model tests * Add missing maxspeed builder * Add missing javadoc * Adjust doc line length
1 parent 2818ce8 commit ab23c5e

4 files changed

Lines changed: 185 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ public final class DirectionsCriteria {
115115
*/
116116
public static final String ANNOTATION_CONGESTION = "congestion";
117117

118+
/**
119+
* The posted speed limit, between each pair of coordinates.
120+
*
121+
* @since 2.1.0
122+
*/
123+
public static final String ANNOTATION_MAXSPEED = "maxspeed";
124+
118125
/**
119126
* Exclude all tolls along the returned directions route.
120127
*
@@ -255,7 +262,8 @@ private DirectionsCriteria() {
255262
ANNOTATION_CONGESTION,
256263
ANNOTATION_DISTANCE,
257264
ANNOTATION_DURATION,
258-
ANNOTATION_SPEED
265+
ANNOTATION_SPEED,
266+
ANNOTATION_MAXSPEED
259267
})
260268
public @interface AnnotationCriteria {
261269
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ public static Builder builder() {
5858
@Nullable
5959
public abstract List<Double> speed();
6060

61+
/**
62+
* The posted speed limit, between each pair of coordinates.
63+
* Maxspeed is only available for the `mapbox/driving` and `mapbox/driving-traffic`
64+
* profiles, other profiles will return `unknown`s only.
65+
*
66+
* @return a list with each entry being a {@link MaxSpeed} value between two of
67+
* the routeLeg geometry coordinates
68+
* @since 3.0.0
69+
*/
70+
@Nullable
71+
public abstract List<MaxSpeed> maxSpeed();
72+
6173
/**
6274
* The congestion between each pair of coordinates.
6375
*
@@ -117,6 +129,17 @@ public abstract static class Builder {
117129
*/
118130
public abstract Builder speed(@Nullable List<Double> speed);
119131

132+
/**
133+
* The posted speed limit, between each pair of coordinates.
134+
* Maxspeed is only available for the `mapbox/driving` and `mapbox/driving-traffic`
135+
* profiles, other profiles will return `unknown`s only.
136+
*
137+
* @param maxSpeed list of speeds between each pair of coordinates
138+
* @return this builder for chaining options together
139+
* @since 3.0.0
140+
*/
141+
public abstract Builder maxSpeed(@Nullable List<MaxSpeed> maxSpeed);
142+
120143
/**
121144
* The congestion between each pair of coordinates.
122145
*
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.mapbox.api.directions.v5.models;
2+
3+
import android.support.annotation.Nullable;
4+
5+
import com.google.auto.value.AutoValue;
6+
import com.google.gson.Gson;
7+
import com.google.gson.TypeAdapter;
8+
9+
import java.io.Serializable;
10+
11+
/**
12+
* Object representing max speeds along a route.
13+
*
14+
* @since 3.0.0
15+
*/
16+
@AutoValue
17+
public abstract class MaxSpeed implements Serializable {
18+
19+
/**
20+
* Create a new instance of this class by using the {@link Builder} class.
21+
*
22+
* @return {@link Builder} for creating a new instance
23+
* @since 3.0.0
24+
*/
25+
public static Builder builder() {
26+
return new AutoValue_MaxSpeed.Builder();
27+
}
28+
29+
/**
30+
* Number indicating the posted speed limit.
31+
*
32+
* @return number indicating the posted speed limit
33+
* @since 3.0.0
34+
*/
35+
@Nullable
36+
public abstract Integer speed();
37+
38+
/**
39+
* String indicating the unit of speed, either as `km/h` or `mph`.
40+
*
41+
* @return String unit either as `km/h` or `mph`
42+
* @since 3.0.0
43+
*/
44+
@Nullable
45+
public abstract String unit();
46+
47+
/**
48+
* Boolean is true if the speed limit is not known, otherwise null.
49+
*
50+
* @return Boolean true if speed limit is not known, otherwise null
51+
* @since 3.0.0
52+
*/
53+
@Nullable
54+
public abstract Boolean unknown();
55+
56+
/**
57+
* Boolean is `true` if the speed limit is unlimited, otherwise null.
58+
*
59+
* @return Boolean true if speed limit is unlimited, otherwise null
60+
* @since 3.0.0
61+
*/
62+
@Nullable
63+
public abstract Boolean none();
64+
65+
/**
66+
* Gson type adapter for parsing Gson to this class.
67+
*
68+
* @param gson the built {@link Gson} object
69+
* @return the type adapter for this class
70+
* @since 3.0.0
71+
*/
72+
public static TypeAdapter<MaxSpeed> typeAdapter(Gson gson) {
73+
return new AutoValue_MaxSpeed.GsonTypeAdapter(gson);
74+
}
75+
76+
/**
77+
* This builder can be used to set the values describing the {@link MaxSpeed}.
78+
*
79+
* @since 3.0.0
80+
*/
81+
@AutoValue.Builder
82+
public abstract static class Builder {
83+
84+
/**
85+
* Number indicating the posted speed limit.
86+
*
87+
* @param speed indicating the posted speed limit
88+
* @since 3.0.0
89+
*/
90+
public abstract Builder speed(@Nullable Integer speed);
91+
92+
/**
93+
* String indicating the unit of speed, either as `km/h` or `mph`.
94+
*
95+
* @param unit either as `km/h` or `mph`
96+
* @since 3.0.0
97+
*/
98+
public abstract Builder unit(@Nullable String unit);
99+
100+
/**
101+
* Boolean is true if the speed limit is not known, otherwise null.
102+
*
103+
* @param unknown true if speed limit is not known, otherwise null
104+
* @since 3.0.0
105+
*/
106+
public abstract Builder unknown(@Nullable Boolean unknown);
107+
108+
/**
109+
* Boolean is `true` if the speed limit is unlimited, otherwise null.
110+
*
111+
* @param none true if speed limit is unlimited, otherwise null
112+
* @since 3.0.0
113+
*/
114+
public abstract Builder none(@Nullable Boolean none);
115+
116+
/**
117+
* Build a new {@link MaxSpeed} object.
118+
*
119+
* @return a new {@link MaxSpeed} using the provided values in this builder
120+
* @since 3.0.0
121+
*/
122+
public abstract MaxSpeed build();
123+
}
124+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mapbox.api.directions.v5.models;
2+
3+
import com.mapbox.core.TestUtils;
4+
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNotNull;
9+
10+
public class MaxSpeedTest extends TestUtils {
11+
12+
@Test
13+
public void sanity() throws Exception {
14+
MaxSpeed maxSpeed = MaxSpeed.builder()
15+
.speed(65)
16+
.unit("mph")
17+
.build();
18+
assertNotNull(maxSpeed);
19+
}
20+
21+
@Test
22+
public void testSerializable() throws Exception {
23+
MaxSpeed maxSpeed = MaxSpeed.builder()
24+
.unknown(true)
25+
.build();
26+
byte[] serialized = TestUtils.serialize(maxSpeed);
27+
assertEquals(maxSpeed, deserialize(serialized, MaxSpeed.class));
28+
}
29+
}

0 commit comments

Comments
 (0)