shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.walker.cache.tree;
 
import com.walker.infrastructure.utils.Assert;
import com.walker.infrastructure.utils.StringUtils;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
 
/**
 * 默认的缓存树节点实现
 * @author shikeying
 *
 */
public class DefaultCacheTreeNode implements CacheTreeNode {
 
    protected String key;
    protected String text;
    protected Object source;
    
    private String parentId;
    
    private int order = 0;
    
    protected TreeMap<String, CacheTreeNode> children = new TreeMap<String, CacheTreeNode>();
    
    private boolean checked = false;
    private boolean opened = false;
    
    private String icon = null;
    
    public DefaultCacheTreeNode(String key, String text
            , Object source, String parentId){
        assert(StringUtils.isNotEmpty(key));
        assert(StringUtils.isNotEmpty(text));
        this.key = key;
        this.text = text;
        this.source = source;
        if(StringUtils.isNotEmpty(parentId))
            this.parentId = parentId;
    }
    
    public DefaultCacheTreeNode(String key, String text
            , Object source, String parentId, int orderNum){
        this(key, text, source, parentId);
        this.order = orderNum;
    }
    
    @Override
    public String getKey() {
        return key;
    }
 
    @Override
    public String getText() {
        return text;
    }
 
    @Override
    public Object getSource() {
//        throw new UnsupportedOperationException("no source.");
        return this.source;
    }
 
    @Override
    public boolean hasChild() {
        return this.children.size() > 0;
    }
 
    @Override
    public int getChildrenSize() {
        return this.children.size();
    }
 
    @Override
    public Collection<CacheTreeNode> getChildren() {
        if(hasChild())
            return children.values();
        return null;
    }
 
    @Override
    public CacheTreeNode search(String key) {
        if(StringUtils.isEmpty(key)) return null;
        if(this.key.equals(key))
            return this;
        if(!hasChild())
            return null;
        
        CacheTreeNode child = null;
        for(Iterator<CacheTreeNode> i = children.values().iterator(); i.hasNext();){
            child = i.next().search(key);
            if(child != null)
                return child;
        }
        return null;
    }
    
    @Override
    public List<CacheTreeNode> searchLike(String keyLike){
//        throw new UnsupportedOperationException();
        if(StringUtils.isEmpty(keyLike)) return null;
        List<CacheTreeNode> list = new ArrayList<>(8);
        if(this.key.indexOf(keyLike) >= 0 
                || this.text.indexOf(keyLike) >= 0){
            list.add(this);
        }
        if(hasChild()){
            List<CacheTreeNode> child = null;
            for(Iterator<CacheTreeNode> i = children.values().iterator(); i.hasNext();){
                child = i.next().searchLike(keyLike);
                if(child != null){
                    list.addAll(child);
                }
            }
        }
        return list;
    }
    
    @Override
    public List<CacheTreeNode> searchLike(String[] keys){
//        throw new UnsupportedOperationException();
        if(keys == null) return null;
        List<CacheTreeNode> list = new ArrayList<>(8);
        
        if(this.containKeyLike(this.key, keys) 
                || this.containKeyLike(this.text, keys)){
            list.add(this);
        }
        if(hasChild()){
            List<CacheTreeNode> child = null;
            for(Iterator<CacheTreeNode> i = children.values().iterator(); i.hasNext();){
                child = i.next().searchLike(keys);
                if(child != null){
                    list.addAll(child);
                }
            }
        }
        return list;
    }
    
    /**
     * 给定的关键词必须是并列存在的,有一个不匹配就返回失败
     * @param content
     * @param keys
     * @return
     */
    private boolean containKeyLike(String content, String[] keys){
        for(String k : keys){
            if(content.indexOf(k) < 0){
                return false;
            }
        }
        return true;
    }
 
    @Override
    public void addChild(CacheTreeNode node) {
        if(node != null){
            if(this.search(node.getKey()) == null){
                children.put(node.getKey(), node);
            } 
//            else
//                System.out.println("当前节点已经存在孩子对象,不再添加: " + node.getKey());
        }
    }
 
    @Override
    public String getParentId() {
        return this.parentId;
    }
 
    public String toString(){
        StringBuilder sb = new StringBuilder().append("{key = ").append(key)
                .append(", text = ").append(text)
                .append(", parentId = ").append(parentId)
                .append(", order = ").append(order);
        sb.append(",[");
        for(CacheTreeNode node : this.children.values()){
            sb.append(node.getKey());
            sb.append(":");
            sb.append(node.getText());
            sb.append(",");
        }
        sb.append("]");
        return sb.toString();
    }
 
    @Override
    public CacheTreeNode remove(String key) {
        if(StringUtils.isEmpty(key)) return null;
        if(!hasChild()){
            return null;
        }
        CacheTreeNode removed = children.remove(key);
        if(removed != null)
            return removed;
        for(Iterator<CacheTreeNode> i = children.values().iterator(); i.hasNext();){
            removed = i.next().remove(key);
            if(removed != null){
                return removed;
            }
        }
        return null;
    }
 
    @Override
    public void cloneProperties(CacheTreeNode node) {
        this.text = node.getText();
        this.source = node.getSource();
        this.order = node.getOrder();
    }
 
    @Override
    public int getOrder() {
        return this.order;
    }
 
    @Override
    public CacheTreeNode setOrder(int orderNum) {
        this.order = orderNum;
        return this;
    }
 
    @Override
    public int compareTo(CacheTreeNode o) {
        if(o == null) return 1;
        if(this == o) return 0;
        if(this.order > o.getOrder())
            return 1;
        else if(this.order < o.getOrder()){
            return -1;
        }
        return 0;
    }
 
    @Override
    public boolean isChecked() {
        return this.checked;
    }
 
    @Override
    public boolean isOpen() {
        return this.opened;
    }
 
    @Override
    public void setChecked(boolean bool) {
        this.checked = bool;
    }
 
    @Override
    public void setOpen(boolean bool) {
        this.opened = bool;
    }
 
    @Override
    public void setParentId(String parentId) {
        Assert.hasText(parentId);
        this.parentId = parentId;
    }
 
    @Override
    public String getIcon() {
        return icon;
    }
 
    @Override
    public void setIcon(String icon) {
        this.icon = icon;
    }
 
    @Override
    public CacheTreeNode copy() {
        CacheTreeNode newObject = new DefaultCacheTreeNode(key, text, source, parentId);
        newObject.setIcon(icon);
        return newObject.setOrder(this.order);
    }
}