LongRangeUtils { public static Set<LongRange> getComplement(final LongRange range1, final LongRange range2) { final Set<LongRange> complement = new HashSet<>(); if (!range1.overlapsRange(range2)) { complement.add(range1); return complement; } else { final LongRange intersect = getIntersection(range1, range2); final long min1 = range1.getMinimumLong(); final long max1 = range1.getMaximumLong(); final long min2 = intersect.getMinimumLong(); final long max2 = intersect.getMaximumLong(); final long minMax = Math.min(max1, max2); final long maxMin = Math.max(min1, min2); final long leftEnd = Math.min(min1, min2); final long centerLeft = Math.min(minMax, maxMin); final long centerRight = Math.max(minMax, maxMin); final long rightEnd = Math.max(max1, max2); if (leftEnd < centerLeft) { final LongRange left = new LongRange(leftEnd, centerLeft - 1); complement.add(left); } if (rightEnd > centerRight) { final LongRange right = new LongRange(centerRight + 1, rightEnd); complement.add(right); } return complement; } } private  LongRangeUtils(); static NavigableSet<LongRange> createLongRangeSet(); static Set<LongRange> generateDenseLongRanges(final SortedSet<Long> longSortedSet); static LongRange getIntersection(final LongRange range1, final LongRange range2); static Set<LongRange> getComplement(final LongRange range1, final LongRange range2); static List<Pair<Long, Long>> fromRangesToPairs(final List<LongRange> rangeList); static List<LongRange> fromPairsToRanges(final List<Pair<Long, Long>> pairList); static final Comparator<LongRange> LONG_RANGE_COMPARATOR; }
@Test public void testComplement() { final Set<LongRange> complement1 = LongRangeUtils.getComplement(ORIGINAL, OUTSIDE); assertEquals(1, complement1.size()); assertTrue(exists(ORIGINAL, complement1)); assertFalse(exists(OUTSIDE, complement1)); final Set<LongRange> complement2 = LongRangeUtils.getComplement(ORIGINAL, SAME_EDGE_INSIDE); assertEquals(1, complement2.size()); assertTrue(exists(new LongRange(4, 7), complement2)); assertFalse(exists(SAME_EDGE_INSIDE, complement2)); final Set<LongRange> complement3 = LongRangeUtils.getComplement(ORIGINAL, SAME_EDGE_OUTSIDE); assertEquals(1, complement3.size()); assertTrue(exists(new LongRange(3, 7), complement3)); assertFalse(exists(SAME_EDGE_OUTSIDE, complement3)); final Set<LongRange> complement4 = LongRangeUtils.getComplement(ORIGINAL, INSIDE); assertEquals(2, complement4.size()); assertTrue(exists(new LongRange(2, 3), complement4)); assertTrue(exists(new LongRange(6, 7), complement4)); assertFalse(exists(INSIDE, complement4)); }