Skip to content

Instantly share code, notes, and snippets.

@desamtralized
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save desamtralized/e2e3a37a2e0b6602286a to your computer and use it in GitHub Desktop.

Select an option

Save desamtralized/e2e3a37a2e0b6602286a to your computer and use it in GitHub Desktop.
import com.google.gson.annotations.SerializedName;
public class AddressComponent {
@SerializedName("long_name")
private String longName;
@SerializedName("short_name")
private String shortName;
private String[] types;
public String getLongName() {
return longName;
}
public String getShortName() {
return shortName;
}
public String[] getTypes() {
return types;
}
}
import java.util.Arrays;
public class AddressComponentsUtils {
private static final String STATE = "administrative_area_level_1";
private static final String CITY = "administrative_area_level_2";
public enum NameOption {
SHORT, LONG
}
/**
* Returns a component based on a name and it's length.
*
* @param components the {@link com.globo.colaborativo.rest.google.model.AddressComponent}
* @param componentName name of the desired component
* @param nameOption if it is the short or long version of the component.
* @return a String with the desired component or empty if component was not found.
*/
private static String getComponentWithType(AddressComponent[] components, String componentName,
NameOption nameOption) {
String result = "";
for (AddressComponent component : components) {
if (Arrays.asList(component.getTypes()).contains(componentName)) {
switch (nameOption) {
case SHORT:
return component.getShortName();
case LONG:
return component.getLongName();
}
}
}
return result;
}
/**
* Returns the abbreviation for the state.
*/
public static String getUF(AddressComponent[] components) {
return getComponentWithType(components, STATE, NameOption.SHORT);
}
/**
* Returns the city.
*/
public static String getCity(AddressComponent[] components) {
return getComponentWithType(components, CITY, NameOption.SHORT);
}
/**
* Returns the long State name
*/
public static String getState(AddressComponent[] components) {
return getComponentWithType(components, STATE, NameOption.LONG);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment