Don't like ads? PRO users don't see any ads ;-)
Guest

CachedGeocodeResponse

By: a guest on Aug 1st, 2012  |  syntax: Java  |  size: 1.99 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.weather.dal;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import org.json.JSONException;
  7. import com.weather.dal.CachedGeocodeResponse.GeocodeResponseData;
  8.  
  9. /** A lightweight object for caching responses from the Google Geocoding service. It is important
  10.  * that this class have as small a footprint as possible.  This class is intentionally scoped
  11.  * for package access.
  12.  * */
  13. class CachedGeocodeResponse implements Iterable<GeocodeResponseData> {
  14.  
  15.     private final List<GeocodeResponseData> responseList;
  16.    
  17.     static class GeocodeResponseData {
  18.         private final String name;
  19.         private final Double lat;
  20.         private final Double lng;
  21.        
  22.         GeocodeResponseData(String name, double lat, double lng) {
  23.             this.name = name;
  24.             this.lat = lat;
  25.             this.lng = lng;
  26.         }
  27.  
  28.         String getName() {
  29.             return name;
  30.         }
  31.  
  32.         Double getLat() {
  33.             return lat;
  34.         }
  35.  
  36.         Double getLng() {
  37.             return lng;
  38.         }
  39.     }
  40.    
  41.     private CachedGeocodeResponse(String jsonString) throws JSONException {
  42.         GoogleGeocodeResponse response = GoogleGeocodeResponse.newInstance(jsonString);
  43.         int count = Math.min(response.getCount(), SearchStringConnection.MAX_LOCATION_CHOICES);
  44.         responseList = new ArrayList<GeocodeResponseData>(count);
  45.         for (int i = 0, n = count; i < n; i++) {
  46.             String name = response.getNickname(i);
  47.             double lat = response.getLat(i);
  48.             double lng = response.getLong(i);
  49.             GeocodeResponseData data = new GeocodeResponseData(name, lat, lng);
  50.             responseList.add(data);
  51.         }
  52.     }
  53.    
  54.     static CachedGeocodeResponse newInstance(String jsonString) throws JSONException {
  55.         return new CachedGeocodeResponse(jsonString);
  56.     }
  57.  
  58.     public Iterator<GeocodeResponseData> iterator() {
  59.         return responseList.iterator();
  60.     }
  61.  
  62.  
  63. }