/** * Returns the path of the given node. * * @param treeDef the treeDef * @param node the root of the tree * @param toString a function to map each node to a string in the path * @param delimiter a string to use as a path separator */ public static <T> String path(TreeDef.Parented<T> treeDef, T node, Function<? super T, String> toString, String delimiter) { requireNonNull(node); requireNonNull(delimiter); List<T> toRoot = toRoot(treeDef, node); ListIterator<T> iterator = toRoot.listIterator(toRoot.size()); StringBuilder builder = new StringBuilder(); while (iterator.hasPrevious()) { T segment = iterator.previous(); // add the node builder.append(toString.apply(segment)); // add the separator if it makes sense if (iterator.hasPrevious()) { builder.append(delimiter); } } return builder.toString(); }