File (GenerationEnvironment data, File file, Set<? super File> exhausted) { while (true) { File[] children = file.listFiles(f -> !exhausted.contains(f) && containsAtLeastOneFileDeep(f) && myFilter.accept(f)); if (children == null) { return file; } if (children.length == 0) { exhausted.add(file); return null; } List<File> toChoose = preferDirs(data, children); toChoose.sort(Comparator.comparing(File::getName)); File chosen = data.generate(Generator.sampledFrom(toChoose)); File generated = generateRandomFile(data, chosen, exhausted); if (generated != null) { return generated; } } }