TimeRange implements Comparable<TimeRange> { @SuppressWarnings("squid:S3776") public List<TimeRange> getRemains(List<TimeRange> timeRangesPrev) { List<TimeRange> remains = new ArrayList<>(); for (TimeRange prev : timeRangesPrev) { if (prev.min >= max + 2) { break; } if (intersects(prev)) { if (prev.contains(this)) { return remains; } else if (this.contains(prev)) { if (prev.min > this.min && prev.max == this.max) { this.setMax(prev.min); this.setRightClose(false); remains.add(this); return remains; } else if (prev.min == this.min) { min = prev.max; leftClose = false; } else { TimeRange r = new TimeRange(this.min, prev.min); r.setLeftClose(this.leftClose); r.setRightClose(false); remains.add(r); min = prev.max; leftClose = false; } } else { if (prev.min < this.min) { min = prev.max; leftClose = false; } else { this.setMax(prev.min); this.setRightClose(false); remains.add(this); return remains; } } } } remains.add(this); return remains; } TimeRange(long min, long max); @Override int compareTo(TimeRange r); void setMin(long min); void setMax(long max); boolean contains(TimeRange r); boolean contains(long min, long max); boolean contains(long time); void set(long min, long max); long getMin(); long getMax(); boolean intersects(TimeRange r); @Override boolean equals(Object o); @Override int hashCode(); boolean overlaps(TimeRange rhs); @Override String toString(); void setLeftClose(boolean leftClose); void setRightClose(boolean rightClose); boolean getLeftClose(); boolean getRightClose(); static List<TimeRange> sortAndMerge(List<TimeRange> unionCandidates); @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning List<TimeRange> getRemains(List<TimeRange> timeRangesPrev); IExpression getExpression();  }
@Test public void getRemainsTest1() { TimeRange r = new TimeRange(1, 10); ArrayList<TimeRange> prevRanges = new ArrayList<>(); prevRanges.add(new TimeRange(0, 10)); ArrayList<TimeRange> remainRanges = new ArrayList<>(r.getRemains(prevRanges)); assertEquals(0, remainRanges.size()); }