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

com.memfactory.utils.tree.common.BaseNodeService Maven / Gradle / Ivy

There is a newer version: 0.0.25
Show newest version
package com.memfactory.utils.tree.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.memfactory.utils.tree.bean.TreeNode;
import com.memfactory.utils.tree.util.TreeHelper;

/**
 * 节点基础服务实现
 * 
 * @author zhairp createDate: 2018-09-04
 */
public abstract class BaseNodeService implements NodeService {

	@Override
	public void collectParentNodes(TreeNode leafNode, List target) {
		target.add(leafNode);
		Long parentId = leafNode.getParentId();
		if (leafNode.isRoot(parentId)) {
			return;
		} else {
			TreeNode parentNode = getNode(parentId);
			collectParentNodes(parentNode, target);
		}
	}

	@Override
	public Map getRelativeNodes(List leafNodes) {
		if (!CollectionUtils.isEmpty(leafNodes)) {
			List tatalTarget = new ArrayList();
			leafNodes.forEach(leafNode -> {
				List target = new ArrayList();
				collectParentNodes(leafNode, target);
				tatalTarget.addAll(target);
			});
			Map result = new HashMap();
			tatalTarget.forEach(node -> {
				result.put(node.getId(), node);
			});
			return result;
		}
		return null;
	}

	@Override
	public TreeNode generateTree(List leafNodes) {
		if (!CollectionUtils.isEmpty(leafNodes)) {
			Map nodeMap = getRelativeNodes(leafNodes);
			TreeHelper.generateTree(nodeMap);
			return TreeHelper.buildTree(nodeMap);
		}
		return null;
	}

	@Override
	public TreeNode spanTree(TreeNode child) {
		if (null == child) {
			return null;
		}
		if (child.isRoot(child.getParentId())) {
			return child;
		} else {
			TreeNode parent = getNode(child.getParentId());
			parent.addChildNode(child);
			return spanTree(parent);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy