Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active October 27, 2022 11:50
Show Gist options
  • Save KrabCode/509e50981fe9595cb0a44fd1a013a7c9 to your computer and use it in GitHub Desktop.
Save KrabCode/509e50981fe9595cb0a44fd1a013a7c9 to your computer and use it in GitHub Desktop.
void setup() {
NodeFolder root = new NodeFolder("root");
root.addChild(new IntNode("nerdsSniped", 32));
root.addChild(new StringNode("cheese", "gouda"));
NodeFolder cats = new NodeFolder("cats");
cats.addChild(new IntNode("kittens", 3));
cats.addChild(new IntNode("chomkers", 7));
StringNode catSentiment = new StringNode("sentiment", "cute");
cats.addChild(catSentiment);
root.addChild(cats);
NodeFolder dogs = new NodeFolder("dogs");
dogs.addChild(new IntNode("puppers", 8));
dogs.addChild(new IntNode("fluffers", 2));
StringNode dogSentiment = new StringNode("sentiment", "yummy");
dogs.addChild(dogSentiment);
root.addChild(dogs);
prettyPrintTree(root);
copyValue(catSentiment, dogSentiment);
}
void copyValue(AbstractNode source, AbstractNode target){
// TODO pls implement me in a nice generic way
// without the ugly a.getClass().getSimpleName().equals(b.getClass().getSimpleName()) to check for valid casts
}
abstract class AbstractNode {
String name;
boolean isFolder = false;
AbstractNode(String name) {
this.name = name;
}
String getPrintableValue() {
return null;
}
}
class NodeFolder extends AbstractNode {
ArrayList<AbstractNode> children = new ArrayList<AbstractNode>();
NodeFolder(String name) {
super(name);
isFolder = true;
}
void addChild(AbstractNode node) {
children.add(node);
}
}
class IntNode extends AbstractNode {
int valueInt;
IntNode(String name, int value) {
super(name);
this.valueInt = value;
}
String getPrintableValue() {
return String.valueOf(valueInt);
}
}
class StringNode extends AbstractNode {
String valueString;
StringNode(String name, String valueString) {
super(name);
this.valueString = valueString;
}
String getPrintableValue() {
return String.valueOf(valueString);
}
}
static void prettyPrintTree(NodeFolder root) {
StringBuilder sb = new StringBuilder();
buildPrettyPrintedTreeString(root, 1, sb);
println(sb);
}
private static void buildPrettyPrintedTreeString(AbstractNode node, int depth, StringBuilder outputBuilder) {
StringBuilder prefix = new StringBuilder();
boolean hasChildren = node.isFolder && ((NodeFolder) node).children.size() > 0;
for (int i = 0; i < depth; i++) {
boolean atMaxDepth = i == depth - 1;
if (atMaxDepth) {
prefix.append(hasChildren ? "+ " : "- ");
} else {
prefix.append("| ");
}
}
String nodeValue = node.getPrintableValue();
boolean hasValue = nodeValue != null && nodeValue.length() > 0;
outputBuilder.append(prefix)
.append(node.name)
.append(hasValue ? ": " : "")
.append(nodeValue == null ? "" : nodeValue)
.append("\n");
if (node.isFolder) {
NodeFolder folder = (NodeFolder) node;
for (AbstractNode child : folder.children) {
buildPrettyPrintedTreeString(child, depth + 1, outputBuilder);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment