@Test public void compare() throws Exception { UrlIpComparator demo = new UrlIpComparator(); URL a = new URL("test", "192.168.0.1", 0); URL b = new URL("test", "192.168.0.7", 0); Assert.assertTrue(demo.compare(a, b) < 0); a = new URL("test", "192.168.0.1", 0); b = new URL("test", "0.0.0.0", 0); Assert.assertTrue(demo.compare(a, b) > 0); a = new URL("test", null, 0); b = new URL("test", null, 0); Assert.assertEquals(0, demo.compare(a, b)); a = new URL("test", "192.168.0.1", 0); b = new URL("test", null, 0); Assert.assertTrue(demo.compare(a, b) > 0); a = new URL("test", null, 0); b = new URL("test", "192.168.0.7", 0); Assert.assertTrue(demo.compare(a, b) < 0); }
@Override public int compare(URL first, URL second) { if (null == first && null == second) { return 0; } else if (null == first) { return -1; } else if (null == second) { return 1; } String firstIp = first.getIp(); String secendIp = second.getIp(); if (firstIp == secendIp) { return 0; } if (firstIp != null && secendIp != null) { return firstIp.compareTo(secendIp); } else if (firstIp != null && secendIp == null) { return 1; } else if (firstIp == null && secendIp != null) { return -1; } return 0; }
UrlIpComparator implements Comparator<URL> { @Override public int compare(URL first, URL second) { if (null == first && null == second) { return 0; } else if (null == first) { return -1; } else if (null == second) { return 1; } String firstIp = first.getIp(); String secendIp = second.getIp(); if (firstIp == secendIp) { return 0; } if (firstIp != null && secendIp != null) { return firstIp.compareTo(secendIp); } else if (firstIp != null && secendIp == null) { return 1; } else if (firstIp == null && secendIp != null) { return -1; } return 0; } }
UrlIpComparator implements Comparator<URL> { @Override public int compare(URL first, URL second) { if (null == first && null == second) { return 0; } else if (null == first) { return -1; } else if (null == second) { return 1; } String firstIp = first.getIp(); String secendIp = second.getIp(); if (firstIp == secendIp) { return 0; } if (firstIp != null && secendIp != null) { return firstIp.compareTo(secendIp); } else if (firstIp != null && secendIp == null) { return 1; } else if (firstIp == null && secendIp != null) { return -1; } return 0; }  }
UrlIpComparator implements Comparator<URL> { @Override public int compare(URL first, URL second) { if (null == first && null == second) { return 0; } else if (null == first) { return -1; } else if (null == second) { return 1; } String firstIp = first.getIp(); String secendIp = second.getIp(); if (firstIp == secendIp) { return 0; } if (firstIp != null && secendIp != null) { return firstIp.compareTo(secendIp); } else if (firstIp != null && secendIp == null) { return 1; } else if (firstIp == null && secendIp != null) { return -1; } return 0; }  @Override int compare(URL first, URL second); }
UrlIpComparator implements Comparator<URL> { @Override public int compare(URL first, URL second) { if (null == first && null == second) { return 0; } else if (null == first) { return -1; } else if (null == second) { return 1; } String firstIp = first.getIp(); String secendIp = second.getIp(); if (firstIp == secendIp) { return 0; } if (firstIp != null && secendIp != null) { return firstIp.compareTo(secendIp); } else if (firstIp != null && secendIp == null) { return 1; } else if (firstIp == null && secendIp != null) { return -1; } return 0; }  @Override int compare(URL first, URL second);  }