InetAddressComparator implements Comparator<InetAddress> { @Override public int compare(InetAddress l, InetAddress r) { byte[] la = l.getAddress(); byte[] ra = r.getAddress(); if(la.length < ra.length) return -1; else if(la.length > ra.length) return 1; else { for(int i=0; i<la.length; i++) { if(la[i] < ra[i]) return -1; else if(la[i] > ra[i]) return 1; } return 0; } }  @Override int compare(InetAddress l, InetAddress r);  }
@Test public void testEqualsIPv4IPv4() throws Exception { InetAddress a = InetAddress.getByAddress(new byte[] { (byte)0xc0, (byte)0xa8, (byte)0x01, (byte)0x01 }); InetAddress b = InetAddress.getByAddress(new byte[] { (byte)0xc0, (byte)0xa8, (byte)0x01, (byte)0x01 }); Assert.assertEquals(0, (new InetAddressComparator()).compare(a, b)); }
@Test public void testSmallerIPv4IPv4() throws Exception { InetAddress a = InetAddress.getByAddress(new byte[] { (byte)0xc0, (byte)0xa8, (byte)0x01, (byte)0x01 }); InetAddress b = InetAddress.getByAddress(new byte[] { (byte)0xc0, (byte)0xa8, (byte)0x01, (byte)0x02 }); Assert.assertTrue((new InetAddressComparator()).compare(a, b) < 0); }
@Test public void testLargerIPv4IPv4() throws Exception { InetAddress a = InetAddress.getByAddress(new byte[] { (byte)0xc0, (byte)0xa8, (byte)0x01, (byte)0x02 }); InetAddress b = InetAddress.getByAddress(new byte[] { (byte)0xc0, (byte)0xa8, (byte)0x01, (byte)0x01 }); Assert.assertTrue((new InetAddressComparator()).compare(a, b) > 0); }
@Test public void testEqualsIPv6IPv6() throws Exception { InetAddress a = InetAddress.getByAddress(new byte[] { (byte)0xfe, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x22, (byte)0x15, (byte)0xff, (byte)0xfe, (byte)0x85, (byte)0x0d, (byte)0x9e }); InetAddress b = InetAddress.getByAddress(new byte[] { (byte)0xfe, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x22, (byte)0x15, (byte)0xff, (byte)0xfe, (byte)0x85, (byte)0x0d, (byte)0x9e }); Assert.assertEquals(0, (new InetAddressComparator()).compare(a, b)); }
@Test public void testSmallerIPv6IPv6() throws Exception { InetAddress a = InetAddress.getByAddress(new byte[] { (byte)0xfe, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x22, (byte)0x15, (byte)0xff, (byte)0xfe, (byte)0x85, (byte)0x0d, (byte)0x9e }); InetAddress b = InetAddress.getByAddress(new byte[] { (byte)0xfe, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x22, (byte)0x15, (byte)0xff, (byte)0xfe, (byte)0x85, (byte)0x0d, (byte)0x9f }); Assert.assertTrue((new InetAddressComparator()).compare(a, b) < 0); }
@Test public void testLargerIPv6IPv6() throws Exception { InetAddress a = InetAddress.getByAddress(new byte[] { (byte)0xfe, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x22, (byte)0x15, (byte)0xff, (byte)0xfe, (byte)0x85, (byte)0x0d, (byte)0x9f }); InetAddress b = InetAddress.getByAddress(new byte[] { (byte)0xfe, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x22, (byte)0x15, (byte)0xff, (byte)0xfe, (byte)0x85, (byte)0x0d, (byte)0x9e }); Assert.assertTrue((new InetAddressComparator()).compare(a, b) > 0); }
@Test public void testSmallerIPv4IPv6() throws Exception { InetAddress a = InetAddress.getByAddress(new byte[] { (byte)0xc0, (byte)0xa8, (byte)0x01, (byte)0x01 }); InetAddress b = InetAddress.getByAddress(new byte[] { (byte)0xfe, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x22, (byte)0x15, (byte)0xff, (byte)0xfe, (byte)0x85, (byte)0x0d, (byte)0x9f }); Assert.assertTrue((new InetAddressComparator()).compare(a, b) < 0); }
@Test public void testLargerIPv6IPv4() throws Exception { InetAddress a = InetAddress.getByAddress(new byte[] { (byte)0xfe, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x22, (byte)0x15, (byte)0xff, (byte)0xfe, (byte)0x85, (byte)0x0d, (byte)0x9f }); InetAddress b = InetAddress.getByAddress(new byte[] { (byte)0xc0, (byte)0xa8, (byte)0x01, (byte)0x01 }); Assert.assertTrue((new InetAddressComparator()).compare(a, b) > 0); }