RingRange { public static RingRange merge(List<RingRange> ranges) { Collections.sort(ranges, START_COMPARATOR); int gap = 0; for (; gap < ranges.size() - 1; gap++) { RingRange left = ranges.get(gap); RingRange right = ranges.get(gap + 1); if (!left.end.equals(right.start)) { break; } } if (gap == ranges.size() - 1) { return new RingRange(ranges.get(0).start, ranges.get(gap).end); } else { return new RingRange(ranges.get(gap + 1).start, ranges.get(gap).end); } } RingRange(BigInteger start, BigInteger end);  RingRange(String... range); BigInteger getStart(); BigInteger getEnd(); BigInteger span(BigInteger ringSize); boolean encloses(RingRange other); @JsonIgnore boolean isWrapping(); @Override String toString(); static RingRange merge(List<RingRange> ranges); static final Comparator<RingRange> START_COMPARATOR; }
@Test public void mergeTest() { List<RingRange> ranges = Lists.newArrayList(); ranges.add(new RingRange(BigInteger.valueOf(30L), BigInteger.valueOf(50L))); ranges.add(new RingRange(BigInteger.valueOf(10L), BigInteger.valueOf(30L))); ranges.add(new RingRange(BigInteger.valueOf(80L), BigInteger.valueOf(10L))); RingRange merged = RingRange.merge(ranges); assertEquals("80", merged.getStart().toString()); assertEquals("50", merged.getEnd().toString()); }