|
1 | | -package example.genericJ8; |
2 | | - |
3 | | -import static java.util.stream.Collectors.joining; |
4 | | - |
5 | | -import java.util.ArrayList; |
6 | | -import java.util.List; |
7 | | -import java.util.function.Function; |
8 | | - |
9 | | -import example.TreeNode; |
10 | | - |
11 | | -public class TreePathUtil { |
12 | | - |
13 | | - public static <T> String getTreePath(TreeNode<T> treeNode, Function<T, String> extractorFct, String separator) { |
14 | | - List<String> path = new ArrayList<>(); |
15 | | - getTreePath(path, treeNode, extractorFct); |
16 | | - return path.stream().collect(joining(separator)); |
17 | | - } |
18 | | - |
19 | | - private static <T, U> void getTreePath(List<U> path, TreeNode<T> node, Function<T, U> extractorFct) { |
20 | | - if (node != null && node.getData() != null) { |
21 | | - path.add(0, extractorFct.apply(node.getData())); |
22 | | - if (node.getParent() != null) { |
23 | | - getTreePath(path, node.getParent(), extractorFct); |
24 | | - } |
25 | | - } |
26 | | - } |
27 | | - |
28 | | -} |
| 1 | +package example.genericJ8; |
| 2 | + |
| 3 | +import static java.util.stream.Collectors.joining; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.function.Function; |
| 8 | + |
| 9 | +import example.TreeNode; |
| 10 | + |
| 11 | +public class TreePathUtil { |
| 12 | + |
| 13 | + public static <T> String getTreePath(TreeNode<T> treeNode, Function<T, String> extractorFct, String separator) { |
| 14 | + List<String> path = new ArrayList<>(); |
| 15 | + getTreePath(path, treeNode, extractorFct); |
| 16 | + return path.stream().collect(joining(separator)); |
| 17 | + } |
| 18 | + |
| 19 | + private static <T, U> void getTreePath(List<U> path, TreeNode<T> node, Function<T, U> extractorFct) { |
| 20 | + if (node != null && node.getData() != null) { |
| 21 | + path.add(0, extractorFct.apply(node.getData())); |
| 22 | + if (node.getParent() != null) { |
| 23 | + getTreePath(path, node.getParent(), extractorFct); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | +} |
0 commit comments