/** An instance of {@code TreeDef.Parented} for {@link Path}. */ public static TreeDef.Parented<Path> forPath(Consumer<Throwable> errorPolicy) { Errors.Handling errors = Errors.createHandling(errorPolicy); return TreeDef.Parented.of( path -> errors.<List<Path>> getWithDefault(() -> { if (Files.isDirectory(path)) { return Files.list(path).collect(Collectors.toList()); } else { return Collections.emptyList(); } }, Collections.emptyList()), path -> errors.<Path> getWithDefault(() -> { return path.getParent(); }, null)); } }
/** An instance of {@code TreeDef.Parented} for {@link File}. */ public static TreeDef.Parented<File> forFile(Consumer<Throwable> errorPolicy) { Errors.Handling errors = Errors.createHandling(errorPolicy); return TreeDef.Parented.of( file -> errors.<List<File>> getWithDefault(() -> { if (file.isDirectory()) { return Arrays.asList(file.listFiles()); } else { return Collections.emptyList(); } }, Collections.emptyList()), file -> errors.<File> getWithDefault(() -> { return file.getParentFile(); }, null)); }
/** A TreeDef for projects. */ public static TreeDef.Parented<Project> treeDef() { return TreeDef.Parented.of(p -> ImmutableList.copyOf(p.getChildProjects().values()), Project::getParent); } }
/** Creates a new {@code TreeDef.Parented} whose {@code childrenOf} and {@code parentOf} methods are filtered by {@code predicate}. */ @Override default Parented<T> filter(Predicate<T> predicate) { return of(node -> TreeImp.filteredList(childrenOf(node), predicate), node -> { if (predicate.test(node)) { return parentOf(node); } else { return null; } }); }