我们通常会在应用中碰到树形结构的内容,比如 文件夹/文件模型, 部门组织结构,目录树等等,通常在设计模式中叫做 compose 模式。
在数据库中常常这样表示: 我们以Catalog (分级目录) 为例子
Catalog (分级目录)
|
字段名称
|
字段
|
类型
|
备注
|
目录ID
|
catalog_id
|
varchar(36)
|
pk, not null
|
目录名称
|
catalog_name
|
varchar(50)
|
not null
|
父目录ID
|
parent_id
|
varchar(36)
|
fk
|
创建时间
|
create_datetime
|
datetime
|
not null
|
目录描述
|
description
|
varchar(200)
|
|
我们考虑在数据库中一次将所有数据读入内存,然后在内存中生成一个Tree,这样可以减少数据库的访问,增加性能,并且只有的数据方式改变的时候,全部重新从数据库中生成Tree,否则一直保持在内存中。
我们使用标准的DAO模式先生成 POJO类(Catalog)和DAO类(CatalogDAO)。
然后我们建立相对通用的 Tree 和 TreeNode 类。
Tree.java
package com.humpic.helper.tree;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public abstract class Tree {
protected static Log log = LogFactory.getLog(Tree.class);
private Map treeNodeMaps = new Hashtable();
private TreeNode root;
/**
* root if it's parent is empty
*/
protected void reload(List nodes) {
log.info("tree will start reload all data");
synchronized (this) {
// initialize
treeNodeMaps.clear();
root = null;
List treeNodes = new Vector(nodes.size());
for (int i = 0; i < nodes.size(); i++) {
TreeNode node = this.transform(nodes.get(i)); // transform
treeNodes.add(node);
node.setTree(this);
treeNodeMaps.put(node.getNodeId(), node);
}
for (int i = 0; i < treeNodes.size(); i++) {
TreeNode node = (TreeNode) treeNodes.get(i);
String parentId = node.getParentId();
if (this.isRootNode(node)) {
if (root == null) {
root = node;
} else {
log.error("find more then one root node. ignore.");
}
} else {
TreeNode parent = (TreeNode) treeNodeMaps.get(parentId);
if (parent != null) {
parent.addChild(node);
node.setParent(parent);
} else {
log.warn("node [id=" + node.getNodeId() + "]: missing parent node.");
}
}
}
}
if (root == null) {
log.error("the root node is not be defined");
}
}
protected boolean isRootNode(TreeNode node) {
return StringUtils.isBlank(node.getParentId());
}
public TreeNode getRootNode() {
return root;
}
public TreeNode getTreeNode(String nodeId) {
return (TreeNode) treeNodeMaps.get(nodeId);
}
public void addTreeNode(TreeNode node) {
synchronized (this) {
treeNodeMaps.put(node.getNodeId(), node);
String parentId = node.getParentId();
if (StringUtils.isNotBlank(parentId)) {
TreeNode parent = getTreeNode(parentId);
if (parent != null) {
parent.addChild(node);
node.setParent(parent);
} else {
log.error("parent cannot be found: " + node.getParentId());
}
} else {
if (root == null) {
root = node;
} else {
log.error("find more then one root node. ignore.");
}
}
}
}
public void deleteTreeNode(String nodeId) {
synchronized (this) {
TreeNode node = getTreeNode(nodeId);
if (node == null)
throw new IllegalArgumentException(nodeId + " cannot be found.");
if (node.getParent() == null) {
root = null;
treeNodeMaps.clear();
log.warn("the root node has been removed.");
} else {
node.getParent().getChildren().remove(node);
treeNodeMaps.remove(nodeId);
List children = node.getAllChildren();
for (int i = 0; i < children.size(); i++) {
TreeNode n = (TreeNode) children.get(i);
treeNodeMaps.remove(n.getNodeId());
}
}
}
}
/**
* <pre>
* Usage: Office ->
*
* public TreeNode transform(Object info) {
* OfficeInfo office_info = (OfficeInfo) info;
* TreeNode node = new TreeNode();
* node.setNodeId(office_info.getOfficeId());
* node.setParentId(office_info.getParentId());
* node.setBindData(office_info);
* return node;
* }
* </pre>
*/
protected abstract TreeNode transform(Object info);
}
TreeNode.java
package com.humpic.helper.tree;
import java.util.List;
import java.util.Vector;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.ObjectUtils;
public class TreeNode {
private Tree tree;
private TreeNode parent;
private List children = new Vector();
private List childrenGroup = new Vector();
private String nodeId;
private String parentId;
private Object bindData;
public String getNodeId() {
return nodeId;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public Object getBindData() {
return bindData;
}
public void setBindData(Object bindData) {
this.bindData = bindData;
}
public Tree getTree() {
return tree;
}
public void setTree(Tree tree) {
this.tree = tree;
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
public TreeNode getParent() {
return this.parent;
}
public List getChildren() {
return this.children;
}
public void addChild(TreeNode node) {
children.add(node);
}
/**
* get all children, and chilren's children
*/
public List getAllChildren() {
if (this.childrenGroup.isEmpty()) {
synchronized (this.tree) {
for (int i = 0; i < this.children.size(); i++) {
TreeNode node = (TreeNode) this.children.get(i);
this.childrenGroup.add(node);
this.childrenGroup.addAll(node.getAllChildren());
}
}
}
return this.childrenGroup;
}
/**
* get all children, and chilren's children
*/
public List getAllChildren(Predicate predicate) {
List groups = new Vector();
fillAllChildren(groups, predicate);
return groups;
}
private void fillAllChildren(List groups, Predicate predicate) {
for (int i = 0; i < this.children.size(); i++) {
TreeNode node = (TreeNode) this.children.get(i);
if (predicate.evaluate(node)) {
groups.add(node);
node.fillAllChildren(groups, predicate);
}
}
}
/**
* get all parents, and parent's parent
*/
public List getParents() {
List results = new Vector();
TreeNode parent = this.getParent();
while (parent != null) {
results.add(parent);
parent = parent.getParent();
}
return results;
}
/**
* A.isMyParent(B) == B is A' parent ? <br>
* root.isMyParent(null) == true; <br>
* root.isMyParent(*) == false <br>
* *.isMyParent(null) == false
*/
public boolean isMyParent(String nodeId) {
TreeNode target = tree.getTreeNode(nodeId);
TreeNode parent = this.getParent();
if (parent == null) {
return target == null;
} else {
return parent.equals(target);
}
}
/**
* A.isMyAncestor(B) == B is A' ancestor ? <br>
* *.isMyAncestor(null) == true;
*/
public boolean isMyAncestor(String nodeId) {
TreeNode target = tree.getTreeNode(nodeId);
if (target == null)
return true;
return target.getAllChildren().contains(this);
}
/**
* A.isMyBrother(B) == B is A' brother ? <br>
* *.isMyBrother(null) == false
*/
public boolean isMyBrother(String nodeId) {
TreeNode target = tree.getTreeNode(nodeId);
if (target == null)
return false;
TreeNode p1 = this.getParent();
TreeNode p2 = target.getParent();
return ObjectUtils.equals(p1, p2);
}
}
然后建立业务 Tree
CatalogTree.java
package com.humpic.helper.tree;
import java.util.List;
import org.apache.commons.collections.Predicate;
public class CatalogTree extends Tree {
private static CatalogTree instance = null;
private CatalogTree() {}
public static synchronized CatalogTree getInstance() {
if (instance == null) {
instance = new CatalogTree();
instance.reloadCatalogs();
}
return instance;
}
protected TreeNode transform(Object info) {
Catalog catalog = (Catalog) info;
TreeNode node = new TreeNode();
node.setNodeId(catalog.getCatalogId());
node.setParentId(catalog.getParentId());
node.setBindData(catalog);
return node;
}
public void reloadCatalogs() {
List nodes = CatalogDAO.getInstance().findAll();
super.reload(nodes);
}
public Catalog getCatalogNode(String catalogId) {
TreeNode node = super.getTreeNode(catalogId);
return node == null ? null : (Catalog) node.getBindData();
}
}
最后,我们只要使用以下的语句就可以了:
1. CatalogTree.getInstance().getTreeNode(...)
2. CatalogTree.getInstance().getCatalogNode(...)
3. CatalogTree.getInstance().getRootNode()
然后通过 TreeNode,就可以得到 parent, parents 和 children, allChildren
分享到:
相关推荐
java递归树型结构通用数据库
需要修改Hibernate的配置信息,并创建一个与数据库表同名的Java Bean类和一个Vo类并继承Vo(类名:数据库表名+Vo) 将代码部署在服务器上后,访问index.jsp,输入类名和分页大小,就可以生成该表的树形目录
基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...
基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...
就是将类用树形结构组合成一个单位.你向别人介绍你是某单位,你是单位中的一个元素,别人和你做买卖,相当于 和单位做买卖。文章中还对 Jive再进行了剖析。 设计模式之 Decorator(装饰器) Decorator 是个油漆工,给...
修改扩展界面支持库一,为“树形框”增加多态检查框功能,相应地添加了多个与检查框相关的属性、方法和事件。 17. 修改高级表格支持库,允许“复制选定文本()”“剪切选定文本()”在“允许选择块”属性为假时复制...
Treasure Box是一个免费的独立Java应用程序,可记录,显示和搜索... 该程序相当通用,可用于树形结构中的各种集合。 数字图像易于拍摄,并且存储成本很小。 宝藏盒可以帮助您整理您的宝藏,并在需要时帮助您找到它们。
树形艺术形式返回。 可以通过查询 Neo4j webapp 来创建网络可视化,如下所示。 数据模型应该通过原生 Neo4j API 以及 NetGrph 的树数据结构转换为与 D3.js、vis.js 或 Graphwiz 等工具一起使用。 所有数据都可以通过...
基于SSM整合的单一架构,使用SpringSecurity进行权限控制、使用zTree实现树形结构数据在页面的显示,并通过MyBatis的PageHelper插件实现分页。基于SpringMVC的异常映射实现了一套通用的错误消息管理机制,并做到了...
SqlToolBox是一款纯绿色的免费数据库客户端软件,基于Java Swing编制而成,旨在于为开发人员,系统工程师和数据库管理员提供一种通用方便和快捷的数据库操作工具,使他们摆脱需要学习掌握使用多种数据库客户端的苦恼...
jQuery EasyUI 提供了用于创建跨浏览器网页的完整的组件集合,包括功能强大的 datagrid(数据网格)、treegrid(树形表格)、 panel(面板)、combo(下拉组合)等等。 用户可以组合使用这些组件,也可以单独使用其中一个。 ...
对象浏览器——可配置的树形浏览能够显示同PL/SQL开发相关的全部信息,使用该浏览器可以获取对象描述、浏览对象定义、创建测试脚本以便调试、使能或禁止触发器或约束条件、重新编译不合法对象、查询或编辑表格、...