
CachedGeocodeResponse
By: a guest on
Aug 1st, 2012 | syntax:
Java | size: 1.99 KB | hits: 11 | expires: Never
package com.weather.dal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.json.JSONException;
import com.weather.dal.CachedGeocodeResponse.GeocodeResponseData;
/** A lightweight object for caching responses from the Google Geocoding service. It is important
* that this class have as small a footprint as possible. This class is intentionally scoped
* for package access.
* */
class CachedGeocodeResponse implements Iterable<GeocodeResponseData> {
private final List<GeocodeResponseData> responseList;
static class GeocodeResponseData {
private final String name;
private final Double lat;
private final Double lng;
GeocodeResponseData(String name, double lat, double lng) {
this.name = name;
this.lat = lat;
this.lng = lng;
}
String getName() {
return name;
}
Double getLat() {
return lat;
}
Double getLng() {
return lng;
}
}
private CachedGeocodeResponse(String jsonString) throws JSONException {
GoogleGeocodeResponse response = GoogleGeocodeResponse.newInstance(jsonString);
int count = Math.min(response.getCount(), SearchStringConnection.MAX_LOCATION_CHOICES);
responseList = new ArrayList<GeocodeResponseData>(count);
for (int i = 0, n = count; i < n; i++) {
String name = response.getNickname(i);
double lat = response.getLat(i);
double lng = response.getLong(i);
GeocodeResponseData data = new GeocodeResponseData(name, lat, lng);
responseList.add(data);
}
}
static CachedGeocodeResponse newInstance(String jsonString) throws JSONException {
return new CachedGeocodeResponse(jsonString);
}
public Iterator<GeocodeResponseData> iterator() {
return responseList.iterator();
}
}