IPv4Ranges { static boolean withinRanges(int[] ranges, int value) { int low = 0; int high = ranges.length - 1; if (value < ranges[low] || value > ranges[high]) return false; while (high - low > 1) { int middle = (low + high) >>> 1; long middleValue = ranges[middle]; if (middleValue < value) low = middle; else if (middleValue > value) high = middle; else { return true; } } return low % 2 == 0; } IPv4Ranges(List<String> cidrs); boolean matches(byte[] address);  }
@Test void withinRanges() { int[] ranges = {1, 1, 2, 3, 5, 8, 20, 30}; assertThat(IPv4Ranges.withinRanges(ranges, 0)).isFalse(); assertThat(IPv4Ranges.withinRanges(ranges, 2)).isTrue(); assertThat(IPv4Ranges.withinRanges(ranges, 3)).isTrue(); assertThat(IPv4Ranges.withinRanges(ranges, 4)).isFalse(); assertThat(IPv4Ranges.withinRanges(ranges, 9)).isFalse(); assertThat(IPv4Ranges.withinRanges(ranges, 22)).isTrue(); assertThat(IPv4Ranges.withinRanges(ranges, 31)).isFalse(); }