/** Creates a deep copy of this TreeNode. */ public TreeNode<T> copy() { return copy(treeDef(), this, TreeNode::getContent); }
/** @see #findByPath(Object...) */ public TreeNode<T> findByPath(List<T> path) { Optional<TreeNode<T>> result = TreeQuery.findByPath(treeDef(), this, TreeNode::getContent, path, Function.identity()); if (result.isPresent()) { return result.get(); } else { throw new IllegalArgumentException(this.toString() + " has no element with path " + path); } }
/** Returns the path of this node, using the given {@code toString} method and {@code delimiter}. */ public String getPath(Function<? super T, String> toString, String delimiter) { requireNonNull(toString); requireNonNull(delimiter); return TreeQuery.path(treeDef(), this, node -> toString.apply(node.getContent()), delimiter); }
/** * Returns a "deep" toString, including the entire tree below this level. * * @see TreeQuery#toString(TreeDef, Object, Function, String) */ public String toStringDeep() { return TreeQuery.toString(treeDef(), this, node -> node.getContent().toString()); }
/** * Searches breadth-first for the TreeNode with the given content. * * @throws IllegalArgumentException if no such node exists */ public TreeNode<T> findByContent(T content) { requireNonNull(content); Optional<TreeNode<T>> opt = TreeStream.breadthFirst(treeDef(), this).filter(node -> node.getContent().equals(content)).findFirst(); if (opt.isPresent()) { return opt.get(); } else { throw new IllegalArgumentException(this.toString() + " has no child with content " + content); } } }
/** 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); }