@@ -19,7 +19,8 @@ public class CloudRegionRelative extends CloudRegion { | |||
public CloudRegionRelative(CloudRegion region) { copy(this, region); } | |||
@Getter @Setter private double distance; | |||
@Getter @Setter private Double distance; | |||
public boolean hasDistance () { return distance != null; } | |||
public void setDistance(double latitude, double longitude) { | |||
if (hasLocation()) distance = getLocation().distance(latitude, longitude); | |||
@@ -29,8 +30,14 @@ public class CloudRegionRelative extends CloudRegion { | |||
public static class CloudRegionRelativeComparator implements Comparator<CloudRegionRelative> { | |||
@Override public int compare(CloudRegionRelative crr1, CloudRegionRelative crr2) { | |||
final int diff = Double.compare(crr1.getDistance(), crr2.getDistance()); | |||
if (diff != 0) return diff; | |||
if (crr1.hasDistance() && crr2.hasDistance()) { | |||
final int diff = Double.compare(crr1.getDistance(), crr2.getDistance()); | |||
if (diff != 0) return diff; | |||
} else if (crr1.hasDistance()) { | |||
return -1; | |||
} else if (crr2.hasDistance()) { | |||
return 1; | |||
} | |||
return crr1.getInternalName().compareTo(crr2.getInternalName()); | |||
} | |||
} | |||
@@ -32,8 +32,7 @@ public class GeoLocation { | |||
public static final GeoLocation DEFAULT_GEO_LOCATION = GEO_ATLANTA; | |||
public static final GeoLocation NULL_LOCATION = new GeoLocation().setLat("-1.0").setLon("-1.0"); | |||
public static final double INVALID_LOCATION = -1.0; | |||
public static final GeoLocation NULL_LOCATION = new GeoLocation(); | |||
public GeoLocation(String country) { | |||
copy(this, NULL_LOCATION); | |||
@@ -63,11 +62,11 @@ public class GeoLocation { | |||
big(g2.getLon()).doubleValue()); | |||
} | |||
public double distance(double lat, double lon) { | |||
if (lat < 0 || lon < 0) return INVALID_LOCATION; | |||
public Double distance(double lat, double lon) { | |||
if (lat < 0 || lon < 0) return null; | |||
double thisLat = big(getLat()).doubleValue(); | |||
double thisLon = big(getLon()).doubleValue(); | |||
if (thisLat < 0 || thisLon < 0) return INVALID_LOCATION; | |||
if (thisLat < 0 || thisLon < 0) return null; | |||
return Haversine.distance( | |||
thisLat, | |||
lat, | |||
@@ -91,8 +90,7 @@ public class GeoLocation { | |||
public boolean hasLatLon() { | |||
try { | |||
distance(0, 0); | |||
return true; | |||
return distance(0, 0) != null; | |||
} catch (Exception e) { | |||
return false; | |||
} | |||
@@ -27,16 +27,16 @@ public interface RegionalServiceDriver { | |||
static List<CloudRegionRelative> findClosestRegions(BubbleConfiguration configuration, | |||
List<CloudService> clouds, | |||
BubbleFootprint footprint, | |||
double latitude, | |||
double longitude) { | |||
Double latitude, | |||
Double longitude) { | |||
return findClosestRegions(configuration, clouds, footprint, latitude, longitude, null, true); | |||
} | |||
static List<CloudRegionRelative> findClosestRegions(BubbleConfiguration configuration, | |||
List<CloudService> clouds, | |||
BubbleFootprint footprint, | |||
double latitude, | |||
double longitude, | |||
Double latitude, | |||
Double longitude, | |||
Collection<CloudAndRegion> exclude, | |||
boolean latLonIsValid) { | |||
@@ -60,7 +60,7 @@ public interface RegionalServiceDriver { | |||
addRegionWithUnknownDistance(allRegions, c, region); | |||
continue; | |||
} | |||
if (latitude == INVALID_LOCATION && longitude == INVALID_LOCATION) { | |||
if (latitude == null || longitude == null) { | |||
// region has a location, we can never match with invalid coordinates | |||
addRegionWithUnknownDistance(allRegions, c, region); | |||
continue; | |||
@@ -70,7 +70,7 @@ public interface RegionalServiceDriver { | |||
} | |||
final CloudRegionRelative r = new CloudRegionRelative(region); | |||
r.setCloud(c.getUuid()); | |||
if (latLonIsValid && latitude >= 0 && longitude >= 0) { | |||
if (latLonIsValid) { | |||
r.setDistance(latitude, longitude); | |||
} else { | |||
r.setDistance(DEFAULT_GEO_LOCATION.getLatitude(), DEFAULT_GEO_LOCATION.getLongitude()); | |||
@@ -87,7 +87,7 @@ public interface RegionalServiceDriver { | |||
CloudService c, | |||
CloudRegion region) { | |||
final CloudRegionRelative r = new CloudRegionRelative(region); | |||
r.setDistance(0); | |||
r.setDistance(null); | |||
r.setCloud(c.getUuid()); | |||
allRegions.add(r); | |||
} | |||
@@ -189,7 +189,8 @@ public class GeoService { | |||
// throw out any that are more than 50km off the average | |||
final List<GeoLocation> near = new ArrayList<>(); | |||
for (GeoLocation loc : resolved) { | |||
if (loc.distance(averageLat, averageLon) <= LOC_MAX_DISTANCE) { | |||
final Double distance = loc.distance(averageLat, averageLon); | |||
if (distance != null && distance <= LOC_MAX_DISTANCE) { | |||
near.add(loc); | |||
} | |||
} | |||