@Test public void testLongestCommonPrefix() { Prefix p1 = Prefix.parse("1.1.1.0/24"); assertThat(longestCommonPrefix(p1, p1), equalTo(Prefix.parse("1.1.1.0/24"))); p1 = Prefix.parse("1.1.1.0/24"); Prefix p2 = Prefix.parse("1.1.1.0/32"); assertThat(longestCommonPrefix(p1, p2), equalTo(p1)); p1 = Prefix.parse("1.1.1.0/32"); p2 = Prefix.parse("1.1.1.0/24"); assertThat(longestCommonPrefix(p1, p2), equalTo(p2)); p1 = Prefix.parse("1.1.1.1/24"); p2 = Prefix.parse("1.1.1.1/8"); assertThat(longestCommonPrefix(p1, p2), equalTo(Prefix.parse("1.0.0.0/8"))); p1 = Prefix.parse("0.0.0.0/32"); p2 = Prefix.parse("0.0.0.255/32"); assertThat(longestCommonPrefix(p1, p2), equalTo(Prefix.parse("0.0.0.0/24"))); }
public static Prefix longestCommonPrefix(Prefix p1, Prefix p2) { if (p1.containsPrefix(p2)) { return p1; } if (p2.containsPrefix(p1)) { return p2; } long l1 = p1.getStartIp().asLong(); long l2 = p2.getStartIp().asLong(); long oneAtFirstDifferentBit = Long.highestOneBit(l1 ^ l2); int lengthInCommon = MAX_PREFIX_LENGTH - 1 - Long.numberOfTrailingZeros(oneAtFirstDifferentBit); return Prefix.create(Ip.create(l1), lengthInCommon); }
Prefix implements Comparable<Prefix>, Serializable { public static Prefix longestCommonPrefix(Prefix p1, Prefix p2) { if (p1.containsPrefix(p2)) { return p1; } if (p2.containsPrefix(p1)) { return p2; } long l1 = p1.getStartIp().asLong(); long l2 = p2.getStartIp().asLong(); long oneAtFirstDifferentBit = Long.highestOneBit(l1 ^ l2); int lengthInCommon = MAX_PREFIX_LENGTH - 1 - Long.numberOfTrailingZeros(oneAtFirstDifferentBit); return Prefix.create(Ip.create(l1), lengthInCommon); } }
Prefix implements Comparable<Prefix>, Serializable { public static Prefix longestCommonPrefix(Prefix p1, Prefix p2) { if (p1.containsPrefix(p2)) { return p1; } if (p2.containsPrefix(p1)) { return p2; } long l1 = p1.getStartIp().asLong(); long l2 = p2.getStartIp().asLong(); long oneAtFirstDifferentBit = Long.highestOneBit(l1 ^ l2); int lengthInCommon = MAX_PREFIX_LENGTH - 1 - Long.numberOfTrailingZeros(oneAtFirstDifferentBit); return Prefix.create(Ip.create(l1), lengthInCommon); } private  Prefix(Ip ip, int prefixLength); }
Prefix implements Comparable<Prefix>, Serializable { public static Prefix longestCommonPrefix(Prefix p1, Prefix p2) { if (p1.containsPrefix(p2)) { return p1; } if (p2.containsPrefix(p1)) { return p2; } long l1 = p1.getStartIp().asLong(); long l2 = p2.getStartIp().asLong(); long oneAtFirstDifferentBit = Long.highestOneBit(l1 ^ l2); int lengthInCommon = MAX_PREFIX_LENGTH - 1 - Long.numberOfTrailingZeros(oneAtFirstDifferentBit); return Prefix.create(Ip.create(l1), lengthInCommon); } private  Prefix(Ip ip, int prefixLength); @Nonnull @JsonCreator static Prefix parse(@Nullable String text); @Nonnull static Optional<Prefix> tryParse(@Nonnull String text); static @Nonnull Prefix strict(String prefixStr); static Prefix create(Ip ip, int prefixLength); static Prefix create(Ip address, Ip mask); static Prefix longestCommonPrefix(Prefix p1, Prefix p2); @Override int compareTo(Prefix rhs); boolean containsIp(Ip ip); boolean containsPrefix(Prefix prefix); @Override boolean equals(@Nullable Object o); @Nonnull Ip getEndIp(); int getPrefixLength(); @Nonnull Ip getPrefixWildcard(); @Nonnull Ip getStartIp(); @Override int hashCode(); @Nonnull IpSpace toIpSpace(); IpSpace toHostIpSpace(); Ip getFirstHostIp(); Ip getLastHostIp(); @Override @JsonValue String toString(); }
Prefix implements Comparable<Prefix>, Serializable { public static Prefix longestCommonPrefix(Prefix p1, Prefix p2) { if (p1.containsPrefix(p2)) { return p1; } if (p2.containsPrefix(p1)) { return p2; } long l1 = p1.getStartIp().asLong(); long l2 = p2.getStartIp().asLong(); long oneAtFirstDifferentBit = Long.highestOneBit(l1 ^ l2); int lengthInCommon = MAX_PREFIX_LENGTH - 1 - Long.numberOfTrailingZeros(oneAtFirstDifferentBit); return Prefix.create(Ip.create(l1), lengthInCommon); } private  Prefix(Ip ip, int prefixLength); @Nonnull @JsonCreator static Prefix parse(@Nullable String text); @Nonnull static Optional<Prefix> tryParse(@Nonnull String text); static @Nonnull Prefix strict(String prefixStr); static Prefix create(Ip ip, int prefixLength); static Prefix create(Ip address, Ip mask); static Prefix longestCommonPrefix(Prefix p1, Prefix p2); @Override int compareTo(Prefix rhs); boolean containsIp(Ip ip); boolean containsPrefix(Prefix prefix); @Override boolean equals(@Nullable Object o); @Nonnull Ip getEndIp(); int getPrefixLength(); @Nonnull Ip getPrefixWildcard(); @Nonnull Ip getStartIp(); @Override int hashCode(); @Nonnull IpSpace toIpSpace(); IpSpace toHostIpSpace(); Ip getFirstHostIp(); Ip getLastHostIp(); @Override @JsonValue String toString(); static final int MAX_PREFIX_LENGTH; static final Prefix ZERO; static final Prefix MULTICAST; static final int HOST_SUBNET_MAX_PREFIX_LENGTH; }