File (GenerationEnvironment data, File file, Set<File> exhausted) { File[] children = myChildrenCache.get(file); if (children == null) { return file; } if (children == EMPTY_DIRECTORY) { return null; } Arrays.sort(children, Comparator.comparing(File::getName)); while (true) { int[] weights = Arrays.stream(children).mapToInt(child -> estimateWeight(child, exhausted)).toArray(); int index; try { index = spin(data, weights); } catch (RuntimeException e) { if ("org.jetbrains.jetCheck.CannotRestoreValue".equals(e.getClass().getName())) { throw new RuntimeException("Directory structure changed in " + file + " or its direct children?", e); } throw e; } if (index == -1) return null; File chosen = children[index]; File generated = generateRandomFile(data, chosen, exhausted); if (generated != null) { return generated; } exhausted.add(chosen); } }