completed nodes

This commit is contained in:
Erik Bročko 2018-06-29 22:56:40 +02:00
parent 5169427a28
commit fea89fc1f8
6 changed files with 142 additions and 70 deletions

View File

@ -1,11 +1,8 @@
package eu.lixko.ext.forraceexpander; package eu.lixko.ext.forraceexpander;
import eu.lixko.ext.forraceexpander.components.Node;
import eu.lixko.ext.forraceexpander.components.Parent; import eu.lixko.ext.forraceexpander.components.Parent;
import eu.lixko.ext.forraceexpander.components.Property;
import eu.lixko.ext.forraceexpander.exporter.TreeSerializerProvider; import eu.lixko.ext.forraceexpander.exporter.TreeSerializerProvider;
import eu.lixko.ext.forraceexpander.exporter.TreeTestSerializer; import eu.lixko.ext.forraceexpander.exporter.TreeTestSerializer;
import eu.lixko.ext.forraceexpander.importer.OrderImporter;
public class Main { public class Main {
@ -13,18 +10,16 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Parent main = new Parent("main"); Parent main = new Parent("main");
Parent child = new Parent("child1"); main.createParent("child1")
child.addProperty("prop", "sumtext"); .createProperty("tag1", 5).parent()
.attr("field1", "yes, nodes can have attributes")
Property<String> prop = new Property<>("property1"); .createProperty("tag2", "hello").parent()
prop.setData("ayy lmao"); .createProperty("somefloat", 2.0f);
child.addCloneChild(prop);
main.addCloneChild(child);
System.out.println(">>> " + main.getChildByNameFirst("child1"));
main.createParent("child2")
.createProperty("another_tag", 10).parent()
.createProperty("tag2", "some string again");
TreeSerializerProvider serializer = new TreeTestSerializer(); TreeSerializerProvider serializer = new TreeTestSerializer();
System.out.println(serializer.serialize(main)); System.out.println(serializer.serialize(main));

View File

@ -1,7 +1,6 @@
package eu.lixko.ext.forraceexpander.components; package eu.lixko.ext.forraceexpander.components;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
public class Address extends SerializableComponent { public class Address extends SerializableComponent {
private ArrayList<String> addressLines = new ArrayList<>(); private ArrayList<String> addressLines = new ArrayList<>();

View File

@ -1,48 +1,59 @@
package eu.lixko.ext.forraceexpander.components; package eu.lixko.ext.forraceexpander.components;
import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
public class Node { public abstract class Node implements Cloneable {
public String type = "node"; protected Parent parent = null;
protected Node parent = null;
protected String name = ""; protected String name = "";
protected HashMap<String, String> properties = new HashMap<>(); protected HashMap<String, Serializable> properties = new HashMap<>();
public Node(Node parent, String name) { public Node(Parent parent, String name) {
this.parent = parent; this.parent = parent;
this.name = name; this.name = name;
} }
public Node(Node src) {
this.parent = src.parent;
this.name = src.name;
this.properties = new HashMap<String, String>(src.properties);
this.type = src.type;
}
public Node(String name) { public Node(String name) {
this.name = name; this.name = name;
} }
public Node getParent() { public Parent parent() {
return this.parent; return this.parent;
} }
public String getName() { public String name() {
return this.name; return this.name;
} }
public HashMap<String, String> getProperties() { public void name(String name) {
this.name = name;
}
public HashMap<String, Serializable> getProperties() {
return this.properties; return this.properties;
} }
public void addProperty(String name, String value) { public Node attr(String name, Serializable value) {
properties.put(name, value); properties.put(name, value);
return this;
} }
public String getProperty(String name) { public Serializable attr(String name) {
return properties.get(name); return properties.get(name);
} }
public boolean hasAttr(String name) {
return properties.containsKey(name);
}
@Override
public Node clone() throws CloneNotSupportedException {
Node n = (Node) super.clone();
n.name = this.name;
n.parent = this.parent;
n.properties = new HashMap<>(this.properties);
return n;
}
} }

View File

@ -1,27 +1,19 @@
package eu.lixko.ext.forraceexpander.components; package eu.lixko.ext.forraceexpander.components;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
public class Parent extends Node { public class Parent extends Node implements Cloneable {
private ArrayList<Node> children = new ArrayList<>(); private ArrayList<Node> children = new ArrayList<>();
public Parent(Parent src) {
super(src);
for(Node c : src.children) {
this.children.add(new Node(c));
}
}
public Parent(String name) { public Parent(String name) {
super(name); super(name);
this.type = "parent";
} }
public Parent(Parent parent, String name) { public Parent(String name, Parent parent) {
super(name); super(name);
this.parent = parent; this.parent = parent;
this.type = "parent";
} }
public ArrayList<Node> getChildren() { public ArrayList<Node> getChildren() {
@ -32,24 +24,59 @@ public class Parent extends Node {
this.children.add(child); this.children.add(child);
} }
public void addCloneChild(Parent child) { public void addCloneChild(Node child) {
Parent p = new Parent(child); try {
p.parent = this; Node cc = child.clone();
this.children.add(p); cc.parent = this;
this.children.add(cc);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
} }
public void addCloneChild(Property<?> child) { public <T extends Serializable> Property<T> createProperty(String name, T value) {
Property<?> n = new Property<>(child); Property<T> p = new Property<T>(name, value, this);
n.parent = this; this.addChild(p);
this.children.add(n); return p;
} }
public Node getChildByNameFirst(String name) { public Parent createParent(String name) {
Parent p = new Parent(name, this);
this.addChild(p);
return p;
}
public Node first(String name) {
for(Node n : children) { for(Node n : children) {
if(n.getName() == name) if(n.name() == name)
return n; return n;
} }
return null; return null;
} }
public Parent firstParent(String name) {
for(Node n : children) {
if(!(n instanceof Parent))
continue;
if(n.name() == name)
return (Parent) n;
}
return null;
}
public Parent attr(String name, Serializable value) {
return (Parent) super.attr(name, value);
}
@Override
public Parent clone() throws CloneNotSupportedException {
Node x = super.clone();
Parent p = (Parent) x;
p.children = new ArrayList<>();
for(Node c : this.children) {
p.children.add(c.clone());
}
return p;
}
} }

View File

@ -1,17 +1,24 @@
package eu.lixko.ext.forraceexpander.components; package eu.lixko.ext.forraceexpander.components;
public class Property<T> extends Node { import java.io.Serializable;
public class Property<T extends Serializable> extends Node {
private T data; private T data;
public Property(Property<T> src) {
super(src);
this.data = src.data;
}
public Property(String name) { public Property(String name) {
super(name); super(name);
this.type = "prop"; }
public Property(String name, T data) {
super(name);
this.data = data;
}
public Property(String name, T data, Parent p) {
super(name);
this.data = data;
this.parent = p;
} }
public T getData() { public T getData() {
@ -22,9 +29,18 @@ public class Property<T> extends Node {
this.data = data; this.data = data;
} }
@SuppressWarnings("unchecked")
public Property<T> attr(String name, Serializable value) {
return (Property<T>) super.attr(name, value);
}
@Override @Override
public Object clone() throws CloneNotSupportedException { @SuppressWarnings("unchecked")
return super.clone(); public Property<T> clone() throws CloneNotSupportedException {
Node x = super.clone();
Property<T> p = (Property<T>) x;
p.data = this.data;
return p;
} }
} }

View File

@ -1,5 +1,8 @@
package eu.lixko.ext.forraceexpander.exporter; package eu.lixko.ext.forraceexpander.exporter;
import java.io.Serializable;
import java.util.Map;
import eu.lixko.ext.forraceexpander.components.Node; import eu.lixko.ext.forraceexpander.components.Node;
import eu.lixko.ext.forraceexpander.components.Parent; import eu.lixko.ext.forraceexpander.components.Parent;
import eu.lixko.ext.forraceexpander.components.Property; import eu.lixko.ext.forraceexpander.components.Property;
@ -12,23 +15,44 @@ public class TreeTestSerializer implements TreeSerializerProvider {
} }
public String serialize(Node n, int level) { public String serialize(Node n, int level) {
String res = ""; String res = "\n";
for(int i = 0; i < level; i++) for(int i = 0; i < level; i++)
res += "\t"; res += '\t';
if(n instanceof Parent) { if(n instanceof Parent) {
Parent p = (Parent) n; Parent p = (Parent) n;
res += "P " + p.getName() + ": \n"; res += p.name();
if(!p.getProperties().isEmpty()) {
res += " (";
int i = p.getProperties().size();
for (Map.Entry<String, Serializable> entry : p.getProperties().entrySet()) {
i--;
res += entry.getKey();
if(entry.getValue() != null) {
res += ": ";
if(entry.getValue() instanceof String)
res += "\"" + entry.getValue() + "\"";
else
res += entry.getValue();
res += i == 0 ? "" : ", ";
}
}
res += ')';
}
res += ':';
for(Node c : p.getChildren()) { for(Node c : p.getChildren()) {
res += serialize(c, level + 1); res += serialize(c, level + 1);
} }
} else if(n instanceof Property) { } else if(n instanceof Property) {
Property<?> p = (Property<?>) n; Property<?> p = (Property<?>) n;
res += "O " + p.getName() + " = " + p.getData(); res += p.getData().getClass().getSimpleName() + " " + p.name() + ": ";
if(p.getData() instanceof String)
res += "\"" + p.getData() + "\"";
else
res += p.getData();
} else { } else {
System.out.println("EXC: " + n.getName() + " / " + n.type); res += "else";
System.out.println(n.getClass());
System.out.println("> " + (Property.class.isAssignableFrom(n.getClass())));
res += "else\n";
} }
return res; return res;
} }