All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unitils.objectvalidation.utils.TreeNode Maven / Gradle / Ivy

There is a newer version: 1.1.9
Show newest version
package org.unitils.objectvalidation.utils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


/**
 * Represents the hierarchy of a specific class, with his parent node and his child nodes.
 * 
 * @author Jeroen Horemans
 * @since Feb 20, 2012
 */
public class TreeNode {

    private static final String IDENTATION = "   ";

    private Class value;

    private TreeNode parent;

    private List childs;
    private List genericSubtype = new ArrayList();

    public TreeNode(Class value) {
        this.value = value;
        childs = new LinkedList();
    }
    
    public void addChild(TreeNode child) {
        child.setParent(this);
        childs.add(child);
    }

    public TreeNode addChild(Class child) {
        TreeNode node = new TreeNode(child);
        addChild(node);
        return node;
    }
    
    public void setGenericSubtype(List genericSubtype) {
        this.genericSubtype = genericSubtype;
    }
    
    public List getGenericSubtype() {
        return genericSubtype;
    }

    public Class getValue() {
        return value;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public List getChilds() {
        return childs;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("\n");
        return toString(this, builder, "").toString();
    }

    public StringBuilder toString(TreeNode node, StringBuilder builder, String idents) {

        builder.append(idents).append("->").append(node.getValue().getName()).append("\n");

        for (TreeNode child : node.getChilds()) {
            toString(child, builder, idents + IDENTATION);
        }

        return builder;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy