org.javabuilders.Node Maven / Gradle / Ivy
The newest version!
package org.javabuilders;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Represents a node being currently processed
* @author Jacek Furmankiewicz
*/
public class Node {
private Object mainObject = null;
private Node parent = null;
private String key = null;
private Map> childValues = new HashMap>();
private Set childNodes = new LinkedHashSet();
private Map properties = new HashMap();
private Set consumedKeys = new HashSet();
private boolean usePreInstantiatedRoot = false;
private Map customProperties = new HashMap();
/**
* Constructor (for use by Builder only)
* @param mainObject Main object being processed
* @param properties The list of properties for this object
*/
Node(Node parent, String key) {
this(parent,key,null);
}
/**
* Constructor
* @param mainObject Main object being processed
* @param properties The list of properties for this object
*/
public Node(String key, Map properties) {
this(null,key, properties);
}
/**
* Constructor
* @param parent Parent node (null is accepted if root)
* @param mainObject Main object being processed
*/
public Node(Node parent, String key, Map properties) {
this(parent,key, properties, null);
}
/**
* Constructor
* @param parent Parent node (null is accepted if root)
* @param mainObject Main object being processed
*/
public Node(Node parent, String key, Map properties, Object mainObject) {
if (key == null) {
throw new NullPointerException("key cannot be null");
}
this.key = key;
if (properties != null) {
this.properties = properties;
}
//automatically add to parent if there is one
if (parent != null) {
parent.addChildNode(this);
this.parent = parent;
}
if (mainObject != null) {
this.setMainObject(mainObject);
}
}
/**
* Returns the optional main object that was created by this node
* @return Main object
*/
public Object getMainObject() {
return mainObject;
}
/**
* Sets the optional main object that was created by this node
*/
public void setMainObject(Object object) {
mainObject = object;
}
/**
* @return The parent node (null if root)
*/
public Node getParent() {
return parent;
}
/**
* Returns the list of all the children nodes belonging to this parent
* @return
*/
public Map> getChildValues() {
return childValues;
}
/**
* Returns the list of all the children for a specific key
* @return
*/
public List