Permalink
Browse files

cleanup: introduce City class. Removes duplication and will help with…

… World View...
  • Loading branch information...
1 parent 234eb3f commit fbf1206f0d428e7b48388bee4ec74df08ce44641 @lacostej committed Jun 23, 2009
@@ -24,9 +24,10 @@
import android.util.Log;
import com.google.android.maps.GeoPoint;
-import com.mycitybikes.android.model.StationStatus;
+import com.mycitybikes.android.model.City;
import com.mycitybikes.android.model.StationInfoBuilder;
import com.mycitybikes.android.model.StationLocation;
+import com.mycitybikes.android.model.StationStatus;
import com.mycitybikes.android.util.AndroidUtils;
import com.mycitybikes.android.util.Utils;
@@ -43,7 +44,7 @@ public static void loadOsloBikeLocations(Context context,
try {
AssetManager assets = context.getAssets();
InputStream is = assets.open("oslo2.xml");
- loadStationLocationsAsset(is, stationLocations, "Oslo", "Norway");
+ loadStationLocationsAsset(is, stationLocations, Constants.CITY_OSLO);
} catch (Exception e) {
Log.e(Constants.TAG, "Failed to load Oslo bike station locations: "
@@ -56,8 +57,7 @@ public static void loadStockholmBikeLocations(Context context,
try {
AssetManager assets = context.getAssets();
InputStream is = assets.open("stockholm.xml");
- loadStationLocationsAsset(is, stationLocations, "Stockholm",
- "Sweeden");
+ loadStationLocationsAsset(is, stationLocations, Constants.CITY_STOCKHOLM);
} catch (Exception e) {
Log.e(Constants.TAG, "Failed to load Stokholm bike station locations: "
@@ -66,7 +66,7 @@ public static void loadStockholmBikeLocations(Context context,
}
static void loadStationLocationsAsset(InputStream is,
- List<StationLocation> stationLocations, String city, String country)
+ List<StationLocation> stationLocations, City city)
throws IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
@@ -119,7 +119,7 @@ static void loadStationLocationsAsset(InputStream is,
}
final StationLocation stationLocation = new StationLocation(id,
- city, country, description, longitude, latitude);
+ city, description, longitude, latitude);
stationLocation.setStationInfoBuilder(new StationInfoBuilder() {
@Override
@@ -285,7 +285,7 @@ public static void loadBarcelonaBikeLocations(Context context,
try {
loadBikeLocationsAndStatusFromKmlInPage(context, stationLocations,
"http://www.bicing.com/localizaciones/localizaciones.php",
- "Barcelona", "Spain");
+ Constants.CITY_BARCELONA);
} catch (Exception e) {
Log.e(Constants.TAG, "Failed to load Barcelona bike station locations: "
@@ -294,28 +294,26 @@ public static void loadBarcelonaBikeLocations(Context context,
}
public static void loadBikeLocationsAndStatusFromKmlInPage(Context context,
- List<StationLocation> stationLocations, String httpUrl, String city, String country) {
+ List<StationLocation> stationLocations, String httpUrl, City city) {
try {
InputStream is = Utils.readContent(httpUrl, 5000);
- loadBikeLocationsAndStatusFromKmlInPage(stationLocations, is, city,
- country);
+ loadBikeLocationsAndStatusFromKmlInPage(stationLocations, is, city);
} catch (Exception e) {
- Log.e(Constants.TAG, "Failed to load " + city + "," + country
- + " bike station locations: " + e.getMessage(), e);
+ Log.e(Constants.TAG, "Failed to load " + city + " bike station locations: " + e.getMessage(), e);
}
}
static void loadBikeLocationsAndStatusFromKmlInPage(
List<StationLocation> stationLocations, InputStream is,
- String city, String country) throws UnsupportedEncodingException {
+ City city) throws UnsupportedEncodingException {
String kml = extractKMLFromHtml(is);
Log.v(Constants.TAG, "Extracted KML: " + kml);
InputStream is2 = new ByteArrayInputStream(kml.getBytes("UTF-8"));
- parseKml(is2, stationLocations, city, country);
+ parseKml(is2, stationLocations, city);
}
static void parseKml(InputStream is,
- List<StationLocation> stationLocations, String city, String country) {
+ List<StationLocation> stationLocations, City city) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
@@ -377,7 +375,7 @@ static void parseKml(InputStream is,
continue;
}
final StationLocation stationLocation = new StationLocation(id,
- city, country, description, geoPoint);
+ city, description, geoPoint);
stationLocation.setStationInfoBuilder(new StationInfoBuilder() {
@@ -453,15 +451,15 @@ static String extractKMLFromHtml(InputStream is) {
public static void loadWashingtonBikeLocations(Context applicationContext,
List<StationLocation> stationLocations) {
try {
- parseWashington(Utils.readContent("https://www.smartbikedc.com/smartbike_locations.asp", 5000), stationLocations, "Washington DC", "USA");
+ parseWashington(Utils.readContent("https://www.smartbikedc.com/smartbike_locations.asp", 5000), stationLocations, Constants.CITY_WASHINGTON_DC);
} catch (Exception e) {
Log.e(Constants.TAG, "Failed to load Washington DC bike station locations: "
+ e.getMessage(), e);
}
}
- static void parseWashington(InputStream is, List<StationLocation> stationLocations, String city, String country) {
+ static void parseWashington(InputStream is, List<StationLocation> stationLocations, City city) {
String line = null;
BufferedReader r = null;
try {
@@ -517,7 +515,7 @@ static void parseWashington(InputStream is, List<StationLocation> stationLocatio
bikeStationStatus.setEmptyLocks(emptyLocks);
bikeStationStatus.setReadyBikes(readyBikes);
bikeStationStatus.setOnline(true);
- final StationLocation stationLocation = new StationLocation(id, city, country, description, longitude, latitude);
+ final StationLocation stationLocation = new StationLocation(id, city, description, longitude, latitude);
stationLocation.setStationInfoBuilder(new StationInfoBuilder() {
@Override
public String buildStationInfo() {
@@ -1,7 +1,15 @@
package com.mycitybikes.android;
+import com.google.android.maps.GeoPoint;
+import com.mycitybikes.android.model.City;
+
public class Constants {
public static final String TAG = "MyCityBikes";
+ public static final City CITY_OSLO = new City("Oslo", "Norway", new GeoPoint(59913820,10738741));
+ public static final City CITY_PARIS = new City("Paris", "France", new GeoPoint(59332789,18064487));
+ public static final City CITY_STOCKHOLM = new City("Stockholm", "Sweeden", new GeoPoint(48856667,2350987));
+ public static final City CITY_BARCELONA = new City("Barcelona", "Spain", new GeoPoint(41387918,2169918));
+ public static final City CITY_WASHINGTON_DC = new City("Washington DC", "USA", new GeoPoint(38892091,-77024055));
}
@@ -15,9 +15,10 @@
import android.content.Context;
import android.util.Log;
-import com.mycitybikes.android.model.StationStatus;
+import com.mycitybikes.android.model.City;
import com.mycitybikes.android.model.StationInfoBuilder;
import com.mycitybikes.android.model.StationLocation;
+import com.mycitybikes.android.model.StationStatus;
import com.mycitybikes.android.util.Utils;
public class JCDecaux {
@@ -26,7 +27,7 @@ public static void loadParisBikeLocations(Context context,
try {
InputStream is = Utils.readContent(
"http://www.velib.paris.fr/service/carto", 5000);
- loadParisBikeLocations(is, stationLocations, "Paris", "France");
+ loadParisBikeLocations(is, stationLocations, Constants.CITY_PARIS);
} catch (Exception e) {
Log.e(Constants.TAG,
"Failed to load Paris bike station locations: "
@@ -35,7 +36,7 @@ public static void loadParisBikeLocations(Context context,
}
static void loadParisBikeLocations(InputStream is,
- List<StationLocation> stationLocations, String city, String country) {
+ List<StationLocation> stationLocations, City city) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
@@ -90,7 +91,7 @@ static void loadParisBikeLocations(InputStream is,
// markerAttributes.getNamedItem("bonus");
final StationLocation stationLocation = new StationLocation(id,
- city, country, description, longitude, latitude);
+ city, description, longitude, latitude);
stationLocation.setStationInfoBuilder(new StationInfoBuilder() {
@@ -0,0 +1,33 @@
+package com.mycitybikes.android.model;
+
+import com.google.android.maps.GeoPoint;
+
+public class City {
+ private final String name;
+ private final String country;
+ private final GeoPoint location;
+
+ public City(String name, String country, GeoPoint location) {
+ this.name = name;
+ this.country = country;
+ this.location = location;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getCountry() {
+ return country;
+ }
+
+ public GeoPoint getLocation() {
+ return location;
+ }
+
+ @Override
+ public String toString() {
+ return new String(name + "," + country + "[" + location + "]");
+ }
+
+}
@@ -6,22 +6,20 @@
public class StationLocation {
private final GeoPoint location;
- private final String city;
- private final String country;
+ private final City city;
private final String description;
private final int id;
private StationInfoBuilder stationInfoBuilder;
- public StationLocation(int id, String city, String country,
+ public StationLocation(int id, City city,
String description, double longitude, double latitude) {
- this(id, city, country, description, AndroidUtils.buildGeoPoint(latitude, longitude));
+ this(id, city, description, AndroidUtils.buildGeoPoint(latitude, longitude));
}
- public StationLocation(int id, String city, String country,
+ public StationLocation(int id, City city,
String description, GeoPoint geoPoint) {
this.id = id;
this.city = city;
- this.country = country;
this.location = geoPoint;
this.description = description;
}
@@ -31,11 +29,11 @@ public void setStationInfoBuilder(StationInfoBuilder stationInfoBuilder) {
}
public String getCity() {
- return city;
+ return city.getName();
}
public String getCountry() {
- return country;
+ return city.getCountry();
}
public GeoPoint getLocation() {
@@ -57,6 +55,6 @@ public int getId() {
@Override
public String toString() {
return new String("[" + location + "] " + description
- + " [" + country + "," + city + "-" + id + "]");
+ + " [" + getCountry() + "," + getCity() + "-" + id + "]");
}
}
@@ -37,8 +37,7 @@
public void testLoadOsloBikeLocations() throws IOException {
List<StationLocation> bikeLocations = new ArrayList<StationLocation>();
InputStream is = getStringInputStream(OSLO_XML);
- ClearChannel.loadStationLocationsAsset(is, bikeLocations, "Oslo",
- "Norway");
+ ClearChannel.loadStationLocationsAsset(is, bikeLocations, Constants.CITY_OSLO);
assertEquals(3, bikeLocations.size());
assertEquals(1, bikeLocations.get(0).getId());
@@ -85,7 +84,7 @@ public void testLoadBarcelonaBikeLocations_encoding() throws Exception {
InputStream is = getResource("tests/localizaciones_bclna_catalan.html");
- ClearChannel.loadBikeLocationsAndStatusFromKmlInPage(bikeLocations, is, "Barcelona", "Spain");
+ ClearChannel.loadBikeLocationsAndStatusFromKmlInPage(bikeLocations, is, Constants.CITY_BARCELONA);
assertTrue(bikeLocations.size() > 0);
assertTrue(bikeLocations.get(5).getDescription().startsWith("Pg Lluís Companys"));
@@ -105,7 +104,7 @@ public void testParseWashington() throws Exception {
List<StationLocation> stationLocations = new ArrayList<StationLocation>();
- ClearChannel.parseWashington(is, stationLocations, "Washington DC", "USA");
+ ClearChannel.parseWashington(is, stationLocations, Constants.CITY_WASHINGTON_DC);
assertEquals(10, stationLocations.size());
@@ -121,7 +120,7 @@ public void testParseKMLCatalan() throws Exception {
List<StationLocation> stationLocations = new ArrayList<StationLocation>();
// Debug.startMethodTracing("unitTest-myCityBikes");
- ClearChannel.parseKml(is, stationLocations, "Barcelona", "Spain");
+ ClearChannel.parseKml(is, stationLocations, Constants.CITY_BARCELONA);
//Debug.stopMethodTracing();
assertEquals(412, stationLocations.size());
@@ -66,7 +66,7 @@
public void testLoadParisBikeLocations() throws IOException {
List<StationLocation> bikeLocations = new ArrayList<StationLocation>();
InputStream is = getStringInputStream(PARIS_CARTO_XML);
- JCDecaux.loadParisBikeLocations(is, bikeLocations, "Paris", "France");
+ JCDecaux.loadParisBikeLocations(is, bikeLocations, Constants.CITY_PARIS);
assertEquals(2, bikeLocations.size());
assertEquals(20020, bikeLocations.get(0).getId());

0 comments on commit fbf1206

Please sign in to comment.