Created
June 4, 2020 23:24
-
-
Save grahamcrowell/7dd676c70360377fbd0e4829abe68360 to your computer and use it in GitHub Desktop.
Java Hierarchy Fluent Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.foo; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import org.junit.jupiter.api.Test; | |
public class Node { | |
private static final String ROOT = "ROOT"; | |
String name; | |
String alias; | |
List<Node> children; | |
public Node(Builder builder) { | |
this.name = builder.name; | |
this.alias = builder.alias; | |
this.children = builder.children.stream().map(Builder::build).collect(Collectors.toList()); | |
} | |
public static Builder builder() { | |
return new Builder(); | |
} | |
public static Builder builder(String name) { | |
return new Builder(name); | |
} | |
public static class Builder { | |
String name; | |
String alias; | |
List<Builder> children; | |
public Builder() { | |
this.name = ROOT; | |
this.children = new ArrayList<>(); | |
} | |
public Builder(String name) { | |
this.name = name; | |
this.children = new ArrayList<>(); | |
} | |
public Builder withChild(Builder child) { | |
this.children.add(child); | |
return this; | |
} | |
public Builder withChildren(List<Builder> builders) { | |
builders.forEach(this::withChild); | |
return this; | |
} | |
public Node build() { | |
return new Node(this); | |
} | |
} | |
// | |
// @Test | |
// void testTrivial() { | |
// Node tree = Node.builder() | |
// .withChild(Node.builder("name")) | |
// .build(); | |
// // 1st layer | |
// assertEquals(1, tree.children.size()); | |
// assertEquals("name", tree.children.get(0).name); | |
// | |
// } | |
// | |
// @Test | |
// void test2Child() { | |
// Node tree = Node.builder() | |
// .withChild(Node.builder("one")) | |
// .withChild(Node.builder("two")) | |
// .build(); | |
// // 1st layer | |
// assertEquals(2, tree.children.size()); | |
// assertEquals("one", tree.children.get(0).name); | |
// assertEquals("two", tree.children.get(1).name); | |
// // 2nd layer | |
// assertEquals(0, tree.children.get(0).children.size()); | |
// assertEquals(0, tree.children.get(1).children.size()); | |
// | |
// } | |
// | |
// | |
// @Test | |
// void test2Deep() { | |
// Node tree = Node.builder() | |
// .withChild(Node.builder("one") | |
// .withChild(Node.builder("one.one")) | |
// .withChild(Node.builder("one.two")) | |
// ) | |
// .withChild(Node.builder("two")) | |
// .build(); | |
// // 1st layer | |
// assertEquals(2, tree.children.size()); | |
// assertEquals("one", tree.children.get(0).name); | |
// assertEquals("two", tree.children.get(1).name); | |
// // 2nd layer | |
// assertEquals(2, tree.children.get(0).children.size()); | |
// assertEquals("one.one", tree.children.get(0).children.get(0).name); | |
// assertEquals("one.two", tree.children.get(0).children.get(1).name); | |
// assertEquals(0, tree.children.get(1).children.size()); | |
// // 3rd layer | |
// assertEquals(0, tree.children.get(0).children.get(0).children.size()); | |
// assertEquals(0, tree.children.get(0).children.get(1).children.size()); | |
// } | |
// | |
// @Test | |
// void test3Deep() { | |
// Node tree = Node.builder() | |
// .withChild(Node.builder("one") | |
// .withChild(Node.builder("one.one")) | |
// .withChild(Node.builder("one.two") | |
// .withChild(Node.builder("one.two.one"))) | |
// ) | |
// .withChild(Node.builder("two")) | |
// .build(); | |
// // 1st layer | |
// assertEquals(2, tree.children.size()); | |
// assertEquals("one", tree.children.get(0).name); | |
// assertEquals("two", tree.children.get(1).name); | |
// // 2nd layer | |
// assertEquals(2, tree.children.get(0).children.size()); | |
// assertEquals("one.one", tree.children.get(0).children.get(0).name); | |
// assertEquals("one.two", tree.children.get(0).children.get(1).name); | |
// assertEquals(0, tree.children.get(1).children.size()); | |
// // 3rd layer | |
// assertEquals(0, tree.children.get(0).children.get(0).children.size()); | |
// assertEquals(1, tree.children.get(0).children.get(1).children.size()); | |
// assertEquals("one.two.one", tree.children.get(0).children.get(1).children.get(0).name); | |
// // 4th layer | |
// assertEquals(0, tree.children.get(0).children.get(1).children.get(0).children.size()); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment