/** * Used in various APIs to format the user provided radiuses to a String matching the APIs format. * * @param radiuses a double array which represents the radius values * @return a String ready for being passed into the Retrofit call * @since 3.0.0 */ public static String formatRadiuses(double[] radiuses) { if (radiuses == null || radiuses.length == 0) { return null; } String[] radiusesFormatted = new String[radiuses.length]; for (int i = 0; i < radiuses.length; i++) { if (radiuses[i] == Double.POSITIVE_INFINITY) { radiusesFormatted[i] = "unlimited"; } else { radiusesFormatted[i] = String.format(Locale.US, "%s", TextUtils.formatCoordinate(radiuses[i])); } } return join(";", radiusesFormatted); }
/** * Checks that the provided access token is not empty or null, and that it starts with * the right prefixes. Note that this method does not check Mapbox servers to verify that * it actually belongs to an account. * * @param accessToken A Mapbox access token. * @return true if the provided access token is valid, false otherwise. * @since 1.0.0 */ public static boolean isAccessTokenValid(String accessToken) { return !TextUtils.isEmpty(accessToken) && !(!accessToken.startsWith("pk.") && !accessToken.startsWith("sk.") && !accessToken.startsWith("tk.")); } }
+ " the number of waypoints provided."); final String waypointNamesStr = TextUtils.formatWaypointNames(waypointNames); waypointNames(waypointNamesStr); + "number of coordinates provided."); String formattedApproaches = TextUtils.formatApproaches(approaches); if (formattedApproaches == null) { throw new ServicesException("All approaches values must be one of curb, unrestricted"); bearing(TextUtils.formatBearing(bearings)); annotation(TextUtils.join(",", annotations)); radius(TextUtils.formatRadiuses(radiuses));
/** * Converts String array with waypoint_names values * to a string ready for API consumption. * * @param waypointNames a string representing approaches to each coordinate. * @return a formatted string. * @since 3.3.0 */ public static String formatWaypointNames(String[] waypointNames) { for (int i = 0; i < waypointNames.length; i++) { if (waypointNames[i] == null) { waypointNames[i] = ""; } } return TextUtils.join(";", waypointNames); } }
/** * This uses the provided parameters set using the {@link Builder} and first checks that all * values are valid, formats the values as strings for easier consumption by the API, and lastly * creates a new {@link MapboxOptimization} object with the values provided. * * @return a new instance of Mapbox Optimization * @since 2.1.0 */ public MapboxOptimization build() { if (coordinates == null || coordinates.size() < 2) { throw new ServicesException("At least two coordinates must be provided with your API" + "request."); } else if (coordinates.size() > 12) { throw new ServicesException("Maximum of 12 coordinates are allowed for this API."); } coordinates(formatCoordinates(coordinates)); bearings(TextUtils.formatBearing(bearings)); annotations(TextUtils.join(",", annotations)); radiuses(TextUtils.formatRadiuses(radiuses)); distributions(TextUtils.formatDistributions(distributions)); // Generate build so that we can check that values are valid. MapboxOptimization optimization = autoBuild(); if (!MapboxUtils.isAccessTokenValid(optimization.accessToken())) { throw new ServicesException("Using Mapbox Services requires setting a valid access token."); } return optimization; }
/** * The longitude and latitude to be queried. * * @param point query point * @return this builder for chaining options together * @since 3.5.0 */ public Builder query(@NonNull Point point) { query(String.format(Locale.US, "%s,%s", TextUtils.formatCoordinate(point.longitude()), TextUtils.formatCoordinate(point.latitude()))); String str = String.format(Locale.US, "%s,%s", TextUtils.formatCoordinate(point.longitude()), TextUtils.formatCoordinate(point.latitude())); return this; }
+ " the number of waypoints provided."); final String waypointNamesStr = TextUtils.formatWaypointNames(waypointNames); waypointNames(waypointNamesStr); + "number of coordinates provided."); String formattedApproaches = TextUtils.formatApproaches(approaches); if (formattedApproaches == null) { throw new ServicesException("All approaches values must be one of curb, unrestricted"); timestamps(TextUtils.join(";", timestamps)); annotations(TextUtils.join(",", annotations)); radiuses(TextUtils.join(";", radiuses)); waypoints(TextUtils.join(";", waypoints));
+ " the number of waypoints provided."); final String waypointNamesStr = TextUtils.formatWaypointNames(waypointNames); waypointNames(waypointNamesStr); + "number of coordinates provided."); String formattedApproaches = TextUtils.formatApproaches(approaches); if (formattedApproaches == null) { throw new ServicesException("All approaches values must be one of curb, unrestricted"); bearing(TextUtils.formatBearing(bearings)); annotation(TextUtils.join(",", annotations)); radius(TextUtils.formatRadiuses(radiuses));
/** * Converts String array with approaches values * to a string ready for API consumption. * An approache could be unrestricted, curb or null. * * @param approaches a string representing approaches to each coordinate. * @return a formatted string. * @since 3.2.0 */ public static String formatApproaches(String[] approaches) { for (int i = 0; i < approaches.length; i++) { if (approaches[i] == null) { approaches[i] = ""; } else if (!approaches[i].equals("unrestricted") && !approaches[i].equals("curb") && !approaches[i].isEmpty()) { return null; } } return TextUtils.join(";", approaches); }
/** * This uses the provided parameters set using the {@link Builder} and first checks that all * values are valid, formats the values as strings for easier consumption by the API, and lastly * creates a new {@link MapboxOptimization} object with the values provided. * * @return a new instance of Mapbox Optimization * @since 2.1.0 */ public MapboxOptimization build() { if (coordinates == null || coordinates.size() < 2) { throw new ServicesException("At least two coordinates must be provided with your API" + "request."); } else if (coordinates.size() > 12) { throw new ServicesException("Maximum of 12 coordinates are allowed for this API."); } coordinates(formatCoordinates(coordinates)); bearings(TextUtils.formatBearing(bearings)); annotations(TextUtils.join(",", annotations)); radiuses(TextUtils.formatRadiuses(radiuses)); distributions(TextUtils.formatDistributions(distributions)); // Generate build so that we can check that values are valid. MapboxOptimization optimization = autoBuild(); if (!MapboxUtils.isAccessTokenValid(optimization.accessToken())) { throw new ServicesException("Using Mapbox Services requires setting a valid access token."); } return optimization; }
/** * The longitude and latitude to be queried. * * @param point query point * @return this builder for chaining options together * @since 3.5.0 */ public Builder query(@NonNull Point point) { query(String.format(Locale.US, "%s,%s", TextUtils.formatCoordinate(point.longitude()), TextUtils.formatCoordinate(point.latitude()))); String str = String.format(Locale.US, "%s,%s", TextUtils.formatCoordinate(point.longitude()), TextUtils.formatCoordinate(point.latitude())); return this; }
+ " the number of waypoints provided."); final String waypointNamesStr = TextUtils.formatWaypointNames(waypointNames); waypointNames(waypointNamesStr); + "number of coordinates provided."); String formattedApproaches = TextUtils.formatApproaches(approaches); if (formattedApproaches == null) { throw new ServicesException("All approaches values must be one of curb, unrestricted"); timestamps(TextUtils.join(";", timestamps)); annotations(TextUtils.join(",", annotations)); radiuses(TextUtils.join(";", radiuses)); waypoints(TextUtils.join(";", waypoints));
private static String formatCoordinates(List<Point> coordinates) { List<String> coordinatesFormatted = new ArrayList<>(); for (Point point : coordinates) { coordinatesFormatted.add(String.format(Locale.US, "%s,%s", TextUtils.formatCoordinate(point.longitude()), TextUtils.formatCoordinate(point.latitude()))); } return TextUtils.join(";", coordinatesFormatted.toArray()); } }
+ " the number of waypoints provided."); final String waypointNamesStr = TextUtils.formatWaypointNames(waypointNames); waypointNames(waypointNamesStr); + "number of coordinates provided."); String formattedApproaches = TextUtils.formatApproaches(approaches); if (formattedApproaches == null) { throw new ServicesException("All approaches values must be one of curb, unrestricted"); bearing(TextUtils.formatBearing(bearings)); annotation(TextUtils.join(",", annotations)); radius(TextUtils.formatRadiuses(radiuses));
/** * This optionally specifies the desired response language for user queries. For forward * geocodes, results that match the requested language are favored over results in other * languages. If more than one language tag is supplied, text in all requested languages will be * returned. For forward geocodes with more than one language tag, only the first language will * be used to weight results. * <p> * Any valid IETF language tag can be submitted, and a best effort will be made to return * results in the requested language or languages, falling back first to similar and then to * common languages in the event that text is not available in the requested language. In the * event a fallback language is used, the language field will have a different value than the * one requested. * <p> * Translation availability varies by language and region, for a full list of supported regions, * see the link provided below. * * @param languages one or more locale's specifying the language you'd like results to support * @return this builder for chaining options together * @see <a href="https://www.mapbox.com/api-documentation/search/#language-coverage">Supported languages * </a> * @since 2.0.0 */ public Builder languages(Locale... languages) { String[] languageStrings = new String[languages.length]; for (int i = 0; i < languages.length; i++) { languageStrings[i] = languages[i].getLanguage(); } languages(TextUtils.join(",", languageStrings)); return this; }
/** * This uses the provided parameters set using the {@link Builder} and first checks that all * values are valid, formats the values as strings for easier consumption by the API, and lastly * creates a new {@link MapboxOptimization} object with the values provided. * * @return a new instance of Mapbox Optimization * @since 2.1.0 */ public MapboxOptimization build() { if (coordinates == null || coordinates.size() < 2) { throw new ServicesException("At least two coordinates must be provided with your API" + "request."); } else if (coordinates.size() > 12) { throw new ServicesException("Maximum of 12 coordinates are allowed for this API."); } coordinates(formatCoordinates(coordinates)); bearings(TextUtils.formatBearing(bearings)); annotations(TextUtils.join(",", annotations)); radiuses(TextUtils.formatRadiuses(radiuses)); distributions(TextUtils.formatDistributions(distributions)); // Generate build so that we can check that values are valid. MapboxOptimization optimization = autoBuild(); if (!MapboxUtils.isAccessTokenValid(optimization.accessToken())) { throw new ServicesException("Using Mapbox Services requires setting a valid access token."); } return optimization; }
/** * The longitude and latitude to be queried. * * @param point query point * @return this builder for chaining options together * @since 3.5.0 */ public Builder query(@NonNull Point point) { query(String.format(Locale.US, "%s,%s", TextUtils.formatCoordinate(point.longitude()), TextUtils.formatCoordinate(point.latitude()))); String str = String.format(Locale.US, "%s,%s", TextUtils.formatCoordinate(point.longitude()), TextUtils.formatCoordinate(point.latitude())); return this; }
+ " the number of waypoints provided."); final String waypointNamesStr = TextUtils.formatWaypointNames(waypointNames); waypointNames(waypointNamesStr); + "number of coordinates provided."); String formattedApproaches = TextUtils.formatApproaches(approaches); if (formattedApproaches == null) { throw new ServicesException("All approaches values must be one of curb, unrestricted"); timestamps(TextUtils.join(";", timestamps)); annotations(TextUtils.join(",", annotations)); radiuses(TextUtils.join(";", radiuses)); waypoints(TextUtils.join(";", waypoints));
/** * Computes a full user agent header of the form: * {@code MapboxJava/1.2.0 Mac OS X/10.11.5 (x86_64)}. * * @param clientAppName Application Name * @return {@link String} representing the header user agent * @since 1.0.0 */ public static String getHeaderUserAgent(@Nullable String clientAppName) { String osName = System.getProperty("os.name"); String osVersion = System.getProperty("os.version"); String osArch = System.getProperty("os.arch"); if (TextUtils.isEmpty(osName) || TextUtils.isEmpty(osVersion) || TextUtils.isEmpty(osArch)) { return Constants.HEADER_USER_AGENT; } else { return getHeaderUserAgent(clientAppName, osName, osVersion, osArch); } }
private static String formatCoordinates(List<Point> coordinates) { String[] coordinatesFormatted = new String[coordinates.size()]; int index = 0; for (Point point : coordinates) { coordinatesFormatted[index++] = String.format(Locale.US, "%s,%s", TextUtils.formatCoordinate(point.longitude()), TextUtils.formatCoordinate(point.latitude())); } return TextUtils.join(";", coordinatesFormatted); }