completed nodes
This commit is contained in:
		| @@ -1,11 +1,8 @@ | ||||
| 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 { | ||||
|  | ||||
| @@ -13,18 +10,16 @@ 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")); | ||||
| 		main.createParent("child1") | ||||
| 				.createProperty("tag1", 5).parent() | ||||
| 					.attr("field1", "yes, nodes can have attributes") | ||||
| 				.createProperty("tag2", "hello").parent() | ||||
| 				.createProperty("somefloat", 2.0f); | ||||
| 		 | ||||
| 		main.createParent("child2") | ||||
| 				.createProperty("another_tag", 10).parent() | ||||
| 				.createProperty("tag2", "some string again"); | ||||
| 				 | ||||
| 		TreeSerializerProvider serializer = new TreeTestSerializer(); | ||||
| 		System.out.println(serializer.serialize(main)); | ||||
| 		 | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| package eu.lixko.ext.forraceexpander.components; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.LinkedHashMap; | ||||
|  | ||||
| public class Address extends SerializableComponent { | ||||
| 	private ArrayList<String> addressLines = new ArrayList<>(); | ||||
|   | ||||
| @@ -1,48 +1,59 @@ | ||||
| package eu.lixko.ext.forraceexpander.components; | ||||
|  | ||||
| import java.io.Serializable; | ||||
| import java.util.HashMap; | ||||
|  | ||||
| public class Node { | ||||
| public abstract class Node implements Cloneable { | ||||
| 	 | ||||
| 	public String type = "node"; | ||||
| 	protected Node parent = null; | ||||
| 	protected Parent parent = null; | ||||
| 	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.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() { | ||||
| 	public Parent parent() { | ||||
| 		return this.parent; | ||||
| 	} | ||||
| 	 | ||||
| 	public String getName() { | ||||
| 	public String 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; | ||||
| 	} | ||||
| 	 | ||||
| 	public void addProperty(String name, String value) { | ||||
| 	public Node attr(String name, Serializable value) { | ||||
| 		properties.put(name, value); | ||||
| 		return this; | ||||
| 	} | ||||
| 	 | ||||
| 	public String getProperty(String name) { | ||||
| 	public Serializable attr(String 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; | ||||
| 	} | ||||
| 	 | ||||
| } | ||||
|   | ||||
| @@ -1,27 +1,19 @@ | ||||
| package eu.lixko.ext.forraceexpander.components; | ||||
|  | ||||
| import java.io.Serializable; | ||||
| import java.util.ArrayList; | ||||
|  | ||||
| public class Parent extends Node { | ||||
| public class Parent extends Node implements Cloneable { | ||||
|  | ||||
| 	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) { | ||||
| 	public Parent(String name, Parent parent) { | ||||
| 		super(name); | ||||
| 		this.parent = parent; | ||||
| 		this.type = "parent"; | ||||
| 	} | ||||
|  | ||||
| 	public ArrayList<Node> getChildren() { | ||||
| @@ -32,24 +24,59 @@ public class Parent extends Node { | ||||
| 		this.children.add(child); | ||||
| 	} | ||||
| 	 | ||||
| 	public void addCloneChild(Parent child) { | ||||
| 		Parent p = new Parent(child); | ||||
| 		p.parent = this; | ||||
| 		this.children.add(p); | ||||
| 	public void addCloneChild(Node child) { | ||||
| 		try { | ||||
| 			Node cc = child.clone(); | ||||
| 			cc.parent = this; | ||||
| 			this.children.add(cc); | ||||
| 		} catch (CloneNotSupportedException e) { | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	public void addCloneChild(Property<?> child) { | ||||
| 		Property<?> n = new Property<>(child); | ||||
| 		n.parent = this; | ||||
| 		this.children.add(n); | ||||
| 	public <T extends Serializable> Property<T> createProperty(String name, T value) { | ||||
| 		Property<T> p = new Property<T>(name, value, this); | ||||
| 		this.addChild(p); | ||||
| 		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) { | ||||
| 			if(n.getName() == name) | ||||
| 			if(n.name() == name) | ||||
| 				return n; | ||||
| 		} | ||||
| 		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; | ||||
| 	}	 | ||||
| 	 | ||||
| } | ||||
|   | ||||
| @@ -1,17 +1,24 @@ | ||||
| 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; | ||||
| 	 | ||||
| 	public Property(Property<T> src) { | ||||
| 		super(src); | ||||
| 		this.data = src.data; | ||||
| 	} | ||||
|  | ||||
| 	public Property(String 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() { | ||||
| @@ -22,9 +29,18 @@ public class Property<T> extends Node { | ||||
| 		this.data = data; | ||||
| 	} | ||||
| 	 | ||||
| 	@SuppressWarnings("unchecked") | ||||
| 	public Property<T> attr(String name, Serializable value) { | ||||
| 		return (Property<T>) super.attr(name, value); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public Object clone() throws CloneNotSupportedException { | ||||
| 	    return super.clone(); | ||||
| 	@SuppressWarnings("unchecked") | ||||
| 	public Property<T> clone() throws CloneNotSupportedException { | ||||
| 		Node x = super.clone(); | ||||
| 		Property<T> p = (Property<T>) x; | ||||
| 		p.data = this.data; | ||||
| 	    return p; | ||||
| 	} | ||||
| 	 | ||||
| } | ||||
|   | ||||
| @@ -1,5 +1,8 @@ | ||||
| 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.Parent; | ||||
| import eu.lixko.ext.forraceexpander.components.Property; | ||||
| @@ -12,23 +15,44 @@ public class TreeTestSerializer implements TreeSerializerProvider { | ||||
| 	} | ||||
| 	 | ||||
| 	public String serialize(Node n, int level) { | ||||
| 		String res = ""; | ||||
| 		String res = "\n"; | ||||
| 		for(int i = 0; i < level; i++) | ||||
| 			res += "\t"; | ||||
| 			res += '\t'; | ||||
| 		if(n instanceof Parent) { | ||||
| 			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()) { | ||||
| 				res += serialize(c, level + 1); | ||||
| 			} | ||||
| 		} else if(n instanceof Property) { | ||||
| 			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 { | ||||
| 			System.out.println("EXC: " + n.getName() + " / " + n.type); | ||||
| 			System.out.println(n.getClass()); | ||||
| 			System.out.println("> " + (Property.class.isAssignableFrom(n.getClass()))); | ||||
| 			res += "else\n"; | ||||
| 			res += "else"; | ||||
| 		} | ||||
| 		return res; | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user