public static SortedSet<Interval> findFullAvailabilityGaps(Set<Interval> availableIntervals,Set<Interval> neededIntervals){
  Comparator<Interval> intervalStartComparator=new IntervalStartComparator();
  SortedSet<Interval> sortedAvailableIntervals=new TreeSet<>(intervalStartComparator);
  sortedAvailableIntervals.addAll(availableIntervals);
  SortedSet<Interval> sortedNeededIntervals=new TreeSet<>(intervalStartComparator);
  sortedNeededIntervals.addAll(neededIntervals);
  Iterator<Interval> availableIntervalsIterator=sortedAvailableIntervals.iterator();
  if (!availableIntervalsIterator.hasNext()) {
    return sortedNeededIntervals;
  }
  Interval available=availableIntervalsIterator.next();
  SortedSet<Interval> missingIntervals=new TreeSet<>(intervalStartComparator);
  for (  Interval needed : sortedNeededIntervals) {
    while (!canDetermineAvailability(available,needed) && availableIntervalsIterator.hasNext()) {
      available=availableIntervalsIterator.next();
    }
    if (available.contains(needed)) {
      continue;
    }
    missingIntervals.add(needed);
  }
  return missingIntervals;
}

public static SortedSet < Interval > findFullAvailabilityGaps ( Set < Interval > availableIntervals , Set < Interval > neededIntervals ) { Comparator < Interval > intervalStartComparator = new IntervalStartComparator ( ) ; SortedSet < Interval > sortedAvailableIntervals = new TreeSet < > ( intervalStartComparator ) ; sortedAvailableIntervals . addAll ( availableIntervals ) ; SortedSet < Interval > sortedNeededIntervals = new TreeSet < > ( intervalStartComparator ) ; sortedNeededIntervals . addAll ( neededIntervals ) ; Iterator < Interval > availableIntervalsIterator = sortedAvailableIntervals . iterator ( ) ; if ( ! availableIntervalsIterator . hasNext ( ) ) { return sortedNeededIntervals ; } Interval available = availableIntervalsIterator . next ( ) ; SortedSet < Interval > missingIntervals = new TreeSet < > ( intervalStartComparator ) ; for ( Interval needed : sortedNeededIntervals ) { while ( ! canDetermineAvailability ( available , needed ) && availableIntervalsIterator . hasNext ( ) ) { available = availableIntervalsIterator . next ( ) ; } if ( available . contains ( needed ) ) { continue ; } missingIntervals . add ( needed ) ; } return missingIntervals ; }
finds the gaps in available vs needed interval sets .
