Skip to content

Commit 123e7cd

Browse files
authored
options refactoring, lists are used instead of strings. Tests updated (#1118)
1 parent 8d21722 commit 123e7cd

16 files changed

Lines changed: 2013 additions & 307 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private static String simpleMapboxDirectionsRequest() throws IOException {
3131
.destination(Point.fromLngLat(-95.3591, 29.7576))
3232
.overview(OVERVIEW_FULL)
3333
.profile(PROFILE_DRIVING_TRAFFIC)
34-
.annotations(ANNOTATION_CONGESTION).build();
34+
.addAnnotation(ANNOTATION_CONGESTION).build();
3535

3636
Response<DirectionsResponse> response = directions.executeCall();
3737
System.out.println("Directions response: " + response);

services-core/src/main/java/com/mapbox/core/utils/TextUtils.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ public static String formatCoordinate(double coordinate, int precision) {
9393
* @param radiuses a double array which represents the radius values
9494
* @return a String ready for being passed into the Retrofit call
9595
* @since 3.0.0
96+
* @deprecated use FormatUtils.formatRadiuses(List)
9697
*/
98+
@Deprecated
9799
public static String formatRadiuses(double[] radiuses) {
98100
if (radiuses == null || radiuses.length == 0) {
99101
return null;
@@ -118,7 +120,9 @@ public static String formatRadiuses(double[] radiuses) {
118120
* @param bearings a List of doubles representing bearing values
119121
* @return a string with the bearing values
120122
* @since 3.0.0
123+
* @deprecated use FormatUtils.formatBearing(List)
121124
*/
125+
@Deprecated
122126
public static String formatBearing(List<Double[]> bearings) {
123127
if (bearings.isEmpty()) {
124128
return null;
@@ -143,7 +147,9 @@ public static String formatBearing(List<Double[]> bearings) {
143147
* @param distributions the list of integer arrays representing the distribution
144148
* @return a string with the distribution values
145149
* @since 3.0.0
150+
* @deprecated use FormatUtils.formatDistributions(List)
146151
*/
152+
@Deprecated
147153
public static String formatDistributions(List<Integer[]> distributions) {
148154
if (distributions.isEmpty()) {
149155
return null;
@@ -170,13 +176,15 @@ public static String formatDistributions(List<Integer[]> distributions) {
170176
* @param approaches a string representing approaches to each coordinate.
171177
* @return a formatted string.
172178
* @since 3.2.0
179+
* @deprecated use FormatUtils.formatApproaches(List)
173180
*/
181+
@Deprecated
174182
public static String formatApproaches(String[] approaches) {
175183
for (int i = 0; i < approaches.length; i++) {
176184
if (approaches[i] == null) {
177185
approaches[i] = "";
178186
} else if (!approaches[i].equals("unrestricted")
179-
&& !approaches[i].equals("curb") && !approaches[i].isEmpty()) {
187+
&& !approaches[i].equals("curb") && !approaches[i].isEmpty()) {
180188
return null;
181189
}
182190
}
@@ -190,7 +198,9 @@ public static String formatApproaches(String[] approaches) {
190198
* @param waypointNames a string representing approaches to each coordinate.
191199
* @return a formatted string.
192200
* @since 3.3.0
201+
* @deprecated use FormatUtils.formatWaypointNames(List)
193202
*/
203+
@Deprecated
194204
public static String formatWaypointNames(String[] waypointNames) {
195205
for (int i = 0; i < waypointNames.length; i++) {
196206
if (waypointNames[i] == null) {

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

Lines changed: 560 additions & 107 deletions
Large diffs are not rendered by default.
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
package com.mapbox.api.directions.v5.utils;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
5+
import com.mapbox.geojson.Point;
6+
import java.text.DecimalFormat;
7+
import java.text.DecimalFormatSymbols;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Locale;
11+
12+
/**
13+
* Methods to convert models to Strings.
14+
*/
15+
public class FormatUtils {
16+
/**
17+
* Returns a string containing the tokens joined by delimiters. Doesn't remove trailing nulls.
18+
*
19+
* @param delimiter the delimiter on which to split.
20+
* @param tokens A list of objects to be joined. Strings will be formed from the objects by
21+
* calling object.toString().
22+
* @return {@link String}
23+
*/
24+
@Nullable
25+
public static String join(@NonNull CharSequence delimiter, @Nullable List<?> tokens) {
26+
return join(delimiter, tokens, false);
27+
}
28+
29+
/**
30+
* Returns a string containing the tokens joined by delimiters.
31+
*
32+
* @param delimiter the delimiter on which to split.
33+
* @param tokens A list of objects to be joined. Strings will be formed from the objects by
34+
* calling object.toString().
35+
* @param removeTrailingNulls true if trailing nulls should be removed.
36+
* @return {@link String}
37+
*/
38+
@Nullable
39+
public static String join(@NonNull CharSequence delimiter, @Nullable List<?> tokens,
40+
boolean removeTrailingNulls) {
41+
if (tokens == null || tokens.size() < 1) {
42+
return null;
43+
}
44+
45+
int lastNonNullToken = tokens.size() - 1;
46+
if (removeTrailingNulls) {
47+
for (int i = tokens.size() - 1; i >= 0; i--) {
48+
Object token = tokens.get(i);
49+
if (token != null) {
50+
break;
51+
} else {
52+
lastNonNullToken--;
53+
}
54+
}
55+
}
56+
57+
StringBuilder sb = new StringBuilder();
58+
boolean firstTime = true;
59+
for (int i = 0; i <= lastNonNullToken; i++) {
60+
if (firstTime) {
61+
firstTime = false;
62+
} else {
63+
sb.append(delimiter);
64+
}
65+
Object token = tokens.get(i);
66+
if (token != null) {
67+
sb.append(token);
68+
}
69+
}
70+
return sb.toString();
71+
}
72+
73+
/**
74+
* Useful to remove any trailing zeros and prevent a coordinate being over 7 significant figures.
75+
*
76+
* @param coordinate a double value representing a coordinate.
77+
* @return a formatted string.
78+
*/
79+
@NonNull
80+
public static String formatCoordinate(double coordinate) {
81+
DecimalFormat decimalFormat = new DecimalFormat("0.######",
82+
new DecimalFormatSymbols(Locale.US));
83+
return String.format(Locale.US, "%s",
84+
decimalFormat.format(coordinate));
85+
}
86+
87+
/**
88+
* Used in various APIs to format the user provided radiuses to a String matching the APIs
89+
* format.
90+
*
91+
* @param radiuses a list of doubles represents the radius values
92+
* @return a String ready for being passed into the Retrofit call
93+
*/
94+
@Nullable
95+
public static String formatRadiuses(@Nullable List<Double> radiuses) {
96+
if (radiuses == null || radiuses.size() == 0) {
97+
return null;
98+
}
99+
100+
List<String> radiusesToJoin = new ArrayList<>();
101+
for (Double radius : radiuses) {
102+
if (radius == null) {
103+
radiusesToJoin.add(null);
104+
} else if (radius == Double.POSITIVE_INFINITY) {
105+
radiusesToJoin.add("unlimited");
106+
} else {
107+
radiusesToJoin.add(String.format(Locale.US, "%s", formatCoordinate(radius)));
108+
}
109+
}
110+
return join(";", radiusesToJoin);
111+
}
112+
113+
/**
114+
* Formats the bearing variables from the raw values to a string which can than be used for the
115+
* request URL.
116+
*
117+
* @param bearings a List of list of doubles representing bearing values
118+
* @return a string with the bearing values
119+
*/
120+
@Nullable
121+
public static String formatBearings(@Nullable List<List<Double>> bearings) {
122+
if (bearings == null || bearings.isEmpty()) {
123+
return null;
124+
}
125+
126+
List<String> bearingsToJoin = new ArrayList<>();
127+
for (List<Double> bearing : bearings) {
128+
if (bearing == null || bearing.size() == 0) {
129+
bearingsToJoin.add(null);
130+
} else {
131+
bearingsToJoin.add(String.format(Locale.US, "%s,%s",
132+
formatCoordinate(bearing.get(0)),
133+
formatCoordinate(bearing.get(1))));
134+
}
135+
}
136+
return join(";", bearingsToJoin);
137+
}
138+
139+
/**
140+
* Converts the list of integer arrays to a string ready for API consumption.
141+
*
142+
* @param distributions the list of integer arrays representing the distribution
143+
* @return a string with the distribution values
144+
*/
145+
@Nullable
146+
public static String formatDistributions(@Nullable List<Integer[]> distributions) {
147+
if (distributions == null || distributions.isEmpty()) {
148+
return null;
149+
}
150+
151+
List<String> distributionsToJoin = new ArrayList<>();
152+
for (Integer[] array : distributions) {
153+
if (array.length == 0) {
154+
distributionsToJoin.add(null);
155+
} else {
156+
distributionsToJoin.add(String.format(Locale.US, "%s,%s",
157+
formatCoordinate(array[0]),
158+
formatCoordinate(array[1])));
159+
}
160+
}
161+
return join(";", distributionsToJoin);
162+
}
163+
164+
/**
165+
* Converts String list with approaches values to a string ready for API consumption. An approach
166+
* could be unrestricted, curb or null.
167+
*
168+
* @param approaches a list representing approaches to each coordinate.
169+
* @return a formatted string.
170+
*/
171+
@Nullable
172+
public static String formatApproaches(@Nullable List<String> approaches) {
173+
if (approaches == null || approaches.isEmpty()) {
174+
return null;
175+
}
176+
177+
for (String approach : approaches) {
178+
if (approach != null && !approach.equals("unrestricted") && !approach.equals("curb")
179+
&& !approach.isEmpty()) {
180+
return null;
181+
}
182+
}
183+
return join(";", approaches);
184+
}
185+
186+
/**
187+
* Converts String list with waypoint_names values to a string ready for API consumption.
188+
*
189+
* @param waypointNames a string representing approaches to each coordinate.
190+
* @return a formatted string.
191+
*/
192+
@Nullable
193+
public static String formatWaypointNames(@Nullable List<String> waypointNames) {
194+
if (waypointNames == null || waypointNames.isEmpty()) {
195+
return null;
196+
}
197+
198+
return join(";", waypointNames);
199+
}
200+
201+
/**
202+
* Converts a list of Points to String.
203+
*
204+
* @param coordinates a list of coordinates.
205+
* @return a formatted string.
206+
*/
207+
@Nullable
208+
public static String formatCoordinates(@NonNull List<Point> coordinates) {
209+
List<String> coordinatesToJoin = new ArrayList<>();
210+
for (Point point : coordinates) {
211+
coordinatesToJoin.add(String.format(Locale.US, "%s,%s",
212+
formatCoordinate(point.longitude()),
213+
formatCoordinate(point.latitude())));
214+
}
215+
216+
return join(";", coordinatesToJoin);
217+
}
218+
219+
/**
220+
* Converts array of Points with waypoint_targets values to a string ready for API consumption.
221+
*
222+
* @param points a list representing approaches to each coordinate.
223+
* @return a formatted string.
224+
*/
225+
@Nullable
226+
public static String formatPointsList(@Nullable List<Point> points) {
227+
if (points == null || points.isEmpty()) {
228+
return null;
229+
}
230+
231+
List<String> coordinatesToJoin = new ArrayList<>();
232+
for (Point point : points) {
233+
if (point == null) {
234+
coordinatesToJoin.add(null);
235+
} else {
236+
coordinatesToJoin.add(String.format(Locale.US, "%s,%s",
237+
formatCoordinate(point.longitude()),
238+
formatCoordinate(point.latitude())));
239+
}
240+
}
241+
return join(";", coordinatesToJoin);
242+
}
243+
}

0 commit comments

Comments
 (0)