NetworkLayerReachabilityInformation implements Serializable, Comparable<NetworkLayerReachabilityInformation> { public boolean isPrefixOf(NetworkLayerReachabilityInformation other) { boolean isPrefix = false; if(prefixLength > 0) { if (other.prefixLength > 0 && other.prefixLength > this.prefixLength) { int byteLength = calculateOctetsForPrefixLength(this.prefixLength); boolean match = true; for (int i = 0; i < byteLength - 1; i++) { if (this.prefix[i] != other.prefix[i]) { match = false; break; } } if (match) { int bitsToCheck = this.prefixLength % 8; if (bitsToCheck == 0) { match = (this.prefix[byteLength - 1] == other.prefix[byteLength - 1]); } else { for (int i = 0; i < bitsToCheck; i++) { int mask = 1 << (7 - i); if ((this.prefix[byteLength - 1] & mask) != (other.prefix[byteLength - 1] & mask)) { match = false; break; } } } } isPrefix = match; } } else { isPrefix = (other.prefixLength > 0); } return isPrefix; } 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 testIsPrefixFourFive() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(4, new byte[] { (byte)0xa0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(5, new byte[] { (byte)0xa8 }); Assert.assertTrue(first.isPrefixOf(second)); Assert.assertFalse(second.isPrefixOf(first)); }
@Test public void testIsPrefixEightEight() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(8, new byte[] { (byte)0xa0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(8, new byte[] { (byte)0xa8 }); Assert.assertFalse(first.isPrefixOf(second)); Assert.assertFalse(second.isPrefixOf(first)); }
@Test public void testIsPrefixSevenEight() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(7, new byte[] { (byte)0xa0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(8, new byte[] { (byte)0xa8 }); Assert.assertFalse(first.isPrefixOf(second)); Assert.assertFalse(second.isPrefixOf(first)); }
@Test public void testIsPrefixEightSixteen() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(8, new byte[] { (byte)0xc8 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(16, new byte[] { (byte)0xc8, (byte)0xa8 }); Assert.assertTrue(first.isPrefixOf(second)); Assert.assertFalse(second.isPrefixOf(first)); }
@Test public void testIsNoPrefixEightSixteen() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(8, new byte[] { (byte)0xc0 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(16, new byte[] { (byte)0xc8, (byte)0xa8 }); Assert.assertFalse(first.isPrefixOf(second)); Assert.assertFalse(second.isPrefixOf(first)); }
@Test public void testIsPrefixThirtyThirtytwo() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(30, new byte[] { (byte)0xc8, (byte)0xa8, 0x09, 0x04 }); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(32, new byte[] { (byte)0xc8, (byte)0xa8, 0x09, 0x07 }); Assert.assertTrue(first.isPrefixOf(second)); Assert.assertFalse(second.isPrefixOf(first)); }
@Test public void testIsPrefixZeroEight() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(0, null); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(8, new byte[] { (byte)0xa8 }); Assert.assertTrue(first.isPrefixOf(second)); Assert.assertFalse(second.isPrefixOf(first)); }
@Test public void testIsPrefixZeroZero() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(0, null); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(0, null); Assert.assertFalse(first.isPrefixOf(second)); Assert.assertFalse(second.isPrefixOf(first)); }
@Test public void testIsPrefixZeroTwo() { NetworkLayerReachabilityInformation first = new NetworkLayerReachabilityInformation(0, null); NetworkLayerReachabilityInformation second = new NetworkLayerReachabilityInformation(2, new byte[] { (byte)0xc0 }); Assert.assertTrue(first.isPrefixOf(second)); Assert.assertFalse(second.isPrefixOf(first)); }