/** * Finds a child TreeNode based on its path. * <p> * Searches the child nodes for the first element, then that * node's children for the second element, etc. * * @param treeDef defines a tree * @param node starting point for the search * @param path the path of nodes which we're looking * @param mapper maps elements to some value for comparison between the tree and the path */ public static <T> Optional<T> findByPath(TreeDef<T> treeDef, T node, List<T> path, Function<? super T, ?> mapper) { return findByPath(treeDef, node, mapper, path, mapper); }
/** * Finds a child TreeNode based on its path. * <p> * Searches the child nodes for the first element, then that * node's children for the second element, etc. * * @param treeDef defines a tree * @param node starting point for the search * @param treeMapper maps elements in the tree to some value for comparison with the path elements * @param path the path of nodes which we're looking * @param pathMapper maps elements in the path to some value for comparison with the tree elements */ public static <T, P> Optional<T> findByPath(TreeDef<T> treeDef, T node, Function<? super T, ?> treeMapper, List<P> path, Function<? super P, ?> pathMapper) { requireNonNull(treeMapper); requireNonNull(path); requireNonNull(pathMapper); return findByPath(treeDef, node, path, (treeSide, pathSide) -> { return Objects.equals(treeMapper.apply(treeSide), pathMapper.apply(pathSide)); }); }
/** @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); } }