NetworkLayerReachabilityInformation implements Serializable, Comparable<NetworkLayerReachabilityInformation> { @Override public int compareTo(NetworkLayerReachabilityInformation other) { CompareToBuilder builder = new CompareToBuilder(); builder.append(getPrefixLength(), other.getPrefixLength()); if(builder.toComparison() == 0) { int byteLen = calculateOctetsForPrefixLength(this.prefixLength); int otherByteLen = calculateOctetsForPrefixLength(other.prefixLength); int commonByteLen = (byteLen > otherByteLen) ? otherByteLen : byteLen; int commonPrefixLen = (prefixLength > other.prefixLength) ? other.prefixLength : prefixLength; for (int i = 0; i < commonByteLen - 1; i++) { builder.append(getPrefix()[i], other.getPrefix()[i]); } if(builder.toComparison() == 0) { int bitsToCheck = commonPrefixLen% 8; if (bitsToCheck == 0) { if(commonByteLen > 0) builder.append(getPrefix()[commonByteLen-1], other.getPrefix()[commonByteLen-1]); } else { for (int i = 0; i < bitsToCheck; i++) { int mask = 1 << (7 - i); builder.append(getPrefix()[commonByteLen - 1] & mask, other.getPrefix()[commonByteLen - 1] & mask); if(builder.toComparison() != 0) break; } } if(builder.toComparison() == 0) { builder.append(getPrefixLength(), other.getPrefixLength()); } } } return builder.toComparison(); } NetworkLayerReachabilityInformation();  NetworkLayerReachabilityInformation(int prefixLength, byte[] prefix);  NetworkLayerReachabilityInformation(InetAddress source); int getPrefixLength(); byte[] getPrefix(); void setPrefix(int prefixLength, byte[] prefix); boolean isPrefixOf(NetworkLayerReachabilityInformation other); @Override int hashCode(); @Override boolean equals(Object obj); @Override int compareTo(NetworkLayerReachabilityInformation other); static final int calculateOctetsForPrefixLength(int prefixLength); @Override String toString();  }
@Test public void testCompareEqualsFourBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(4, new byte[] { (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(4, new byte[] { (byte)0xc0 }); Assert.assertEquals(0, first.compareTo(second)); }
@Test public void testCompareEqualsGreaterBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(4, new byte[] { (byte)0xd0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(4, new byte[] { (byte)0xc0 }); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testCompareEqualsSmallerBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(4, new byte[] { (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(4, new byte[] { (byte)0xd0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testCompareEqualsTwelveBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0xf0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0xf0 }); Assert.assertEquals(0, first.compareTo(second)); }
@Test public void testCompareGreaterTwelveBitsFirst() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xd0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testCompareGreaterTwelveBitsSecond() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0xd0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testCompareSmallerTwelveBitsFirst() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xd0, (byte)0xc0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testCompareSmallerTwelveBitsSecond() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0x00 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0xd0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testCompareEqualsTwentyBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xf0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xf0, (byte)0xc0 }); Assert.assertEquals(0, first.compareTo(second)); }
@Test public void testCompareGreaterTwentyBitsFirst() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xd0, (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testCompareGreaterTwentyBitsSecond() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xd0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testCompareGreaterTwentyBitsThird() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xd0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testCompareSmallerTwentyBitsFirst() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xd0, (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testCompareSmallerTwentyBitsSecond() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xd0, (byte)0xc0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testCompareSmallerTwentyBitsThird() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xd0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testCompareSmallersFourFiveBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(4, new byte[] { (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(5, new byte[] { (byte)0xc0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testCompareGreaterFiveForBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(5, new byte[] { (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(4, new byte[] { (byte)0xc0 }); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testCompareSmallerTwelveThirteenBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(13, new byte[] { (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testCompareGreaterThirteenTwelveBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(13, new byte[] { (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(12, new byte[] { (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testCompareSmallerTwentyTwentyoneBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(21, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testCompareGreaterTwentyoneTwentyBits() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(21, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testZeroNonZero() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(0, null); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); Assert.assertEquals(-1, first.compareTo(second)); }
@Test public void testNonZeroZero() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(20, new byte[] { (byte)0xc0, (byte)0xc0, (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(0, null); Assert.assertEquals(1, first.compareTo(second)); }
@Test public void testZeroZero() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(0, null); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(0, null); Assert.assertEquals(0, first.compareTo(second)); }