Skip to content

Commit

Permalink
Another version of AutocompleteQueryOptions
Browse files Browse the repository at this point in the history
and junit tests
  • Loading branch information
venosov committed Jul 15, 2013
1 parent c028fcf commit c7ead78
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 35 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public AutocompleteResult autocomplete( String input, AutocompleteQueryOptions o
.queryParam( "sensor", String.valueOf( sensor ) );

if ( options != null )
for ( String param : options.params( ).keySet( ) )
builder.queryParam( param, options.param( param ) );
for ( String param : options.keySet( ) )
builder.queryParam( param, options.get( param ) );

HttpGet get = new HttpGet( builder.buildURL( ).toString( ) );
return this.parseAutocompleteResponse( this.client.execute( get ) );
Expand Down
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());
}

}

0 comments on commit c7ead78

Please sign in to comment.