commit ee43e6bc383dfd0899b31b6cbf651716145b8dbb Author: Karsten Loesing karsten.loesing@gmx.net Date: Fri Mar 21 15:44:12 2014 +0100
Turn latitude and longitude into floats. --- .../torproject/onionoo/DetailsDocumentWriter.java | 8 +++---- src/org/torproject/onionoo/LookupService.java | 21 +++++++++-------- src/org/torproject/onionoo/NodeStatus.java | 12 +++++----- test/org/torproject/onionoo/LookupServiceTest.java | 24 ++++++++++---------- 4 files changed, 33 insertions(+), 32 deletions(-)
diff --git a/src/org/torproject/onionoo/DetailsDocumentWriter.java b/src/org/torproject/onionoo/DetailsDocumentWriter.java index cea6ba3..309b80d 100644 --- a/src/org/torproject/onionoo/DetailsDocumentWriter.java +++ b/src/org/torproject/onionoo/DetailsDocumentWriter.java @@ -127,8 +127,8 @@ public class DetailsDocumentWriter implements DescriptorListener, String running = entry.getRunning() ? "true" : "false"; int dirPort = entry.getDirPort(); String countryCode = entry.getCountryCode(); - String latitude = entry.getLatitude(); - String longitude = entry.getLongitude(); + Float latitude = entry.getLatitude(); + Float longitude = entry.getLongitude(); String countryName = entry.getCountryName(); String regionName = entry.getRegionName(); String cityName = entry.getCityName(); @@ -173,10 +173,10 @@ public class DetailsDocumentWriter implements DescriptorListener, sb.append(",\n"country":"" + countryCode + """); } if (latitude != null) { - sb.append(",\n"latitude":" + latitude); + sb.append(String.format(",\n"latitude":%.4f", latitude)); } if (longitude != null) { - sb.append(",\n"longitude":" + longitude); + sb.append(String.format(",\n"longitude":%.4f", longitude)); } if (countryName != null) { sb.append(",\n"country_name":"" diff --git a/src/org/torproject/onionoo/LookupService.java b/src/org/torproject/onionoo/LookupService.java index a950f02..a968bc0 100644 --- a/src/org/torproject/onionoo/LookupService.java +++ b/src/org/torproject/onionoo/LookupService.java @@ -51,19 +51,19 @@ class LookupResult { return this.cityName; }
- private String latitude; - public void setLatitude(String latitude) { + private Float latitude; + public void setLatitude(Float latitude) { this.latitude = latitude; } - public String getLatitude() { + public Float getLatitude() { return this.latitude; }
- private String longitude; - public void setLongitude(String longitude) { + private Float longitude; + public void setLongitude(Float longitude) { this.longitude = longitude; } - public String getLongitude() { + public Float getLongitude() { return this.longitude; }
@@ -169,8 +169,8 @@ public class LookupService { /* Obtain a map from IP address numbers to blocks and to latitudes and longitudes. */ Map<Long, Long> addressNumberBlocks = new HashMap<Long, Long>(); - Map<Long, String[]> addressNumberLatLong = - new HashMap<Long, String[]>(); + Map<Long, Float[]> addressNumberLatLong = + new HashMap<Long, Float[]>(); try { SortedSet<Long> sortedAddressNumbers = new TreeSet<Long>( addressStringNumbers.values()); @@ -220,7 +220,8 @@ public class LookupService { addressNumberBlocks.put(addressNumber, blockNumber); if (parts[6].length() > 0 && parts[7].length() > 0) { addressNumberLatLong.put(addressNumber, - new String[] { parts[6], parts[7] }); + new Float[] { Float.parseFloat(parts[6]), + Float.parseFloat(parts[7]) }); } } } catch (NumberFormatException e) { @@ -374,7 +375,7 @@ public class LookupService { } } if (addressNumberLatLong.containsKey(addressNumber)) { - String[] latLong = addressNumberLatLong.get(addressNumber); + Float[] latLong = addressNumberLatLong.get(addressNumber); lookupResult.setLatitude(latLong[0]); lookupResult.setLongitude(latLong[1]); } diff --git a/src/org/torproject/onionoo/NodeStatus.java b/src/org/torproject/onionoo/NodeStatus.java index 8e89682..6fd33de 100644 --- a/src/org/torproject/onionoo/NodeStatus.java +++ b/src/org/torproject/onionoo/NodeStatus.java @@ -80,19 +80,19 @@ public class NodeStatus extends Document { return new TreeSet<String>(this.exitAddresses); }
- private String latitude; - public void setLatitude(String latitude) { + private Float latitude; + public void setLatitude(Float latitude) { this.latitude = latitude; } - public String getLatitude() { + public Float getLatitude() { return this.latitude; }
- private String longitude; - public void setLongitude(String longitude) { + private Float longitude; + public void setLongitude(Float longitude) { this.longitude = longitude; } - public String getLongitude() { + public Float getLongitude() { return this.longitude; }
diff --git a/test/org/torproject/onionoo/LookupServiceTest.java b/test/org/torproject/onionoo/LookupServiceTest.java index e12ab2a..23b5a91 100644 --- a/test/org/torproject/onionoo/LookupServiceTest.java +++ b/test/org/torproject/onionoo/LookupServiceTest.java @@ -98,7 +98,7 @@ public class LookupServiceTest { List<String> geoLite2CityLocationsLines, List<String> geoipASNum2Lines, String addressString, String countryCode, String countryName, String regionName, - String cityName, String latitude, String longitude, String aSNumber, + String cityName, Float latitude, Float longitude, String aSNumber, String aSName) { this.addressStrings.add(addressString); this.populateLines(); @@ -151,14 +151,14 @@ public class LookupServiceTest { this.lookupResults.get(addressString).getLatitude() == null); } else { assertEquals(latitude, - this.lookupResults.get(addressString).getLatitude()); + this.lookupResults.get(addressString).getLatitude(), 0.01); } if (longitude == null) { assertTrue(!this.lookupResults.containsKey(addressString) || this.lookupResults.get(addressString).getLongitude() == null); } else { assertEquals(longitude, - this.lookupResults.get(addressString).getLongitude()); + this.lookupResults.get(addressString).getLongitude(), 0.01); } if (aSNumber == null) { assertTrue(!this.lookupResults.containsKey(addressString) || @@ -189,22 +189,22 @@ public class LookupServiceTest { @Test() public void testLookup8888() { this.assertLookupResult(null, null, null, "8.8.8.8", "us", - "United States", "California", "Mountain View", "37.3860", - "-122.0838", "AS15169", "Google Inc."); + "United States", "California", "Mountain View", 37.3860f, + -122.0838f, "AS15169", "Google Inc."); }
@Test() public void testLookup8880() { this.assertLookupResult(null, null, null, "8.8.8.0", "us", - "United States", "California", "Mountain View", "37.3860", - "-122.0838", "AS15169", "Google Inc."); + "United States", "California", "Mountain View", 37.3860f, + -122.0838f, "AS15169", "Google Inc."); }
@Test() public void testLookup888255() { this.assertLookupResult(null, null, null, "8.8.8.255", "us", - "United States", "California", "Mountain View", "37.3860", - "-122.0838", "AS15169", "Google Inc."); + "United States", "California", "Mountain View", 37.3860f, + -122.0838f, "AS15169", "Google Inc."); }
@Test() @@ -253,7 +253,7 @@ public class LookupServiceTest { geoLite2CityLocationsLines.add("6252001,NA,"North America",US," + ""United States",,,,,"); this.assertLookupResult(null, geoLite2CityLocationsLines, null, - "8.8.8.8", null, null, null, null, "37.3860", "-122.0838", + "8.8.8.8", null, null, null, null, 37.3860f, -122.0838f, "AS15169", "Google Inc."); }
@@ -347,8 +347,8 @@ public class LookupServiceTest { geoipASNum2Lines.add("134744320,134750463,"AS3356 Level 3 " + "Communications""); this.assertLookupResult(null, null, geoipASNum2Lines, "8.8.8.8", "us", - "United States", "California", "Mountain View", "37.3860", - "-122.0838", null, null); + "United States", "California", "Mountain View", 37.3860f, + -122.0838f, null, null); }
@Test()