GeoIP2PingERService extends BaseGeolocationService implements IGeolocationService, Closeable { @Override public final double[] getCoordinates(final String ip) { double[] result = coordinatesCache.getIfPresent(ip); if (result == null) { Location location; try { location = reader.city(InetAddress.getByName(ip)).getLocation(); result = new double[] { location.getLatitude(), location.getLongitude() }; } catch (UnknownHostException e) { String msg = "Invalid IP: " + Objects.toString(ip); CustomLog.logError(Level.SEVERE, msg, e); throw new IllegalArgumentException("Invalid IP", e); } catch (IOException e) { String msg = "Could not locate IP: " + Objects.toString(ip) + ", " + "because of I/O error:" + e.getMessage(); CustomLog.logError(Level.SEVERE, msg, e); throw new IllegalStateException(e); } catch (GeoIp2Exception e) { String msg = "Could not locate IP: " + Objects.toString(ip) + ", because " + e.getMessage(); CustomLog.logError(Level.FINER, msg, e); result = new double[] { Double.NaN, Double.NaN }; } coordinatesCache.put(ip, result); } return result; } GeoIP2PingERService(final InputStream geoIP2DB, final InputStream pingErRTT, final InputStream pingerMonitoringSites);  GeoIP2PingERService(final File geoIP2DB, final File pingErRTTFile, final File pingerMonitoringSitesFile);  GeoIP2PingERService(); @Override final double[] getCoordinates(final String ip); @Override IPMetadata getMetaData(final String ip); @Override void close(); @Override final double latency(final String ip1, final String ip2); @Override double latency(final double[] reqCoord1, final double[] reqCoord2); double weigthedAverage(final MinMaxPriorityQueue<PingERLatencyEntry> heap); void updateHeap(final MinMaxPriorityQueue<PingERLatencyEntry> heap, final PingERLatencyEntry qEntry); static void main(String[] args);  }
@Test(expected = IllegalArgumentException.class) public void testInvalidIPOutOfRange() { service.getCoordinates("1.256.3.4"); }
@Test public void testDistanceLondonNewYork() { double[] newYork = service.getCoordinates(NEW_YORK_IP); double[] london = service.getCoordinates(LONDON_IP); double distanceLondonToNewYorkKM = service.distance(london, newYork) / 1000; double distanceNewYorkToLondonKM = service.distance(newYork, london) / 1000; assertEquals(DISTANCE_LONDON_NEW_YORK_KM, distanceLondonToNewYorkKM, DISTANCE_COMPARISON_DELTA_KM); assertEquals(DISTANCE_LONDON_NEW_YORK_KM, distanceNewYorkToLondonKM, DISTANCE_COMPARISON_DELTA_KM); }
@Test public void testDistanceSydneyNewYork() { double[] sydney = service.getCoordinates(SYDNEY_IP); double[] newYork = service.getCoordinates(NEW_YORK_IP); double distanceSydneyToNewYorkKM = service.distance(sydney, newYork) / 1000; double distanceNewYorkToSydneyKM = service.distance(newYork, sydney) / 1000; assertEquals(DISTANCE_SYDNEY_NEW_YORK_KM, distanceSydneyToNewYorkKM, DISTANCE_COMPARISON_DELTA_KM); assertEquals(DISTANCE_SYDNEY_NEW_YORK_KM, distanceNewYorkToSydneyKM, DISTANCE_COMPARISON_DELTA_KM); }
@Test public void testDistanceSydneyLondon() { double[] sydney = service.getCoordinates(SYDNEY_IP); double[] london = service.getCoordinates(LONDON_IP); double distanceSydneyToLondonKM = service.distance(sydney, london) / 1000; double distanceLondonToSydneyKM = service.distance(london, sydney) / 1000; assertEquals(DISTANCE_SYDNEY_LONDON_KM, distanceSydneyToLondonKM, DISTANCE_COMPARISON_DELTA_KM); assertEquals(DISTANCE_SYDNEY_LONDON_KM, distanceLondonToSydneyKM, DISTANCE_COMPARISON_DELTA_KM); }
@Test public void testDistanceSydneyMelbourne() { double[] sydney = service.getCoordinates(SYDNEY_IP); double[] melbourne = service.getCoordinates(MELBOURNE_IP); double distanceSydneyToMelbourneKM = service.distance(sydney, melbourne) / 1000; double distanceMelbourneToSydneyKM = service.distance(melbourne, sydney) / 1000; assertEquals(DISTANCE_SYDNEY_MELBOURNE_KM, distanceSydneyToMelbourneKM, DISTANCE_COMPARISON_DELTA_KM); assertEquals(DISTANCE_SYDNEY_MELBOURNE_KM, distanceMelbourneToSydneyKM, DISTANCE_COMPARISON_DELTA_KM); }
@Test(expected = IllegalArgumentException.class) public void testInvalidIPMultipleAddresses() { service.getCoordinates("1.2.3.4.5"); }