IPv4Ranges { @SuppressWarnings("PMD.UseVarargs") static int[] mergeRanges(int[][] ranges) { int index; Arrays.sort(ranges, Comparator.comparingInt(range -> range[0])); int[] results = new int[ranges.length * 2]; index = 0; for (int[] range : ranges) { if (index > 1 && results[index - 1] >= range[0]) { if (results[index - 1] < range[1]) { results[index - 1] = range[1]; } } else { results[index++] = range[0]; results[index++] = range[1]; } } if (index < results.length) { return Arrays.copyOf(results, index); } else { return results; } } IPv4Ranges(List<String> cidrs); boolean matches(byte[] address);  }
@Test void mergeRanges() { int[][] ranges1 = {{1, 2}, {4, 5}, {5, 9}, {3, 5}, {10, 20}}; assertThat(IPv4Ranges.mergeRanges(ranges1)).containsExactly(1, 2, 3, 9, 10, 20); int[][] ranges2 = {{1, 2}, {10, 20}, {3, 5}, {30, 40}}; assertThat(IPv4Ranges.mergeRanges(ranges2)).containsExactly(1, 2, 3, 5, 10, 20, 30, 40); }