forked from claygregory/google-places-api-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Another version of AutocompleteQueryOptions
and junit tests
- Loading branch information
Showing
3 changed files
with
96 additions
and
35 deletions.
There are no files selected for viewing
56 changes: 23 additions & 33 deletions
56
src/main/java/com/claygregory/api/google/places/AutocompleteQueryOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,44 @@ | ||
package com.claygregory.api.google.places; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.claygregory.common.data.geo.GeoLocation; | ||
import com.claygregory.common.util.StringUtil; | ||
|
||
public class AutocompleteQueryOptions { | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class AutocompleteQueryOptions extends HashMap<String, String> { | ||
|
||
private static final long serialVersionUID = -7290974229881633926L; | ||
private static final String LOCATION = "location"; | ||
|
||
private static final String RADIUS = "radius"; | ||
|
||
private static final String TYPES = "types"; | ||
|
||
private Map<String,String> params = new HashMap<String,String>( ); | ||
|
||
public static AutocompleteQueryOptions create( ) { | ||
return new AutocompleteQueryOptions( ); | ||
} | ||
|
||
public AutocompleteQueryOptions location( GeoLocation location ) { | ||
this.location( location.getLatitude( ), location.getLongitude( ) ); | ||
this.radius( location.getAccuracy( ) ); | ||
return this; | ||
} | ||
|
||
public AutocompleteQueryOptions location( float lat, float lon ) { | ||
return this.param( LOCATION, lat + "," + lon ); | ||
public AutocompleteQueryOptions() { | ||
super(); | ||
} | ||
|
||
public AutocompleteQueryOptions radius( Integer radius ) { | ||
return this.param( RADIUS, radius != null ? String.valueOf( radius ) : null ); | ||
|
||
public AutocompleteQueryOptions(GeoLocation location) { | ||
super(); | ||
assertNotNull(location); | ||
this.put(LOCATION, location.getLatitude() + "," + location.getLongitude()); | ||
Integer radius = location.getAccuracy(); | ||
this.put(RADIUS, radius != null ? String.valueOf(radius) : null); | ||
} | ||
public String param( String key ) { | ||
return this.params.get( key ); | ||
|
||
public void setTypes(String... types) { | ||
this.put(TYPES, StringUtil.join("|", types)); | ||
} | ||
|
||
public AutocompleteQueryOptions param( String key, String value ) { | ||
this.params.put( key, value ); | ||
return this; | ||
public String getLocation() { | ||
return this.get(LOCATION); | ||
} | ||
|
||
public Map<String,String> params( ) { | ||
return Collections.unmodifiableMap( this.params ); | ||
public String getRadius() { | ||
return this.get(RADIUS); | ||
} | ||
|
||
public AutocompleteQueryOptions types( String... types ) { | ||
return this.param( TYPES, StringUtil.join( "|", types ) ); | ||
public String getTypes() { | ||
return this.get(TYPES); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/java/com/claygregory/api/google/places/AutocompleteQueryOptionsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright (c) 2013 Víctor Martín Molina | ||
This file is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This file is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with this file. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.claygregory.api.google.places; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
import com.claygregory.common.data.geo.GeoLocation; | ||
|
||
public class AutocompleteQueryOptionsTests { | ||
|
||
@Test(expected = AssertionError.class) | ||
public void testAutocompleteQueryOptionsNull() { | ||
new AutocompleteQueryOptions(null); | ||
} | ||
|
||
@Test | ||
public void testAutocompleteQueryOptionsDefault() { | ||
GeoLocation geoLocation = new Place.Location(); | ||
AutocompleteQueryOptions autocompleteQueryOptions = new AutocompleteQueryOptions(geoLocation); | ||
assertEquals("0.0,0.0", autocompleteQueryOptions.getLocation()); | ||
assertNull(autocompleteQueryOptions.getRadius()); | ||
} | ||
|
||
@Test | ||
public void testAutocompleteQueryOptions() { | ||
GeoLocation geoLocation = new GeoLocation() { | ||
@Override | ||
public float getLongitude() { | ||
return 10.1f; | ||
} | ||
|
||
@Override | ||
public float getLatitude() { | ||
return 20.2f; | ||
} | ||
|
||
@Override | ||
public Integer getAccuracy() { | ||
return 10; | ||
} | ||
}; | ||
AutocompleteQueryOptions autocompleteQueryOptions = new AutocompleteQueryOptions(geoLocation); | ||
assertEquals("20.2,10.1", autocompleteQueryOptions.getLocation()); | ||
assertEquals("10", autocompleteQueryOptions.getRadius()); | ||
} | ||
|
||
@Test | ||
public void setTypes() { | ||
AutocompleteQueryOptions autocompleteQueryOptions = new AutocompleteQueryOptions(); | ||
autocompleteQueryOptions.setTypes("a", "b"); | ||
assertEquals("a|b", autocompleteQueryOptions.getTypes()); | ||
} | ||
|
||
} |