@SuppressWarnings("ResultOfMethodCallIgnored") @SneakyThrows @Test public void testComplex() { File root = testFolder.getRoot(); Map<String, String> filesContents = ImmutableMap.<String, String>builder() .put("hello.txt", "hello") .put("world.txt", "world") .put("second/1.txt", "1") .put("second/third/2.txt", "22") .put("second/third/3.txt", "333") .put("fourth/4.txt", "4444") .build(); for (Map.Entry<String, String> fileContent : filesContents.entrySet()) { File file = new File(root, fileContent.getKey()); if (!file.getParentFile().exists()) file.getParentFile().mkdirs(); Files.write(fileContent.getValue(), file, UTF_8); } Optional<Tree<FilesystemSummary>> possibleNode = Scanner.recursiveList(root); assertTrue(possibleNode.isPresent()); Tree<FilesystemSummary> thirdTree = new Tree<>(new FilesystemSummary(new File(root, "second/third"), 5, 2), ImmutableSortedSet.of( new Tree<>(new FilesystemSummary(new File(root, "second/third/2.txt"), 2, 1), EMPTY), new Tree<>(new FilesystemSummary(new File(root, "second/third/3.txt"), 3, 1), EMPTY))); Tree<FilesystemSummary> dir = new Tree<>(new FilesystemSummary(root, 20, 6), ImmutableSortedSet.of( new Tree<>(new FilesystemSummary(new File(root, "hello.txt"), 5, 1), EMPTY), new Tree<>(new FilesystemSummary(new File(root, "world.txt"), 5, 1), EMPTY), new Tree<>(new FilesystemSummary(new File(root, "second"), 6, 3), ImmutableSortedSet.of(new Tree<>(new FilesystemSummary(new File(root, "second/1.txt"), 1, 1), EMPTY), thirdTree)), new Tree<>(new FilesystemSummary(new File(root, "fourth"), 4, 1), ImmutableSortedSet.of(new Tree<>(new FilesystemSummary(new File(root, "fourth/4.txt"), 4, 1), EMPTY))))); assertEquals(dir, possibleNode.get()); }
@VisibleForTesting @SneakyThrows static Optional<Tree<FilesystemSummary>> recursiveList(File path) { if (path.isDirectory()) { long subTreeBytes = 0; long subTreeCount = 0; Iterable<Optional<Tree<FilesystemSummary>>> possibleChildren = listChildrenThreaded(path); SortedSet<Tree<FilesystemSummary>> children = ImmutableSortedSet.copyOf(Optional.presentInstances(possibleChildren)); for (Tree<FilesystemSummary> child : children) { subTreeBytes += child.getValue().getSubTreeBytes(); subTreeCount += child.getValue().getSubTreeCount(); } return Optional.of(new Tree<>(new FilesystemSummary(path, subTreeBytes, subTreeCount), children)); } else if (path.isFile()) { long length = SMART_LENGTH_COMPATIBLE ? smartLength(path) : path.length(); return Optional.of(new Tree<>(new FilesystemSummary(path, max(length, MIN_FILE_BYTES), 1), ImmutableSortedSet.<Tree<FilesystemSummary>>of())); } return Optional.absent(); }
Scanner extends AsyncTask<File, Void, Optional<Tree<FilesystemSummary>>> { @VisibleForTesting @SneakyThrows static Optional<Tree<FilesystemSummary>> recursiveList(File path) { if (path.isDirectory()) { long subTreeBytes = 0; long subTreeCount = 0; Iterable<Optional<Tree<FilesystemSummary>>> possibleChildren = listChildrenThreaded(path); SortedSet<Tree<FilesystemSummary>> children = ImmutableSortedSet.copyOf(Optional.presentInstances(possibleChildren)); for (Tree<FilesystemSummary> child : children) { subTreeBytes += child.getValue().getSubTreeBytes(); subTreeCount += child.getValue().getSubTreeCount(); } return Optional.of(new Tree<>(new FilesystemSummary(path, subTreeBytes, subTreeCount), children)); } else if (path.isFile()) { long length = SMART_LENGTH_COMPATIBLE ? smartLength(path) : path.length(); return Optional.of(new Tree<>(new FilesystemSummary(path, max(length, MIN_FILE_BYTES), 1), ImmutableSortedSet.<Tree<FilesystemSummary>>of())); } return Optional.absent(); } }
Scanner extends AsyncTask<File, Void, Optional<Tree<FilesystemSummary>>> { @VisibleForTesting @SneakyThrows static Optional<Tree<FilesystemSummary>> recursiveList(File path) { if (path.isDirectory()) { long subTreeBytes = 0; long subTreeCount = 0; Iterable<Optional<Tree<FilesystemSummary>>> possibleChildren = listChildrenThreaded(path); SortedSet<Tree<FilesystemSummary>> children = ImmutableSortedSet.copyOf(Optional.presentInstances(possibleChildren)); for (Tree<FilesystemSummary> child : children) { subTreeBytes += child.getValue().getSubTreeBytes(); subTreeCount += child.getValue().getSubTreeCount(); } return Optional.of(new Tree<>(new FilesystemSummary(path, subTreeBytes, subTreeCount), children)); } else if (path.isFile()) { long length = SMART_LENGTH_COMPATIBLE ? smartLength(path) : path.length(); return Optional.of(new Tree<>(new FilesystemSummary(path, max(length, MIN_FILE_BYTES), 1), ImmutableSortedSet.<Tree<FilesystemSummary>>of())); } return Optional.absent(); } @Inject  Scanner(Bus bus); }
Scanner extends AsyncTask<File, Void, Optional<Tree<FilesystemSummary>>> { @VisibleForTesting @SneakyThrows static Optional<Tree<FilesystemSummary>> recursiveList(File path) { if (path.isDirectory()) { long subTreeBytes = 0; long subTreeCount = 0; Iterable<Optional<Tree<FilesystemSummary>>> possibleChildren = listChildrenThreaded(path); SortedSet<Tree<FilesystemSummary>> children = ImmutableSortedSet.copyOf(Optional.presentInstances(possibleChildren)); for (Tree<FilesystemSummary> child : children) { subTreeBytes += child.getValue().getSubTreeBytes(); subTreeCount += child.getValue().getSubTreeCount(); } return Optional.of(new Tree<>(new FilesystemSummary(path, subTreeBytes, subTreeCount), children)); } else if (path.isFile()) { long length = SMART_LENGTH_COMPATIBLE ? smartLength(path) : path.length(); return Optional.of(new Tree<>(new FilesystemSummary(path, max(length, MIN_FILE_BYTES), 1), ImmutableSortedSet.<Tree<FilesystemSummary>>of())); } return Optional.absent(); } @Inject  Scanner(Bus bus); @Override void onPostExecute(Optional<Tree<FilesystemSummary>> result); @Override void onCancelled(); }
Scanner extends AsyncTask<File, Void, Optional<Tree<FilesystemSummary>>> { @VisibleForTesting @SneakyThrows static Optional<Tree<FilesystemSummary>> recursiveList(File path) { if (path.isDirectory()) { long subTreeBytes = 0; long subTreeCount = 0; Iterable<Optional<Tree<FilesystemSummary>>> possibleChildren = listChildrenThreaded(path); SortedSet<Tree<FilesystemSummary>> children = ImmutableSortedSet.copyOf(Optional.presentInstances(possibleChildren)); for (Tree<FilesystemSummary> child : children) { subTreeBytes += child.getValue().getSubTreeBytes(); subTreeCount += child.getValue().getSubTreeCount(); } return Optional.of(new Tree<>(new FilesystemSummary(path, subTreeBytes, subTreeCount), children)); } else if (path.isFile()) { long length = SMART_LENGTH_COMPATIBLE ? smartLength(path) : path.length(); return Optional.of(new Tree<>(new FilesystemSummary(path, max(length, MIN_FILE_BYTES), 1), ImmutableSortedSet.<Tree<FilesystemSummary>>of())); } return Optional.absent(); } @Inject  Scanner(Bus bus); @Override void onPostExecute(Optional<Tree<FilesystemSummary>> result); @Override void onCancelled(); static final Semaphore ADDITIONAL_THREADS; static final ExecutorService THREAD_POOL; static final ExecutorService DIRECT_THREAD; }