Skip to content

Commit 8e5ff77

Browse files
committed
initial
0 parents  commit 8e5ff77

File tree

12 files changed

+454
-0
lines changed

12 files changed

+454
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.settings/
2+
/target/
3+
/.classpath
4+
/.project

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"# TODO"

pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>de.doubleslash.mfischer.javase8</groupId>
6+
<artifactId>treeexample</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
</properties>
15+
16+
<dependencies>
17+
18+
<!-- utilities -->
19+
<dependency>
20+
<groupId>org.apache.commons</groupId>
21+
<artifactId>commons-lang3</artifactId>
22+
<version>3.4</version>
23+
</dependency>
24+
25+
<!-- Logging -->
26+
<dependency>
27+
<groupId>ch.qos.logback</groupId>
28+
<artifactId>logback-classic</artifactId>
29+
<version>1.1.3</version>
30+
</dependency>
31+
32+
<!-- Testing -->
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>4.12</version>
37+
<scope>test</scope>
38+
</dependency>
39+
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-compiler-plugin</artifactId>
47+
<version>2.3.2</version>
48+
<configuration>
49+
<source>${maven.compiler.source}</source>
50+
<target>${maven.compiler.target}</target>
51+
</configuration>
52+
</plugin>
53+
</plugins>
54+
55+
</build>
56+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package example;
2+
3+
import java.math.BigDecimal;
4+
5+
public class ExampleData {
6+
String name;
7+
int num;
8+
BigDecimal foo;
9+
10+
public ExampleData(String name, int num) {
11+
super();
12+
this.name = name;
13+
this.num = num;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public int getNum() {
21+
return num;
22+
}
23+
24+
public BigDecimal getNumAsBigDecimal() {
25+
return BigDecimal.valueOf(num);
26+
}
27+
28+
public BigDecimal getFoo() {
29+
return BigDecimal.valueOf(num * 10);
30+
}
31+
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package example;
2+
import java.util.LinkedList;
3+
import java.util.List;
4+
5+
public class TreeNode<T> {
6+
7+
T data;
8+
TreeNode<T> parent;
9+
List<TreeNode<T>> children;
10+
11+
public List<TreeNode<T>> getChildren() {
12+
return children;
13+
}
14+
15+
public TreeNode<T> getParent() {
16+
return parent;
17+
}
18+
19+
public T getData() {
20+
return data;
21+
}
22+
23+
public TreeNode(T data) {
24+
this.data = data;
25+
this.children = new LinkedList<TreeNode<T>>();
26+
}
27+
28+
public TreeNode(TreeNode<T> n) {
29+
this.data = n.getData();
30+
this.children = n.getChildren();
31+
}
32+
33+
public TreeNode<T> addChild(T child) {
34+
TreeNode<T> childNode = new TreeNode<T>(child);
35+
childNode.parent = this;
36+
this.children.add(childNode);
37+
return childNode;
38+
}
39+
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package example.genericJ8;
2+
3+
import java.util.stream.Stream;
4+
5+
import example.TreeNode;
6+
7+
public class ExtendedTreeNode<T> extends TreeNode<T> {
8+
9+
public ExtendedTreeNode(TreeNode<T> node) {
10+
super(node);
11+
}
12+
13+
public Stream<ExtendedTreeNode<T>> flattened() {
14+
return Stream.concat(Stream.of(this),
15+
getChildren().stream()
16+
// extend
17+
.map(ExtendedTreeNode<T>::new)
18+
// recurse
19+
.flatMap(ExtendedTreeNode<T>::flattened));
20+
}
21+
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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 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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package example.simple;
2+
import org.apache.commons.lang3.StringUtils;
3+
4+
import example.ExampleData;
5+
import example.TreeNode;
6+
7+
public class TreeUtilSimple {
8+
9+
public static String printTree(TreeNode<ExampleData> treeNode) {
10+
StringBuilder sb = new StringBuilder();
11+
printTree(treeNode, sb, 0);
12+
return sb.toString();
13+
}
14+
15+
private static void printTree(TreeNode<ExampleData> treeNode, StringBuilder sb, int indent) {
16+
if (treeNode != null) {
17+
sb.append(StringUtils.repeat(" ", indent) + treeNode.getData().getName() + "\n");
18+
if (treeNode.getChildren() != null) {
19+
for (TreeNode<ExampleData> data : treeNode.getChildren()) {
20+
printTree(data, sb, indent+1);
21+
}
22+
}
23+
}
24+
}
25+
26+
}

src/main/resources/logback.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+
<layout class="ch.qos.logback.classic.PatternLayout">
6+
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
7+
</layout>
8+
</appender>
9+
10+
<logger name="hashtoken" level="TRACE"/>
11+
12+
13+
<root level="debug">
14+
<appender-ref ref="STDOUT" />
15+
</root>
16+
</configuration>

0 commit comments

Comments
 (0)