Added nodes

This commit is contained in:
Erik Bročko 2018-06-28 18:56:24 +02:00
parent 12033e0f6e
commit 5169427a28
7 changed files with 216 additions and 0 deletions

View File

@ -0,0 +1,37 @@
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.Property;
import eu.lixko.ext.forraceexpander.exporter.TreeSerializerProvider;
import eu.lixko.ext.forraceexpander.exporter.TreeTestSerializer;
import eu.lixko.ext.forraceexpander.importer.OrderImporter;
public class Main {
public static void main(String[] args) {
Parent main = new Parent("main");
Parent child = new Parent("child1");
child.addProperty("prop", "sumtext");
Property<String> prop = new Property<>("property1");
prop.setData("ayy lmao");
child.addCloneChild(prop);
main.addCloneChild(child);
System.out.println(">>> " + main.getChildByNameFirst("child1"));
TreeSerializerProvider serializer = new TreeTestSerializer();
System.out.println(serializer.serialize(main));
//OrderImporter orderImporter = new OrderImporter();
//orderImporter.run();
}
}

View File

@ -1,6 +1,7 @@
package eu.lixko.ext.forraceexpander.components;
public class Item extends SerializableComponent {
private long itemID = 0, productCode = 0, priceUnit = 0, priceTotal = 0, priceTotalWithTax = 0;
private int qty = 0;
private String name = "", ean = "";

View File

@ -0,0 +1,48 @@
package eu.lixko.ext.forraceexpander.components;
import java.util.HashMap;
public class Node {
public String type = "node";
protected Node parent = null;
protected String name = "";
protected HashMap<String, String> properties = new HashMap<>();
public Node(Node parent, String name) {
this.parent = parent;
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) {
this.name = name;
}
public Node getParent() {
return this.parent;
}
public String getName() {
return this.name;
}
public HashMap<String, String> getProperties() {
return this.properties;
}
public void addProperty(String name, String value) {
properties.put(name, value);
}
public String getProperty(String name) {
return properties.get(name);
}
}

View File

@ -0,0 +1,55 @@
package eu.lixko.ext.forraceexpander.components;
import java.util.ArrayList;
public class Parent extends Node {
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) {
super(name);
this.type = "parent";
}
public Parent(Parent parent, String name) {
super(name);
this.parent = parent;
this.type = "parent";
}
public ArrayList<Node> getChildren() {
return this.children;
}
public void addChild(Node child) {
this.children.add(child);
}
public void addCloneChild(Parent child) {
Parent p = new Parent(child);
p.parent = this;
this.children.add(p);
}
public void addCloneChild(Property<?> child) {
Property<?> n = new Property<>(child);
n.parent = this;
this.children.add(n);
}
public Node getChildByNameFirst(String name) {
for(Node n : children) {
if(n.getName() == name)
return n;
}
return null;
}
}

View File

@ -0,0 +1,30 @@
package eu.lixko.ext.forraceexpander.components;
public class Property<T> extends Node {
private T data;
public Property(Property<T> src) {
super(src);
this.data = src.data;
}
public Property(String name) {
super(name);
this.type = "prop";
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

View File

@ -0,0 +1,9 @@
package eu.lixko.ext.forraceexpander.exporter;
import eu.lixko.ext.forraceexpander.components.Node;
public interface TreeSerializerProvider {
public String serialize(Node n);
}

View File

@ -0,0 +1,36 @@
package eu.lixko.ext.forraceexpander.exporter;
import eu.lixko.ext.forraceexpander.components.Node;
import eu.lixko.ext.forraceexpander.components.Parent;
import eu.lixko.ext.forraceexpander.components.Property;
public class TreeTestSerializer implements TreeSerializerProvider {
@Override
public String serialize(Node n) {
return serialize(n, 0);
}
public String serialize(Node n, int level) {
String res = "";
for(int i = 0; i < level; i++)
res += "\t";
if(n instanceof Parent) {
Parent p = (Parent) n;
res += "P " + p.getName() + ": \n";
for(Node c : p.getChildren()) {
res += serialize(c, level + 1);
}
} else if(n instanceof Property) {
Property<?> p = (Property<?>) n;
res += "O " + p.getName() + " = " + p.getData();
} else {
System.out.println("EXC: " + n.getName() + " / " + n.type);
System.out.println(n.getClass());
System.out.println("> " + (Property.class.isAssignableFrom(n.getClass())));
res += "else\n";
}
return res;
}
}