Skip to content

Commit 7ab6177

Browse files
authored
Merge pull request #31 from little3201/develop
TreeNode改为Builder模式;
2 parents 618668e + ac09a0a commit 7ab6177

14 files changed

+802
-187
lines changed

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@
3838
<groupId>org.apache.poi</groupId>
3939
<artifactId>poi-ooxml</artifactId>
4040
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.data</groupId>
43+
<artifactId>spring-data-jpa</artifactId>
44+
<optional>true</optional>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.data</groupId>
48+
<artifactId>spring-data-relational</artifactId>
49+
<optional>true</optional>
50+
</dependency>
51+
<dependency>
52+
<groupId>jakarta.persistence</groupId>
53+
<artifactId>jakarta.persistence-api</artifactId>
54+
<optional>true</optional>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.security</groupId>
58+
<artifactId>spring-security-core</artifactId>
59+
<optional>true</optional>
60+
</dependency>
4161
<!-- reactor -->
4262
<dependency>
4363
<groupId>io.projectreactor</groupId>

src/main/java/top/leafage/common/AbstractTreeNodeService.java

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
import java.util.stream.Collectors;
2828

2929
/**
30-
* Abstract service for constructing tree nodes from objects.
30+
* Abstract base service for constructing tree structures from objects.
31+
* Provides functionality for creating tree nodes and organizing them
32+
* based on superior-subordinate relationships.
3133
*
32-
* @param <T> the type of object representing a node
33-
* @author wq li
34+
* @param <T> the type of object representing a node in the tree
3435
* @since 0.1.3
3536
*/
3637
public abstract class AbstractTreeNodeService<T> {
@@ -42,76 +43,60 @@ public abstract class AbstractTreeNodeService<T> {
4243
private static final Logger log = StatusLogger.getLogger();
4344

4445
/**
45-
* Constructs a tree node from an object, optionally expanding additional properties.
46+
* Creates a tree node from the given object, using the provided property names to expand additional data.
47+
* This method extracts ID, name, and superior ID from the object and attaches any expanded properties.
4648
*
47-
* @param t the object representing the node
48-
* @param expand a set of property names to expand on the node
49-
* @return a constructed TreeNode
50-
* @since 0.2.0
49+
* @param t the object representing a node
50+
* @param expand a set of property names to be expanded into the node's additional properties
51+
* @return a fully constructed TreeNode instance
52+
* @throws IllegalArgumentException if the ID property is null
53+
* @since 0.3.0
5154
*/
52-
protected TreeNode node(T t, Set<String> expand) {
55+
protected TreeNode createNode(T t, Set<String> expand) {
5356
Class<?> aClass = t.getClass();
5457
Object id = this.getId(t, aClass.getSuperclass());
58+
if (Objects.isNull(id)) {
59+
throw new IllegalArgumentException("ID cannot be null");
60+
}
5561
Object name = this.getName(t, aClass);
5662
Object superiorId = this.getSuperiorId(t, aClass);
5763

58-
TreeNode treeNode = new TreeNode(Objects.nonNull(id) ? (Long) id : null,
59-
Objects.nonNull(name) ? String.valueOf(name) : null);
60-
treeNode.setSuperior(Objects.nonNull(superiorId) ? (Long) superiorId : null);
61-
62-
this.expand(treeNode, aClass, t, expand);
63-
return treeNode;
64+
return TreeNode.withId((Long) id)
65+
.name(Objects.nonNull(name) ? String.valueOf(name) : null)
66+
.superiorId(Objects.nonNull(superiorId) ? (Long) superiorId : null)
67+
.meta(meta(aClass, t, expand)).build();
6468
}
6569

6670
/**
67-
* Sets the children for tree nodes based on their superior IDs.
71+
* Organizes the tree nodes by assigning children to their respective parents based on the superior ID.
72+
* Returns a list of root nodes (nodes that do not have a superior).
6873
*
69-
* @param treeNodes the list of tree nodes
70-
* @return a list of root nodes (nodes without a superior)
74+
* @param treeNodes the list of all nodes to organize
75+
* @return a list of root nodes, each containing its child nodes
7176
* @since 0.2.0
7277
*/
7378
protected List<TreeNode> children(List<TreeNode> treeNodes) {
7479
Map<Long, List<TreeNode>> nodesMap = treeNodes.stream()
75-
.filter(node -> Objects.nonNull(node.getSuperior()) && node.getSuperior() != 0)
76-
.collect(Collectors.groupingBy(TreeNode::getSuperior));
77-
78-
treeNodes.forEach(node -> node.setChildren(nodesMap.get(node.getId())));
80+
.filter(node -> Objects.nonNull(node.getSuperiorId()) && node.getSuperiorId() != 0)
81+
.collect(Collectors.groupingBy(TreeNode::getSuperiorId));
7982

8083
return treeNodes.stream()
81-
.filter(node -> Objects.isNull(node.getSuperior()) || node.getSuperior() == 0)
84+
.map(treeNode -> TreeNode.withId(treeNode.getId())
85+
.name(treeNode.getName())
86+
.superiorId(treeNode.getSuperiorId())
87+
.children(nodesMap.get(treeNode.getId()))
88+
.meta(treeNode.getMeta())
89+
.build())
90+
.filter(node -> Objects.isNull(node.getSuperiorId()))
8291
.collect(Collectors.toList());
8392
}
8493

8594
/**
86-
* Expands additional properties for a TreeNode.
87-
*
88-
* @param treeNode the TreeNode to expand
89-
* @param clazz the class of the object
90-
* @param t the object representing the node
91-
* @param expand a set of property names to expand
92-
*/
93-
private void expand(TreeNode treeNode, Class<?> clazz, T t, Set<String> expand) {
94-
if (expand != null && !expand.isEmpty()) {
95-
Map<String, Object> expandedData = new HashMap<>(expand.size());
96-
try {
97-
for (String field : expand) {
98-
PropertyDescriptor descriptor = new PropertyDescriptor(field, clazz);
99-
Object value = descriptor.getReadMethod().invoke(t);
100-
expandedData.put(field, value);
101-
}
102-
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
103-
log.error("Error expanding data for TreeNode.", e);
104-
}
105-
treeNode.setExpand(expandedData);
106-
}
107-
}
108-
109-
/**
110-
* Retrieves the ID from the object.
95+
* Retrieves the ID value from the given object using reflection.
11196
*
11297
* @param obj the object instance
113-
* @param clazz the class of the object
114-
* @return the ID value, or null if an error occurs
98+
* @param clazz the class of the object or its superclass
99+
* @return the ID value, or null if an error occurs during reflection
115100
*/
116101
private Object getId(Object obj, Class<?> clazz) {
117102
try {
@@ -124,11 +109,11 @@ private Object getId(Object obj, Class<?> clazz) {
124109
}
125110

126111
/**
127-
* Retrieves the name from the object.
112+
* Retrieves the name value from the given object using reflection.
128113
*
129114
* @param t the object instance
130115
* @param clazz the class of the object
131-
* @return the name value, or null if an error occurs
116+
* @return the name value, or null if an error occurs during reflection
132117
*/
133118
private Object getName(T t, Class<?> clazz) {
134119
try {
@@ -141,11 +126,11 @@ private Object getName(T t, Class<?> clazz) {
141126
}
142127

143128
/**
144-
* Retrieves the superior ID from the object.
129+
* Retrieves the superior ID value from the given object using reflection.
145130
*
146131
* @param t the object instance
147132
* @param clazz the class of the object
148-
* @return the superior ID value, or null if an error occurs
133+
* @return the superior ID value, or null if an error occurs during reflection
149134
*/
150135
private Object getSuperiorId(T t, Class<?> clazz) {
151136
try {
@@ -156,5 +141,32 @@ private Object getSuperiorId(T t, Class<?> clazz) {
156141
return null;
157142
}
158143
}
144+
145+
/**
146+
* Expands additional properties for the TreeNode by reflecting on the object's class and property names.
147+
*
148+
* @param clazz the class of the object
149+
* @param t the object representing the node
150+
* @param expand a set of property names to expand as additional data
151+
* @return a map containing the expanded properties
152+
* @since 0.3.0
153+
*/
154+
private Map<String, Object> meta(Class<?> clazz, T t, Set<String> expand) {
155+
Map<String, Object> expandedData = Collections.emptyMap();
156+
if (expand != null && !expand.isEmpty()) {
157+
expandedData = new HashMap<>(expand.size());
158+
try {
159+
for (String field : expand) {
160+
PropertyDescriptor descriptor = new PropertyDescriptor(field, clazz);
161+
Object value = descriptor.getReadMethod().invoke(t);
162+
expandedData.put(field, value);
163+
}
164+
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
165+
log.error("Error expanding data for TreeNode.", e);
166+
}
167+
}
168+
return expandedData;
169+
}
159170
}
160171

172+

0 commit comments

Comments
 (0)