private static Optional<DirId> findDir(final Path path,
                                           final Mode mode,
                                           final Predicate<String> filenamePredicate,
                                           boolean warnIfInvalid) {

        LOGGER.trace("find - path: {}, mode: {}", path, mode);
        try (final Stream<Path> dirStream = findDirectories(path)) {

            final Stream<DirId> dirIdStream = dirStream.map(
                            aPath -> {
                                final DirId dirId;
                                final String fileNamePart = aPath.getFileName().toString();

                                if (!filenamePredicate.test(fileNamePart)) {
                                    if (warnIfInvalid) {
                                        LOGGER.warn(() -> LogUtil.message(
                                                "Found directory '{}' with unexpected filename length, " +
                                                "fileNamePart: '{}'. It will be ignored.",
                                                LogUtil.path(aPath), fileNamePart));
                                    }
                                    dirId = null;
                                } else {
                                    // Parse numeric part of dir.
                                    final OptionalLong optNum = parse(fileNamePart);
                                    if (optNum.isPresent()) {
                                        dirId = new DirId(aPath, optNum.getAsLong());
                                    } else {
                                        LOGGER.warn(() -> LogUtil.message(
                                                "Found unexpected non-numeric directory '{}' in '{}'. " +
                                                "It will be ignored.",
                                                LogUtil.path(aPath), LogUtil.path(path)));
                                        dirId = null;
                                    }
                                }
                                return dirId;
                            })
                    .filter(Objects::nonNull);

            return switch (mode) {
                case MIN -> dirIdStream.min(DIR_ID_COMPARATOR_ASC);
                case MAX -> dirIdStream.max(DIR_ID_COMPARATOR_ASC);
            };

        } catch (final IOException e) {
            throw new UncheckedIOException(e);
        }
    }
Get the {@link DirId} with the highest/lowest {@link DirId#num}.
Ignores files and non-numeric dirs.
stroom-proxy/stroom-proxy-app/src/main/java/stroom/proxy/app/handler/DirUtil.java