code: static boolean needsCleanup(SSTableReader sstable, Collection<Range<Token>> ownedRanges)
      {
          assert !ownedRanges.isEmpty(); // cleanup checks for this
  
          // unwrap and sort the ranges by LHS token
          List<Range<Token>> sortedRanges = Range.normalize(ownedRanges);
  
          // see if there are any keys LTE the token for the start of the first range
          // (token range ownership is exclusive on the LHS.)
          Range<Token> firstRange = sortedRanges.get(0);
          if (sstable.first.getToken().compareTo(firstRange.left) <= 0)
              return true;
  
          // then, iterate over all owned ranges and see if the next key beyond the end of the owned
          // range falls before the start of the next range
          for (int i = 0; i < sortedRanges.size(); i++)
          {
              Range<Token> range = sortedRanges.get(i);
              if (range.right.isMinimum())
              {
                  // we split a wrapping range and this is the second half.
                  // there can't be any keys beyond this (and this is the last range)
                  return false;
              }
  
              DecoratedKey firstBeyondRange = sstable.firstKeyBeyond(range.right.maxKeyBound());
              if (firstBeyondRange == null)
              {
                  // we ran off the end of the sstable looking for the next key; we don't need to check any more ranges
                  return false;
              }
  
              if (i == (sortedRanges.size() - 1))
              {
                  // we're at the last range and we found a key beyond the end of the range
                  return true;
              }
  
              Range<Token> nextRange = sortedRanges.get(i + 1);
              if (!nextRange.contains(firstBeyondRange.getToken()))
              {
                  // we found a key in between the owned ranges
                  return true;
              }
          }
  
          return false;
      }
docs: Determines if a cleanup would actually remove any data in this SSTable based
  on a set of owned ranges.