code: public static List<DiskRange> mergeAdjacentDiskRanges(Collection<DiskRange> diskRanges, DataSize maxMergeDistance, DataSize maxReadSize)
      {
          // sort ranges by start offset
          List<DiskRange> ranges = new ArrayList<>(diskRanges);
          Collections.sort(ranges, new Comparator<DiskRange>()
          {
              @Override
              public int compare(DiskRange o1, DiskRange o2)
              {
                  return Long.compare(o1.getOffset(), o2.getOffset());
              }
          });
  
          // merge overlapping ranges
          long maxReadSizeBytes = maxReadSize.toBytes();
          long maxMergeDistanceBytes = maxMergeDistance.toBytes();
          ImmutableList.Builder<DiskRange> result = ImmutableList.builder();
          DiskRange last = ranges.get(0);
          for (int i = 1; i < ranges.size(); i++) {
              DiskRange current = ranges.get(i);
              DiskRange merged = last.span(current);
              if (merged.getLength() <= maxReadSizeBytes && last.getEnd() + maxMergeDistanceBytes >= current.getOffset()) {
                  last = merged;
              }
              else {
                  result.add(last);
                  last = current;
              }
          }
          result.add(last);
  
          return result.build();
      }
docs: Merge disk ranges that are closer than {@code maxMergeDistance}.