|
| 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