RangeSegment { static List<RangeSegment> sortAndCombine(List<RangeSegment> segments) { try { Collections.sort(segments, RANGE_SEGMENTS_BY_START); } catch (RangeEndpoint.IllegalComparisonException e) { log.warn("illegal comparison in sorting/combining range segments", e); return null; } RangeSegment previous = null; for (ListIterator<RangeSegment> iterator = segments.listIterator(); iterator.hasNext(); ) { RangeSegment currentSegment = iterator.next(); final RangeSegment nextPrevious; if (previous == null) { nextPrevious = currentSegment; } else { RangeEndpoint previousEnd = previous.getEnd(); RangeEndpoint currentStart = currentSegment.getStart(); Boolean startsOverlap = findOverlap(previousEnd, currentStart, true); if (startsOverlap == null) return null; if (startsOverlap) { Boolean endsOverlap = findOverlap(previousEnd, currentSegment.getEnd(), false); if (endsOverlap == null) return null; if (endsOverlap) { iterator.remove(); nextPrevious = previous; } else { nextPrevious = new RangeSegment(previous.getStart(), currentSegment.getEnd()); replacePreviousTwo(iterator, previous, nextPrevious); } } else { nextPrevious = currentSegment; } } previous = nextPrevious; } return segments; } RangeSegment(RangeEndpoint start, RangeEndpoint end); static List<RangeSegment> fromComparison(Comparison op, ConstantExpression constantExpression); RangeEndpoint getStart(); RangeEndpoint getEnd(); boolean isSingle(); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); static RangeSegment onlyNull(ExpressionNode expressionNode);  }
@Test public void sacIncomparableTypesForSorting() { List<RangeSegment> original = Arrays.asList( segment(inclusive("aardvark"), inclusive("person")), segment(inclusive(1L), exclusive("zebra")) ); List<RangeSegment> copy = new ArrayList<>(original); List<RangeSegment> sorted = RangeSegment.sortAndCombine(copy); assertCollectionEquals(original, copy); assertNull("sorted list should be null", sorted); }
@Test public void sacIncomparableTypesForMerging() { List<RangeSegment> original = Arrays.asList( segment(inclusive("aardvark"), inclusive(1L)), segment(inclusive("cat"), exclusive(1L)) ); List<RangeSegment> copy = new ArrayList<>(original); List<RangeSegment> sorted = RangeSegment.sortAndCombine(copy); assertCollectionEquals(original, copy); assertNull("sorted list should be null", sorted); }