|
1 | | -package example.genericJ8; |
2 | | -import java.util.function.Function; |
3 | | - |
4 | | -import org.apache.commons.lang3.StringUtils; |
5 | | - |
6 | | -import example.TreeNode; |
7 | | - |
8 | | -public class TreeUtil_2_GenericJava8 { |
9 | | - |
10 | | - public static <T> String printTree(TreeNode<T> treeNode, Function<TreeNode<T>, String> attrExtr) { |
11 | | - StringBuilder sb = new StringBuilder(); |
12 | | - printTree(treeNode, sb, 0, attrExtr); |
13 | | - return sb.toString(); |
14 | | - } |
15 | | - |
16 | | - private static <T> void printTree(TreeNode<T> treeNode, StringBuilder sb, int indent, Function<TreeNode<T>, String> extractorFct) { |
17 | | - if (treeNode != null) { |
18 | | - sb.append(StringUtils.repeat(" ", indent) + extractorFct.apply(treeNode)); |
19 | | - if (treeNode.getChildren() != null) { |
20 | | - for (TreeNode<T> data : treeNode.getChildren()) { |
21 | | - printTree(data, sb, indent + 1, extractorFct); |
22 | | - } |
23 | | - } |
24 | | - } |
25 | | - } |
26 | | - |
27 | | -} |
| 1 | +package example.genericJ8; |
| 2 | +import java.util.function.Function; |
| 3 | + |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | + |
| 6 | +import example.TreeNode; |
| 7 | + |
| 8 | +public class TreeUtilGenericJava8 { |
| 9 | + |
| 10 | + public static <T> String printTree(TreeNode<T> treeNode, Function<TreeNode<T>, String> attrExtr) { |
| 11 | + StringBuilder sb = new StringBuilder(); |
| 12 | + printTree(treeNode, sb, 0, attrExtr); |
| 13 | + return sb.toString(); |
| 14 | + } |
| 15 | + |
| 16 | + private static <T> void printTree(TreeNode<T> treeNode, StringBuilder sb, int indent, Function<TreeNode<T>, String> extractorFct) { |
| 17 | + if (treeNode != null) { |
| 18 | + sb.append(StringUtils.repeat(" ", indent) + extractorFct.apply(treeNode)); |
| 19 | + if (treeNode.getChildren() != null) { |
| 20 | + for (TreeNode<T> data : treeNode.getChildren()) { |
| 21 | + printTree(data, sb, indent + 1, extractorFct); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | +} |
0 commit comments