@Test public void testRange() { assertThat( rangeToWildcards(Ip.ZERO, Ip.MAX), equalTo(ImmutableSortedSet.of(IpWildcard.create(Prefix.ZERO)))); assertThat( rangeToWildcards(Ip.ZERO, Ip.ZERO), equalTo(ImmutableSortedSet.of(IpWildcard.create(Ip.ZERO)))); assertThat( rangeToWildcards(Ip.MAX, Ip.MAX), equalTo(ImmutableSortedSet.of(IpWildcard.create(Ip.MAX)))); assertThat( rangeToWildcards(Ip.parse("1.2.3.4"), Ip.parse("1.2.3.4")), equalTo(ImmutableSortedSet.of(IpWildcard.parse("1.2.3.4")))); assertThat( rangeToWildcards(Ip.parse("1.2.3.4"), Ip.parse("1.2.3.5")), equalTo(ImmutableSortedSet.of(IpWildcard.parse("1.2.3.4/31")))); assertThat( rangeToWildcards(Ip.parse("1.2.3.4"), Ip.parse("1.2.3.6")), equalTo( ImmutableSortedSet.of(IpWildcard.parse("1.2.3.4/31"), IpWildcard.parse("1.2.3.6")))); assertThat( rangeToWildcards(Ip.parse("1.2.3.4"), Ip.parse("1.2.3.7")), equalTo(ImmutableSortedSet.of(IpWildcard.parse("1.2.3.4/30")))); assertThat( rangeToWildcards(Ip.parse("1.2.3.4"), Ip.parse("1.2.3.8")), equalTo( ImmutableSortedSet.of(IpWildcard.parse("1.2.3.4/30"), IpWildcard.parse("1.2.3.8")))); assertThat( rangeToWildcards(Ip.parse("1.2.3.2"), Ip.parse("1.2.3.5")), equalTo( ImmutableSortedSet.of(IpWildcard.parse("1.2.3.2/31"), IpWildcard.parse("1.2.3.4/31")))); assertThat( rangeToWildcards(Ip.parse("1.2.3.1"), Ip.parse("1.2.3.14")), equalTo( ImmutableSortedSet.of( IpWildcard.parse("1.2.3.1/32"), IpWildcard.parse("1.2.3.2/31"), IpWildcard.parse("1.2.3.4/30"), IpWildcard.parse("1.2.3.8/30"), IpWildcard.parse("1.2.3.12/31"), IpWildcard.parse("1.2.3.14/32")))); }
@VisibleForTesting static SortedSet<IpWildcard> rangeToWildcards(Ip low, Ip high) { checkArgument(low.valid(), "Illegal range: %s is not a valid IP", low); checkArgument(high.valid(), "Illegal range: %s is not a valid IP", high); checkArgument(low.compareTo(high) <= 0, "Illegal range: %s is larger than %s", low, high); long lo = low.asLong(); long hi = high.asLong(); ImmutableSortedSet.Builder<IpWildcard> ret = ImmutableSortedSet.naturalOrder(); long start = lo; while (start <= hi) { long numIps = hi - start + 1; int minPrefixLengthThatStartsAtStart = 32 - Math.min(Long.numberOfTrailingZeros(start), 32); int minPrefixLengthContainedInRange = 32 - LongMath.log2(numIps, RoundingMode.DOWN); int prefixLengthToUse = Math.max(minPrefixLengthThatStartsAtStart, minPrefixLengthContainedInRange); ret.add(IpWildcard.create(Prefix.create(Ip.create(start), prefixLengthToUse))); start += 1L << (32 - prefixLengthToUse); } return ret.build(); }
AddressRangeAddressBookEntry extends AddressBookEntry { @VisibleForTesting static SortedSet<IpWildcard> rangeToWildcards(Ip low, Ip high) { checkArgument(low.valid(), "Illegal range: %s is not a valid IP", low); checkArgument(high.valid(), "Illegal range: %s is not a valid IP", high); checkArgument(low.compareTo(high) <= 0, "Illegal range: %s is larger than %s", low, high); long lo = low.asLong(); long hi = high.asLong(); ImmutableSortedSet.Builder<IpWildcard> ret = ImmutableSortedSet.naturalOrder(); long start = lo; while (start <= hi) { long numIps = hi - start + 1; int minPrefixLengthThatStartsAtStart = 32 - Math.min(Long.numberOfTrailingZeros(start), 32); int minPrefixLengthContainedInRange = 32 - LongMath.log2(numIps, RoundingMode.DOWN); int prefixLengthToUse = Math.max(minPrefixLengthThatStartsAtStart, minPrefixLengthContainedInRange); ret.add(IpWildcard.create(Prefix.create(Ip.create(start), prefixLengthToUse))); start += 1L << (32 - prefixLengthToUse); } return ret.build(); } }
AddressRangeAddressBookEntry extends AddressBookEntry { @VisibleForTesting static SortedSet<IpWildcard> rangeToWildcards(Ip low, Ip high) { checkArgument(low.valid(), "Illegal range: %s is not a valid IP", low); checkArgument(high.valid(), "Illegal range: %s is not a valid IP", high); checkArgument(low.compareTo(high) <= 0, "Illegal range: %s is larger than %s", low, high); long lo = low.asLong(); long hi = high.asLong(); ImmutableSortedSet.Builder<IpWildcard> ret = ImmutableSortedSet.naturalOrder(); long start = lo; while (start <= hi) { long numIps = hi - start + 1; int minPrefixLengthThatStartsAtStart = 32 - Math.min(Long.numberOfTrailingZeros(start), 32); int minPrefixLengthContainedInRange = 32 - LongMath.log2(numIps, RoundingMode.DOWN); int prefixLengthToUse = Math.max(minPrefixLengthThatStartsAtStart, minPrefixLengthContainedInRange); ret.add(IpWildcard.create(Prefix.create(Ip.create(start), prefixLengthToUse))); start += 1L << (32 - prefixLengthToUse); } return ret.build(); } AddressRangeAddressBookEntry(String name, Ip lowerLimit, Ip upperLimit); }
AddressRangeAddressBookEntry extends AddressBookEntry { @VisibleForTesting static SortedSet<IpWildcard> rangeToWildcards(Ip low, Ip high) { checkArgument(low.valid(), "Illegal range: %s is not a valid IP", low); checkArgument(high.valid(), "Illegal range: %s is not a valid IP", high); checkArgument(low.compareTo(high) <= 0, "Illegal range: %s is larger than %s", low, high); long lo = low.asLong(); long hi = high.asLong(); ImmutableSortedSet.Builder<IpWildcard> ret = ImmutableSortedSet.naturalOrder(); long start = lo; while (start <= hi) { long numIps = hi - start + 1; int minPrefixLengthThatStartsAtStart = 32 - Math.min(Long.numberOfTrailingZeros(start), 32); int minPrefixLengthContainedInRange = 32 - LongMath.log2(numIps, RoundingMode.DOWN); int prefixLengthToUse = Math.max(minPrefixLengthThatStartsAtStart, minPrefixLengthContainedInRange); ret.add(IpWildcard.create(Prefix.create(Ip.create(start), prefixLengthToUse))); start += 1L << (32 - prefixLengthToUse); } return ret.build(); } AddressRangeAddressBookEntry(String name, Ip lowerLimit, Ip upperLimit); @Override SortedMap<String, AddressSetEntry> getEntries(); @Override SortedSet<IpWildcard> getIpWildcards(Warnings w); }
AddressRangeAddressBookEntry extends AddressBookEntry { @VisibleForTesting static SortedSet<IpWildcard> rangeToWildcards(Ip low, Ip high) { checkArgument(low.valid(), "Illegal range: %s is not a valid IP", low); checkArgument(high.valid(), "Illegal range: %s is not a valid IP", high); checkArgument(low.compareTo(high) <= 0, "Illegal range: %s is larger than %s", low, high); long lo = low.asLong(); long hi = high.asLong(); ImmutableSortedSet.Builder<IpWildcard> ret = ImmutableSortedSet.naturalOrder(); long start = lo; while (start <= hi) { long numIps = hi - start + 1; int minPrefixLengthThatStartsAtStart = 32 - Math.min(Long.numberOfTrailingZeros(start), 32); int minPrefixLengthContainedInRange = 32 - LongMath.log2(numIps, RoundingMode.DOWN); int prefixLengthToUse = Math.max(minPrefixLengthThatStartsAtStart, minPrefixLengthContainedInRange); ret.add(IpWildcard.create(Prefix.create(Ip.create(start), prefixLengthToUse))); start += 1L << (32 - prefixLengthToUse); } return ret.build(); } AddressRangeAddressBookEntry(String name, Ip lowerLimit, Ip upperLimit); @Override SortedMap<String, AddressSetEntry> getEntries(); @Override SortedSet<IpWildcard> getIpWildcards(Warnings w);  }