/** Creates a new TreeDef which whose {@code childrenOf} method is filtered by the given predicate. */ default TreeDef<T> filter(Predicate<T> predicate) { return TreeDef.of(node -> TreeImp.filteredList(childrenOf(node), predicate)); }
/** * Asserts that the trees are equal, by calling {@link Objects#equals(Object, Object)} on the results of both mappers. * * @see #isEqualMappedBy(Function, Function) */ public void assertEqualMappedBy(Function<? super E, ?> expectedMapper, Function<? super A, ?> actualMapper) { if (!isEqualMappedBy(expectedMapper, actualMapper)) { throwAssertionError(); } }
protected String getContents(Predicate<String> subpathsToInclude) throws IOException { TreeDef<File> treeDef = TreeDef.forFile(Errors.rethrow()); List<File> files = TreeStream.depthFirst(treeDef, rootFolder()) .filter(File::isFile) .collect(Collectors.toList()); ListIterator<File> iterator = files.listIterator(files.size()); int rootLength = rootFolder().getAbsolutePath().length() + 1; return StringPrinter.buildString(printer -> Errors.rethrow().run(() -> { while (iterator.hasPrevious()) { File file = iterator.previous(); String subPath = file.getAbsolutePath().substring(rootLength); if (subpathsToInclude.test(subPath)) { printer.println("### " + subPath + " ###"); printer.println(read(subPath)); } } })); }
/** Returns all of the .gitattributes files which affect the given files. */ static List<File> gitAttributes(Iterable<File> files) { // build a radix tree out of all the parent folders in these files ConcurrentRadixTree<String> tree = new ConcurrentRadixTree<>(new DefaultCharSequenceNodeFactory()); for (File file : files) { String parentPath = file.getParent() + File.separator; tree.putIfAbsent(parentPath, parentPath); } // traverse the edge nodes to find the outermost folders List<File> edgeFolders = TreeStream.depthFirst(Node::getOutgoingEdges, tree.getNode()) .filter(node -> node.getOutgoingEdges().isEmpty() && node.getValue() != null) .map(node -> new File((String) node.getValue())) .collect(Collectors.toList()); List<File> gitAttrFiles = new ArrayList<>(); Set<File> visitedFolders = new HashSet<>(); for (File edgeFolder : edgeFolders) { gitAttrAddWithParents(edgeFolder, visitedFolders, gitAttrFiles); } return gitAttrFiles; }
/** Returns the root of the given tree. */ public static <T> T root(TreeDef.Parented<T> treeDef, T node) { T lastParent; T parent = node; do { lastParent = parent; parent = treeDef.parentOf(lastParent); } while (parent != null); return lastParent; }
/** * Asserts that the trees are equal, based on the given {@link BiPredicate}. * * @see #isEqualBasedOn(BiPredicate) */ public void assertEqualBasedOn(BiPredicate<E, A> compareFunc) { if (!isEqualBasedOn(compareFunc)) { throwAssertionError(); } }
private static <T, CopyType> void copyMutableRecurse(TreeDef<T> def, T root, List<T> children, CopyType copiedRoot, BiFunction<T, CopyType, CopyType> mapper) { for (T child : children) { List<T> grandChildren = def.childrenOf(child); copyMutableRecurse(def, root, grandChildren, mapper.apply(child, copiedRoot), mapper); } }
/** * Converts the entire tree into a string-based representation. * * @see #toString(TreeDef, Object, Function, String) */ public static <T> String toString(TreeDef<T> treeDef, T root) { return toString(treeDef, root, Object::toString); }
/** * Returns the path of the given node, using {@code /} as the path delimiter. * * @see #path(com.diffplug.common.tree.TreeDef.Parented, Object, Function, String) */ public static <T> String path(TreeDef.Parented<T> treeDef, T node, Function<? super T, String> toString) { return path(treeDef, node, toString, "/"); }
/** * Asserts that the trees are equal, based on `Objects.equals()`. * * @see #isEqualBasedOn(BiPredicate) */ public void assertEqual() { assertEqualBasedOn(Objects::equals); }
/** Returns true if the two trees are equal. */ @Override public boolean isEqual() { return comparison.isEqualMappedBy(mapExpected, mapActual); }
/** Asserts that the two trees are equal. */ @Override public void assertEqual() { comparison.assertEqualMappedBy(mapExpected, mapActual); }
/** Removes this TreeNode from its parent. */ public void removeFromParent() { setParent(null); }
/** Returns true if the two trees are equal, based on `Objects.equals`. */ public boolean isEqual() { return isEqualBasedOn(Objects::equals); }
/** Throws an assetion error. */ private void throwAssertionError() { throw createAssertionError(); }
/** Creates a {@link SameType} for comparing a {@link TreeNode} against a generic tree which been mapped. */ public static <T, U> SameType<T> of(TreeNode<T> expected, TreeDef<U> treeDef, U actual, Function<? super U, ? extends T> mapper) { return of(TreeNode.treeDef(), expected, treeDef, actual).mapToSame(TreeNode::getContent, mapper); }
@Override public Object getParent(Object element) { return treeDef.parentOf((T) element); } });
/** * Converts the entire tree into a string-based representation. * * @see #toString(TreeDef, Object, Function, String) */ public static <T> String toString(TreeDef<T> treeDef, T root, Function<? super T, String> toString) { return toString(treeDef, root, toString, " "); }
/** * Returns the path of the given node, using {@code /} as the path delimiter and {@link Object#toString()} as the mapping function. * * @see #path(com.diffplug.common.tree.TreeDef.Parented, Object, Function, String) */ public static <T> String path(TreeDef.Parented<T> treeDef, T node) { return path(treeDef, node, Object::toString); }