|
| 1 | +package com.mapbox.api.directions.v5.models; |
| 2 | + |
| 3 | +import androidx.annotation.NonNull; |
| 4 | +import androidx.annotation.Nullable; |
| 5 | +import com.google.auto.value.AutoValue; |
| 6 | +import com.mapbox.api.directions.v5.DirectionsCriteria; |
| 7 | +import com.mapbox.api.directions.v5.utils.ParseUtils; |
| 8 | +import com.mapbox.geojson.Point; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Objects; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +/** |
| 17 | + * Defines excludes for {@link RouteOptions.Builder#excludeObject(Exclude)}. |
| 18 | + * |
| 19 | + * This class provides type-safe way to read and build {@link RouteOptions#exclude()} parameter. |
| 20 | + * |
| 21 | + * All properties are strictly categorized after parsing. Unknown data types or flags |
| 22 | + * are ignored. If you want to work with exclude criteria which is not yet supported, |
| 23 | + * consider using raw {@link RouteOptions#exclude()} directly. |
| 24 | + */ |
| 25 | +@AutoValue |
| 26 | +public abstract class Exclude { |
| 27 | + |
| 28 | + /** |
| 29 | + * Build a new instance of {@link Exclude}. Nothing is excluded by default. |
| 30 | + */ |
| 31 | + @NonNull |
| 32 | + public static Builder builder() { |
| 33 | + return new AutoValue_Exclude.Builder(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Exclude certain road types from routing. The default is to not exclude anything from the |
| 38 | + * profile selected. The following exclude flags are available for each profile: |
| 39 | + * <p> |
| 40 | + * {@link DirectionsCriteria#PROFILE_DRIVING}: One of {@link DirectionsCriteria#EXCLUDE_TOLL}, |
| 41 | + * {@link DirectionsCriteria#EXCLUDE_MOTORWAY}, or {@link DirectionsCriteria#EXCLUDE_FERRY}. |
| 42 | + * <p> |
| 43 | + * {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}: One of |
| 44 | + * {@link DirectionsCriteria#EXCLUDE_TOLL}, {@link DirectionsCriteria#EXCLUDE_MOTORWAY}, or |
| 45 | + * {@link DirectionsCriteria#EXCLUDE_FERRY}. |
| 46 | + * <p> |
| 47 | + * {@link DirectionsCriteria#PROFILE_WALKING}: No excludes supported |
| 48 | + * <p> |
| 49 | + * {@link DirectionsCriteria#PROFILE_CYCLING}: {@link DirectionsCriteria#EXCLUDE_FERRY} |
| 50 | + * |
| 51 | + * @return a list of strings matching one |
| 52 | + * of the {@link DirectionsCriteria.ExcludeCriteria} exclusions |
| 53 | + */ |
| 54 | + @Nullable |
| 55 | + public abstract List<String> criteria(); |
| 56 | + |
| 57 | + /** |
| 58 | + * Exclude certain points from routing. The default is to not exclude anything. |
| 59 | + * |
| 60 | + * @return a list of points excluded from the routing. |
| 61 | + */ |
| 62 | + @Nullable |
| 63 | + public abstract List<Point> points(); |
| 64 | + |
| 65 | + /** |
| 66 | + * Use this builder to build an {@link Exclude} object. |
| 67 | + */ |
| 68 | + @AutoValue.Builder |
| 69 | + public abstract static class Builder { |
| 70 | + |
| 71 | + /** |
| 72 | + * Exclude certain road types from routing. The default is to not exclude anything from the |
| 73 | + * profile selected. The following exclude flags are available for each profile: |
| 74 | + * <p> |
| 75 | + * {@link DirectionsCriteria#PROFILE_DRIVING}: One of {@link DirectionsCriteria#EXCLUDE_TOLL}, |
| 76 | + * {@link DirectionsCriteria#EXCLUDE_MOTORWAY}, or {@link DirectionsCriteria#EXCLUDE_FERRY}. |
| 77 | + * <p> |
| 78 | + * {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}: One of |
| 79 | + * {@link DirectionsCriteria#EXCLUDE_TOLL}, {@link DirectionsCriteria#EXCLUDE_MOTORWAY}, or |
| 80 | + * {@link DirectionsCriteria#EXCLUDE_FERRY}. |
| 81 | + * <p> |
| 82 | + * {@link DirectionsCriteria#PROFILE_WALKING}: No excludes supported |
| 83 | + * <p> |
| 84 | + * {@link DirectionsCriteria#PROFILE_CYCLING}: {@link DirectionsCriteria#EXCLUDE_FERRY} |
| 85 | + * |
| 86 | + * @param criteria a list of exclude criteria |
| 87 | + * matching {@link DirectionsCriteria.ExcludeCriteria} |
| 88 | + * @return this builder. |
| 89 | + */ |
| 90 | + public abstract Builder criteria(@Nullable List<String> criteria); |
| 91 | + |
| 92 | + /** |
| 93 | + * Exclude certain points from routing. The default is to not exclude anything. |
| 94 | + * |
| 95 | + * @param points a list of points to exclude. |
| 96 | + * @return this builder. |
| 97 | + */ |
| 98 | + public abstract Builder points(@Nullable List<Point> points); |
| 99 | + |
| 100 | + /** |
| 101 | + * Builds the object. |
| 102 | + * |
| 103 | + * @return a new instance of {@link Exclude} |
| 104 | + */ |
| 105 | + public abstract Exclude build(); |
| 106 | + } |
| 107 | + |
| 108 | + @Nullable |
| 109 | + static Exclude fromUrlQueryParameter(@Nullable String queryParameter) { |
| 110 | + if (queryParameter == null || Objects.equals(queryParameter, "")) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + ArrayList<Point> points = new ArrayList<>(); |
| 115 | + ArrayList<String> criteria = new ArrayList<>(); |
| 116 | + |
| 117 | + List<String> excludeItems = ParseUtils.parseToStrings(queryParameter, ","); |
| 118 | + if (excludeItems != null) { |
| 119 | + for (String excludeItem : excludeItems) { |
| 120 | + if (excludeItem.startsWith("point(") && excludeItem.endsWith(")")) { |
| 121 | + Point point = parsePointOrNull(excludeItem); |
| 122 | + if (point != null) { |
| 123 | + points.add(point); |
| 124 | + } |
| 125 | + } else if (VALID_EXCLUDE_CRITERIA.contains(excludeItem)) { |
| 126 | + criteria.add(excludeItem); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (criteria.size() == 0) { |
| 132 | + criteria = null; |
| 133 | + } |
| 134 | + if (points.size() == 0) { |
| 135 | + points = null; |
| 136 | + } |
| 137 | + if (criteria == null && points == null) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + return Exclude.builder() |
| 142 | + .criteria(criteria) |
| 143 | + .points(points) |
| 144 | + .build(); |
| 145 | + } |
| 146 | + |
| 147 | + @Nullable |
| 148 | + String toUrlQueryParameter() { |
| 149 | + if (points() == null && criteria() == null) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + StringBuilder result = new StringBuilder(); |
| 154 | + |
| 155 | + appendPoints(result); |
| 156 | + appendCriterias(result); |
| 157 | + |
| 158 | + return result.toString(); |
| 159 | + } |
| 160 | + |
| 161 | + private void appendCriterias(StringBuilder result) { |
| 162 | + if (criteria() != null) { |
| 163 | + for (String item : criteria()) { |
| 164 | + if (result.length() != 0) { |
| 165 | + result.append(','); |
| 166 | + } |
| 167 | + result.append(item); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private void appendPoints(StringBuilder result) { |
| 173 | + if (points() != null) { |
| 174 | + for (Point point : points()) { |
| 175 | + if (result.length() != 0) { |
| 176 | + result.append(","); |
| 177 | + } |
| 178 | + appendPoint(result, point); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + @Nullable |
| 184 | + private static Point parsePointOrNull(String point) { |
| 185 | + int delimiter = point.indexOf(' '); |
| 186 | + if (delimiter != -1) { |
| 187 | + String longitude = point.substring(6, delimiter); |
| 188 | + String latitude = point.substring(delimiter + 1, point.length() - 1); |
| 189 | + try { |
| 190 | + return Point.fromLngLat( |
| 191 | + Double.parseDouble(longitude), |
| 192 | + Double.parseDouble(latitude) |
| 193 | + ); |
| 194 | + } catch (NumberFormatException numberFormatError) { |
| 195 | + return null; |
| 196 | + } |
| 197 | + } else { |
| 198 | + return null; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private void appendPoint(StringBuilder result, Point point) { |
| 203 | + result.append("point(") |
| 204 | + .append(point.longitude()) |
| 205 | + .append(' ') |
| 206 | + .append(point.latitude()) |
| 207 | + .append(')'); |
| 208 | + } |
| 209 | + |
| 210 | + private static final Set<String> VALID_EXCLUDE_CRITERIA = new HashSet<String>() { |
| 211 | + { |
| 212 | + add(DirectionsCriteria.EXCLUDE_FERRY); |
| 213 | + add(DirectionsCriteria.EXCLUDE_MOTORWAY); |
| 214 | + add(DirectionsCriteria.EXCLUDE_TOLL); |
| 215 | + add(DirectionsCriteria.EXCLUDE_TUNNEL); |
| 216 | + add(DirectionsCriteria.EXCLUDE_RESTRICTED); |
| 217 | + add(DirectionsCriteria.EXCLUDE_CASH_ONLY_TOLLS); |
| 218 | + add(DirectionsCriteria.EXCLUDE_UNPAVED); |
| 219 | + } |
| 220 | + }; |
| 221 | +} |
0 commit comments