from typing import Sequence
from pathlib import Path


def _group_class_file_names(paths: Sequence[Path], max_length: int = 3900) -> Sequence[Sequence[Path]]:
    """
    Take the given sequence of paths and split them into sequences such that
    their cumulative length is no more than requested.  This is used to build
    command lines for the ``javap`` tool that are not overly long, regardless of
    the lengths of individual class file names while still minimizing the number
    of times the ``javap`` tool must be invoked.

    :param paths: the list of paths to group.
    :param max_length: the maximum cumulative length to allow.  The default is
    3900.
    :return: a sequence of sequences.  Each sub-sequence contains an appropriate
    number of paths.
    """
    sets = []
    start = 0
    length = 0

    for index, path in enumerate(paths):
        path_length = len(str(path)) + 1
        if length + path_length > max_length:
            sets.append(paths[start:index])
            start = index
            length = 0
        else:
            length = length + path_length

    sets.append(paths[start:])

    return sets