static <T extends Segment> boolean sweep(@NotNull Generator<? extends T> generator, @NotNull SweepProcessor<T> sweepProcessor) {
    Queue<T> ends = new PriorityQueue<>(5, Comparator.comparingInt(Segment::getEndOffset));
    if (!generator.processAllInStartOffsetOrder(marker -> {
      // decide whether the previous marker ends here or the new marker begins
      int start = marker.getStartOffset();
      while (!ends.isEmpty()) {
        T previous = ends.peek();
        int previousStartOffset = previous.getStartOffset();
        if (start < previousStartOffset) {
          throw new IllegalStateException("Generator "+generator+" supplied segments in a wrong order: "+previous+" was received before "+ marker +" ("+start+"<"+previousStartOffset+")");
        }
        int prevEnd = previous.getEndOffset();
        if (prevEnd <= start) {
          if (!sweepProcessor.process(prevEnd, previous, false, ends)) return false;
          ends.remove();
        }
        else {
          break;
        }
      }
      if (!sweepProcessor.process(start, marker, true, ends)) return false;
      ends.offer(marker);

      return true;
    })) return false;

    while (!ends.isEmpty()) {
      T previous = ends.remove();
      int prevEnd = previous.getEndOffset();
      if (!sweepProcessor.process(prevEnd, previous, false, ends)) return false;
    }

    return true;
  }
static <T extends Segment> boolean sweep(@NotNull Generator<? extends T> generator, @NotNull SweepProcessor<T> sweepProcessor)