From 356f745092598308a0713d06a3743faa62757101 Mon Sep 17 00:00:00 2001 From: Aman Harishkumar Desai Date: Mon, 27 Nov 2023 20:22:41 -0400 Subject: [PATCH] fix design smells --- .../java/com/google/maps/DirectionsApi.java | 25 ---------- .../com/google/maps/DirectionsApiRequest.java | 26 +++++++++++ .../com/google/maps/PendingResultBase.java | 28 +++++++---- .../google/maps/PlaceAutocompleteRequest.java | 17 +------ .../com/google/maps/DirectionsApiTest.java | 46 +++++++++---------- 5 files changed, 69 insertions(+), 73 deletions(-) diff --git a/src/main/java/com/google/maps/DirectionsApi.java b/src/main/java/com/google/maps/DirectionsApi.java index 8f8e9d63..28426714 100644 --- a/src/main/java/com/google/maps/DirectionsApi.java +++ b/src/main/java/com/google/maps/DirectionsApi.java @@ -38,31 +38,6 @@ public class DirectionsApi { private DirectionsApi() {} - /** - * Creates a new DirectionsApiRequest using the given context, with all attributes at their - * default values. - * - * @param context Context that the DirectionsApiRequest will be executed against - * @return A newly constructed DirectionsApiRequest between the given points. - */ - public static DirectionsApiRequest newRequest(GeoApiContext context) { - return new DirectionsApiRequest(context); - } - - /** - * Creates a new DirectionsApiRequest between the given origin and destination, using the defaults - * for all other options. - * - * @param context Context that the DirectionsApiRequest will be executed against - * @param origin Origin address as text - * @param destination Destination address as text - * @return A newly constructed DirectionsApiRequest between the given points. - */ - public static DirectionsApiRequest getDirections( - GeoApiContext context, String origin, String destination) { - return new DirectionsApiRequest(context).origin(origin).destination(destination); - } - /** * Directions may be calculated that adhere to certain restrictions. This is configured by calling * {@link com.google.maps.DirectionsApiRequest#avoid} or {@link diff --git a/src/main/java/com/google/maps/DirectionsApiRequest.java b/src/main/java/com/google/maps/DirectionsApiRequest.java index a2b5645e..84bb5085 100644 --- a/src/main/java/com/google/maps/DirectionsApiRequest.java +++ b/src/main/java/com/google/maps/DirectionsApiRequest.java @@ -32,6 +32,31 @@ public DirectionsApiRequest(GeoApiContext context) { super(context, DirectionsApi.API_CONFIG, DirectionsApi.Response.class); } + /** + * Creates a new DirectionsApiRequest using the given context, with all attributes at their + * default values. + * + * @param context Context that the DirectionsApiRequest will be executed against + * @return A newly constructed DirectionsApiRequest between the given points. + */ + public static DirectionsApiRequest newRequest(GeoApiContext context) { + return new DirectionsApiRequest(context); + } + + /** + * Creates a new DirectionsApiRequest between the given origin and destination, using the defaults + * for all other options. + * + * @param context Context that the DirectionsApiRequest will be executed against + * @param origin Origin address as text + * @param destination Destination address as text + * @return A newly constructed DirectionsApiRequest between the given points. + */ + public static DirectionsApiRequest getDirections( + GeoApiContext context, String origin, String destination) { + return new DirectionsApiRequest(context).origin(origin).destination(destination); + } + @Override protected void validateRequest() { if (!params().containsKey("origin")) { @@ -350,6 +375,7 @@ public String prefixPlaceId(String placeId) { public static class Waypoint { /** The location of this waypoint, expressed as an API-recognized location. */ private String location; + /** Whether this waypoint is a stopover waypoint. */ private boolean isStopover; diff --git a/src/main/java/com/google/maps/PendingResultBase.java b/src/main/java/com/google/maps/PendingResultBase.java index bff5daf1..574cf0d7 100644 --- a/src/main/java/com/google/maps/PendingResultBase.java +++ b/src/main/java/com/google/maps/PendingResultBase.java @@ -15,12 +15,15 @@ package com.google.maps; +import static com.google.maps.internal.StringJoin.join; + import com.google.maps.errors.ApiException; import com.google.maps.internal.ApiConfig; import com.google.maps.internal.ApiResponse; import com.google.maps.internal.HttpHeaders; import com.google.maps.internal.StringJoin; import com.google.maps.internal.StringJoin.UrlValue; +import com.google.maps.model.ComponentFilter; import java.io.IOException; import java.util.*; @@ -141,7 +144,8 @@ protected A param(String key, UrlValue val) { } protected A paramAddToList(String key, String val) { - // Multiple parameter values required to support Static Maps API paths and markers. + // Multiple parameter values required to support Static Maps API paths and + // markers. if (params.get(key) == null) { params.put(key, new ArrayList()); } @@ -175,9 +179,9 @@ public final A language(String language) { /** * A channel to pass with the request. channel is used by Google Maps API for Work users to be - * able to track usage across different applications with the same clientID. See Premium Plan - * Usage Rates and Limits. + * able to track usage across different applications with the same clientID. See Premium Plan Usage + * Rates and Limits. * * @param channel String to pass with the request for analytics. * @return Returns the request for call chaining. @@ -187,13 +191,17 @@ public A channel(String channel) { } /** - * Custom parameter. For advanced usage only. + * Sets the component filters. Each component filter consists of a component:value pair and will + * fully restrict the results from the geocoder. * - * @param parameter The name of the custom parameter. - * @param value The value of the custom parameter. - * @return Returns the request for call chaining. + *

For more information see + * Component Filtering. + * + * @param filters Component filters to apply to the request. + * @return Returns this for call chaining. */ - public A custom(String parameter, String value) { - return param(parameter, value); + public A components(ComponentFilter... filters) { + return param("components", join('|', filters)); } } diff --git a/src/main/java/com/google/maps/PlaceAutocompleteRequest.java b/src/main/java/com/google/maps/PlaceAutocompleteRequest.java index 94155912..71ce2558 100644 --- a/src/main/java/com/google/maps/PlaceAutocompleteRequest.java +++ b/src/main/java/com/google/maps/PlaceAutocompleteRequest.java @@ -15,7 +15,6 @@ package com.google.maps; -import static com.google.maps.internal.StringJoin.join; import com.google.gson.FieldNamingPolicy; import com.google.maps.errors.ApiException; @@ -23,15 +22,14 @@ import com.google.maps.internal.ApiResponse; import com.google.maps.internal.StringJoin.UrlValue; import com.google.maps.model.AutocompletePrediction; -import com.google.maps.model.ComponentFilter; import com.google.maps.model.LatLng; import com.google.maps.model.PlaceAutocompleteType; import java.io.Serializable; import java.util.UUID; /** - * A Place + * A Place * Autocomplete request. */ public class PlaceAutocompleteRequest @@ -135,17 +133,6 @@ public PlaceAutocompleteRequest types(PlaceAutocompleteType types) { return param("types", types); } - /** - * A grouping of places to which you would like to restrict your results. Currently, you can use - * components to filter by country. - * - * @param filters The component filter to restrict results with. - * @return Returns this {@code PlaceAutocompleteRequest} for call chaining. - */ - public PlaceAutocompleteRequest components(ComponentFilter... filters) { - return param("components", join('|', filters)); - } - /** * StrictBounds returns only those places that are strictly within the region defined by location * and radius. This is a restriction, rather than a bias, meaning that results outside this region diff --git a/src/test/java/com/google/maps/DirectionsApiTest.java b/src/test/java/com/google/maps/DirectionsApiTest.java index af72752f..0d73214e 100644 --- a/src/test/java/com/google/maps/DirectionsApiTest.java +++ b/src/test/java/com/google/maps/DirectionsApiTest.java @@ -50,7 +50,7 @@ public DirectionsApiTest() { public void testGetDirections() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext(getDirectionsResponse)) { DirectionsResult result = - DirectionsApi.getDirections(sc.context, "Sydney, AU", "Melbourne, AU").await(); + DirectionsApiRequest.getDirections(sc.context, "Sydney, AU", "Melbourne, AU").await(); assertNotNull(result); assertNotNull(result.toString(), "result.toString() succeeded"); @@ -78,7 +78,7 @@ public void testGetDirections() throws Exception { public void testBuilder() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext(builderResponse)) { DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .mode(TravelMode.BICYCLING) .avoid( DirectionsApi.RouteRestriction.HIGHWAYS, @@ -113,7 +113,7 @@ public void testResponseTimesArePopulatedCorrectly() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext(responseTimesArePopulatedCorrectly)) { DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .mode(TravelMode.TRANSIT) .origin("483 George St, Sydney NSW 2000, Australia") .destination("182 Church St, Parramatta NSW 2150, Australia") @@ -141,7 +141,7 @@ public void testResponseTimesArePopulatedCorrectly() throws Exception { public void testTorontoToMontreal() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context).origin("Toronto").destination("Montreal").await(); + DirectionsApiRequest.newRequest(sc.context).origin("Toronto").destination("Montreal").await(); sc.assertParamValue("Toronto", "origin"); sc.assertParamValue("Montreal", "destination"); @@ -158,7 +158,7 @@ public void testTorontoToMontreal() throws Exception { public void testTorontoToMontrealByBicycleAvoidingHighways() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Toronto") .destination("Montreal") .avoid(DirectionsApi.RouteRestriction.HIGHWAYS) @@ -176,7 +176,7 @@ public void testTorontoToMontrealByBicycleAvoidingHighways() throws Exception { public void testSanFranciscoToSeattleByBicycleAvoidingIndoor() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("San Francisco") .destination("Seattle") .avoid(RouteRestriction.INDOOR) @@ -194,7 +194,7 @@ public void testSanFranciscoToSeattleByBicycleAvoidingIndoor() throws Exception public void testNewYorkToNewJerseyByAlternateRoute() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("New York") .destination("New Jersey") .alternatives(true) @@ -216,7 +216,7 @@ public void testNewYorkToNewJerseyByAlternateRoute() throws Exception { public void testBrooklynToQueensByTransit() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Brooklyn") .destination("Queens") .mode(TravelMode.TRANSIT) @@ -238,7 +238,7 @@ public void testBrooklynToQueensByTransit() throws Exception { public void testBostonToConcordViaCharlestownAndLexington() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Boston,MA") .destination("Concord,MA") .waypoints("Charlestown,MA", "Lexington,MA") @@ -254,7 +254,7 @@ public void testBostonToConcordViaCharlestownAndLexington() throws Exception { public void testBostonToNewJerseyViaNewHavenWithPlaceId() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Boston,US") .destination("New Jersey,US") .waypointsFromPlaceIds("G5c1s86wd2d") // This is a custom dummy place ID for New Haven,US @@ -276,7 +276,7 @@ public void testBostonToNewJerseyViaNewHavenWithPlaceId() throws Exception { public void testBostonToConcordViaCharlestownAndLexingtonNonStopover() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Boston,MA") .destination("Concord,MA") .waypoints( @@ -301,7 +301,7 @@ public void testBostonToConcordViaCharlestownAndLexingtonNonStopover() throws Ex public void testBostonToConcordViaCharlestownAndLexingtonLatLng() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Boston,MA") .destination("Concord,MA") .waypoints(new LatLng(42.379322, -71.063384), new LatLng(42.444303, -71.229087)) @@ -324,7 +324,7 @@ public void testBostonToConcordViaCharlestownAndLexingtonLatLng() throws Excepti public void testBostonToConcordViaCharlestownAndLexingtonLatLngNonStopoever() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Boston,MA") .destination("Concord,MA") .waypoints( @@ -348,7 +348,7 @@ public void testBostonToConcordViaCharlestownAndLexingtonLatLngNonStopoever() th public void testToledoToMadridInSpain() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Toledo") .destination("Madrid") .region("es") @@ -366,7 +366,7 @@ public void testLanguageParameter() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Toledo") .destination("Madrid") .region("es") @@ -388,7 +388,7 @@ public void testTrafficModel() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("48 Pirrama Road, Pyrmont NSW 2009") .destination("182 Church St, Parramatta NSW 2150") .mode(TravelMode.DRIVING) @@ -411,7 +411,7 @@ public void testTransitWithoutSpecifyingTime() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Fisherman's Wharf, San Francisco") .destination("Union Square, San Francisco") .mode(TravelMode.TRANSIT) @@ -431,7 +431,7 @@ public void testTransitParams() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("Fisherman's Wharf, San Francisco") .destination("Union Square, San Francisco") .mode(TravelMode.TRANSIT) @@ -456,7 +456,7 @@ public void testTravelModeWalking() throws Exception { try (LocalTestServerContext sc = new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) { DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .mode(TravelMode.WALKING) .origin("483 George St, Sydney NSW 2000, Australia") .destination("182 Church St, Parramatta NSW 2150, Australia") @@ -490,7 +490,7 @@ public void testNotFound() throws Exception { + " \"routes\" : [],\n" + " \"status\" : \"NOT_FOUND\"\n" + "}")) { - DirectionsApi.getDirections(sc.context, "fksjdhgf", "faldfdaf").await(); + DirectionsApiRequest.getDirections(sc.context, "fksjdhgf", "faldfdaf").await(); } } @@ -513,7 +513,7 @@ public void testGeocodedWaypoints() throws Exception { + " \"status\": \"OK\"\n" + "}")) { DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin("48 Pirrama Rd, Pyrmont NSW") .destination("Airport Dr, Sydney NSW") .mode(TravelMode.DRIVING) @@ -539,7 +539,7 @@ public void testOptimizeWaypointsBeforeWaypoints() throws Exception { LatLng origin = waypoints.get(0); LatLng destination = waypoints.get(1); DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin(origin) .destination(destination) .departureTime(Instant.now()) @@ -573,7 +573,7 @@ public void testOptimizeWaypointsAfterWaypoints() throws Exception { LatLng origin = waypoints.get(0); LatLng destination = waypoints.get(1); DirectionsResult result = - DirectionsApi.newRequest(sc.context) + DirectionsApiRequest.newRequest(sc.context) .origin(origin) .destination(destination) .departureTime(Instant.now())