HostEntryComparator { public static int compare(String s1, String s2) { if (isFullName(s1) && isFullName(s2)) { return netmaskCmp(s1, s2); } else if (isFullName(s1)) { return -1; } else if (isFullName(s2)) { return 1; } return maskCmp(s1, s2); }  static int compare(String s1, String s2);  }
@Test public void shouldReturnZeroIfBoughWitoutMask() { assertEquals(0, compare("a.b.c.d", "d.e.f.g")); }
@Test public void shouldReturnZeroIfBoughWitoutSameMask() { assertEquals(0, compare("a.b.c.d/24", "d.e.f.g/24")); }
@Test public void shouldBiggerMaskWin() { assertEquals(-1, compare("a.b.c.d/32", "d.e.f.g/24")); }
@Test public void shouldWithoutMaskSWin() { assertEquals(-1, compare("a.b.c.d", "d.e.f.g/24")); }
@Test public void shouldFirstWildcatWin() { assertEquals(1, compare("a*.somewhere.in.net", "ab*.somewhere.in.net")); assertEquals(-1, compare("ab*.somewhere.in.net", "a*.somewhere.in.net")); }
@Test public void shouldIpBeFirst() { assertEquals(1, compare("a*.somewhere.in.net", "a.b.c.d")); }
@Test public void shouldFullNameBeFirst() { assertEquals(-1, compare("a.somewhere.in.net", "*.somewhere.in.net")); }
@Test public void shouldReturnExpectedOrder() { List<String> hosts = Arrays.asList( "lab-*wn*.site.com", "lab-cwn01.site.com", "10.1.2.2", "fe80::5e26:aff:fe78:4866", "10.1.2.0/24" ); List<String> expected = Arrays.asList( "fe80::5e26:aff:fe78:4866", "10.1.2.2", "lab-cwn01.site.com", "10.1.2.0/24", "lab-*wn*.site.com" ); Comparator<String> hostComparator = (a,b) -> compare(a, b); Collections.sort(hosts, hostComparator); assertEquals(expected, hosts); }