树工具类

张军 3098 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package zj.tree.util;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
 
import zj.java.util.JavaUtil;
import zj.reflect.util.FieldUtil;
 
/**
 * 类名 :TreeUtil<br>
 * 概况 :树工具类<br>
 
 * @version 1.00 (2014.09.15)
 * @author SHNKCS 张军 {@link  <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a>&nbsp;&nbsp;&nbsp;&nbsp;<a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>}
 */
public class TreeUtil implements Serializable {
    private static final long serialVersionUID = 1L;
 
    /**
     * 回调取机构父对象
     
     * @param p_entity
     * @param p_coll
     */
    @SuppressWarnings("unchecked")
    public static <T> void callPentity(T p_entity, List<T> p_coll, String p_pentityName){
        if (p_entity == null)
            return;
        p_coll.add(p_entity);
        Object objEntity = null;
        try {
            objEntity = FieldUtil.get(p_entity, p_pentityName, true);
        catch (Exception e) {
            e.printStackTrace();
        }
        if (objEntity==null){
            return;
        }
        callPentity((T) objEntity, p_coll, p_pentityName);
    }
 
    /**
     * 取得树路径
     
     * @param p_entity
     */
    public static <T> String entityTextJoin(T p_entity, String text, String p_pentityName){
        return entityTextJoin(p_entity, text, p_pentityName, "\\");
    }
 
    /**
     * 取得树路径
     
     * @param p_entity
     */
    public static <T> String entityTextJoin(T p_entity, String text, String p_pentityName, String split) {
        List<T> coll = new ArrayList<T>();
        callPentity(p_entity, coll, p_pentityName);
        StringBuffer textJoin = new StringBuffer(200);
        for (int i = coll.size() - 1; i >= 0; i--) {
            T entity = coll.get(i);
            if (!"".equals(textJoin.toString())) {
                textJoin.append(split);
            }
            Object objText = null;
            try {
                objText = FieldUtil.get(entity, text, true);
            catch (Exception e) {
                e.printStackTrace();
            }
            textJoin.append(JavaUtil.objToStr(objText));
        }
        return textJoin.toString();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package zj.tree.bean;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import zj.reflect.util.FieldUtil;
/**
 * 类名 :TreeUtil<br>
 * 概况 :树工具类<br>
 
 * @version 1.00 (2014.09.15)
 * @author SHNKCS 张军 {@link  <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a>&nbsp;&nbsp;&nbsp;&nbsp;<a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>}
 */
public class TreeNode<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private String idName;
    private String pidName;
    private boolean isPidT;
    public TreeNode(String idName, String pidName) {
        this(idName, pidName, false);
    }
    public TreeNode(String idName, String pidName, boolean isPidT) {
        this.idName = idName;
        this.isPidT = isPidT;
        this.pidName = pidName;
    }
    /** 返回所有子节点的集合 **/
    private List<String> rtnListId = new ArrayList<String>();
    /** 返回所有子节点的集合 **/
    private List<T> rtnListNode = new ArrayList<T>();
 
    public List<String> getRtnListId() {
        return rtnListId;
    }
 
    public List<T> getRtnListNode() {
        return rtnListNode;
    }
 
    /**
     * 根据父节点的ID获取所有子节点
     
     * @param list
     *            分类表
     * @param id
     *            传入的父节点ID
     * @return String
     */
    public void setChildNodes(List<T> list, String id) throws Exception {
        setChildNodes(list, id, true);
    }
 
    /**
     * 根据父节点的ID获取所有子节点
     
     * @param list
     *            分类表
     * @param id
     *            传入的父节点ID
     * @return String
     */
    public void setChildNodes(List<T> list, String id, boolean self) throws Exception {
        if (list == null || list.size() == 0 || id == null || id.trim().equals(""))
            return;
        if (self)
            for (Iterator<T> iterator = list.iterator(); iterator.hasNext();) {
                T node = iterator.next();
                String nodeId = getIdFieldValue(node);
                if (id.equals(nodeId)) {
                    rtnListId.add(nodeId);
                    rtnListNode.add(node);
                    break;
                }
            }
        for (Iterator<T> iterator = list.iterator(); iterator.hasNext();) {
            T node = iterator.next();
            String nodePid = getPidFieldValue(node);
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (id.equals(nodePid)) {
                recursionFn(list, node);
            }
            // // 二、遍历所有的父节点下的所有子节点
            // if ("".equals(node.getParentId())) {
            // recursionFn(list, node);
            // }
        }
    }
 
    /**
     * 根据父节点的ID获取所有子节点
     
     * @param list
     *            分类表
     * @param pid
     *            传入的父节点ID
     * @return String
     */
    public void setChildNodesPid(List<T> list, String pid) throws Exception {
        setChildNodesPid(list, pid, true);
    }
 
    /**
     * 根据父节点的ID获取所有子节点
     
     * @param list
     *            分类表
     * @param pid
     *            传入的父节点ID
     * @param self
     *            是否包含自己
     * @return String
     */
    public void setChildNodesPid(List<T> list, String pid, boolean self) throws Exception {
        /** 返回所有子节点的集合 **/
        List<String> rtnListIds = new ArrayList<String>();
        /** 返回所有子节点的集合 **/
        List<T> rtnListNodes = new ArrayList<T>();
        if (list == null || list.size() == 0 || pid == null || pid.trim().equals(""))
            return;
        for (Iterator<T> iterator = list.iterator(); iterator.hasNext();) {
            T node = iterator.next();
            String nodePId = getPidFieldValue(node);
            if ((pid == null && nodePId == null) || (pid != null && pid.equals(nodePId))) {
                rtnListId.clear();
                rtnListNode.clear();
                String nodeId = getIdFieldValue(node);
                setChildNodes(list, nodeId, self);
                rtnListIds.addAll(rtnListId);
                rtnListNodes.addAll(rtnListNode);
            }
        }
        rtnListId.clear();
        rtnListNode.clear();
        rtnListId.addAll(rtnListIds);
        rtnListNode.addAll(rtnListNodes);
    }
 
    /**
     * 递归调用
     
     * @param list
     * @param node
     * @throws Exception
     */
    private void recursionFn(List<T> list, T node) throws Exception {
        List<T> childList = getChildList(list, node);// 得到子节点列表
        String nodeId = getIdFieldValue(node);
        if (hasChild(list, node)) {// 判断是否有子节点
            rtnListId.add(nodeId);
            rtnListNode.add(node);
            Iterator<T> it = childList.iterator();
            while (it.hasNext()) {
                T n = it.next();
                recursionFn(list, n);
            }
        else {
            rtnListId.add(nodeId);
            rtnListNode.add(node);
        }
    }
 
    // 判断是否有子节点
    public boolean hasChild(List<T> list, T node) throws Exception {
        return getChildList(list, node).size() > 0 true false;
    }
 
    // 得到子节点列表
    public List<T> getChildList(List<T> list, T node) throws Exception {
        return getChildList(list, node, false);
    }
 
    // 得到子节点列表
    public List<T> getChildList(List<T> list, T node, boolean delChild) throws Exception {
        List<T> nodeList = new ArrayList<T>();
        List<T> newNodeList = new ArrayList<T>();
        newNodeList.addAll(list);
        Iterator<T> it = newNodeList.iterator();
        String nodeId = getIdFieldValue(node);
        while (it.hasNext()) {
            T n = (T) it.next();
            String nodePid = getPidFieldValue(n);
            if (nodeId.equals(nodePid)) {
                nodeList.add(n);
                if (delChild) {
                    list.remove(n);
                }
            }
        }
        return nodeList;
    }
 
    // 得到子节点列表
    public List<T> getChildList(List<T> list, String id) throws Exception {
        return getChildList(list, id, false);
    }
 
    // 得到子节点列表
    public List<T> getChildList(List<T> list, String id, boolean delChild) throws Exception {
        List<T> nodeList = new ArrayList<T>();
        Iterator<T> it = list.iterator();
        while (it.hasNext()) {
            T n = (T) it.next();
            String nodeId = getIdFieldValue(n);
            if (nodeId.equals(id)) {
                return getChildList(list, n, delChild);
            }
        }
        return nodeList;
    }
 
    /**
     * 转对象为字符串
     
     * @param o
     * @return
     */
    private String objToStr(Object o) {
        return o == null "" : String.valueOf(o);
    }
 
    /**
     * 获取属性值ID
     
     * @param t
     * @param fieldName
     * @return
     * @throws Exception
     */
    private String getIdFieldValue(T t) throws Exception {
        return objToStr(FieldUtil.get(t, this.idName, true));
    }
 
    /**
     * 获取属性值PID
     
     * @param t
     * @param fieldName
     * @return
     * @throws Exception
     */
    private String getPidFieldValue(T t) throws Exception {
        Object o = FieldUtil.get(t, this.pidName, true);
        if (this.isPidT) {
            if (o == null)
                return "";
            return objToStr(FieldUtil.get(o, this.idName, true));
        else {
            return objToStr(o);
        }
    }
 
}



更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论