Skip to content

Commit

Permalink
fix design smells
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanDesai10 committed Nov 28, 2023
1 parent 5173b5d commit 356f745
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 73 deletions.
25 changes: 0 additions & 25 deletions src/main/java/com/google/maps/DirectionsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/google/maps/DirectionsApiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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;

Expand Down
28 changes: 18 additions & 10 deletions src/main/java/com/google/maps/PendingResultBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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<String>());
}
Expand Down Expand Up @@ -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 <a
* href="https://developers.google.com/maps/documentation/business/clientside/quota">Premium Plan
* Usage Rates and Limits</a>.
* able to track usage across different applications with the same clientID. See <a href=
* "https://developers.google.com/maps/documentation/business/clientside/quota">Premium Plan Usage
* Rates and Limits</a>.
*
* @param channel String to pass with the request for analytics.
* @return Returns the request for call chaining.
Expand All @@ -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.
* <p>For more information see <a href=
* "https://developers.google.com/maps/documentation/geocoding/intro?hl=pl#ComponentFiltering">
* Component Filtering</a>.
*
* @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));
}
}
17 changes: 2 additions & 15 deletions src/main/java/com/google/maps/PlaceAutocompleteRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@

package com.google.maps;

import static com.google.maps.internal.StringJoin.join;

import com.google.gson.FieldNamingPolicy;
import com.google.maps.errors.ApiException;
import com.google.maps.internal.ApiConfig;
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 <a
* href="https://developers.google.com/places/web-service/autocomplete#place_autocomplete_requests">Place
* A <a href=
* "https://developers.google.com/places/web-service/autocomplete#place_autocomplete_requests">Place
* Autocomplete</a> request.
*/
public class PlaceAutocompleteRequest
Expand Down Expand Up @@ -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
Expand Down
46 changes: 23 additions & 23 deletions src/test/java/com/google/maps/DirectionsApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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");
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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))
Expand All @@ -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(
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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)
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 356f745

Please sign in to comment.