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

jadeutils.datastructures.TreeNodeHelper Maven / Gradle / Ivy

The newest version!
/*
 * 
 */
package jadeutils.datastructures;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class TreeNodeHelper {

	/*
	 * make list to a tree
	 * 
	 * @param root
	 *            root note
	 * @param nodes
	 *            list of all nodes
	 * @param map
	 *            map of nodeId => node
	 * @param cper
	 *            comparator for sort
	 */
	@SuppressWarnings("unchecked")
	public void genTree(T root, List nodes, Map map, Comparator cper) {
		TreeNodeItf rootNode = (TreeNodeItf) root;
		if (null == root || null == nodes || nodes.size() == 0) {
			return;
		} else {
			for (T st : nodes) {
				TreeNodeItf n = (TreeNodeItf) st;
				if (null != n.getParentNodeId()
						&& n.getParentNodeId().equals(rootNode.getId())) {
					map.put(n.getId(), st);
					n.setParentNode((T) root);
					if (null == rootNode.getSubNodeList()) {
						rootNode.setSubNodeList(//
						new ArrayList());
					}
					rootNode.getSubNodeList().add(st);
				}
			}
		}
		if (null == rootNode.getSubNodeList()
				|| rootNode.getSubNodeList().size() == 0) {
			return;
		} else {
			if (null != cper) {
				Collections.sort(rootNode.getSubNodeList(), cper);
			}
			for (T r : rootNode.getSubNodeList()) {
				this.genTree(r, nodes, map, cper);
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy