Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
schbrain-parent
Commits
7e3fec07
Commit
7e3fec07
authored
Aug 31, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update TreeUtils
parent
9225626f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
32 deletions
+32
-32
commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java
...til/src/main/java/com/schbrain/common/util/TreeUtils.java
+32
-32
No files found.
commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java
View file @
7e3fec07
package
com.schbrain.common.util
;
import
cn.hutool.core.collection.
Collection
Util
;
import
cn.hutool.core.collection.
List
Util
;
import
com.google.common.collect.Maps
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.MapUtils
;
...
...
@@ -11,37 +11,37 @@ import java.util.function.BiConsumer;
import
java.util.function.Function
;
/**
* Utils
to build
tree
* Utils
for
tree
*
* @author panwangnan
* @since 2023/7/29
*/
public
class
TreeUtils
{
public
static
<
T
,
K
>
List
<
T
>
buildTree
(
Collection
<
T
>
nodes
,
Function
<
T
,
K
>
keyExtractor
,
Function
<
T
,
K
>
parentKeyExtractor
,
BiConsumer
<
T
,
Collection
<
T
>>
childrenSetter
,
@Nullable
K
parentId
)
{
public
static
<
T
,
K
,
C
extends
Collection
<
T
>>
List
<
T
>
buildTree
(
C
nodes
,
Function
<
T
,
K
>
keyExtractor
,
Function
<
T
,
K
>
parentKeyExtractor
,
BiConsumer
<
T
,
List
<
T
>>
childrenSetter
,
@Nullable
K
parentId
)
{
return
buildTree
(
nodes
,
keyExtractor
,
parentKeyExtractor
,
Function
.
identity
(),
childrenSetter
,
parentId
);
}
public
static
<
T
,
K
,
E
>
List
<
E
>
buildTree
(
Collection
<
T
>
nodes
,
Function
<
T
,
K
>
keyExtractor
,
Function
<
T
,
K
>
parentKeyExtractor
,
Function
<
T
,
E
>
childMapper
,
BiConsumer
<
E
,
Collection
<
E
>>
childrenSetter
,
@Nullable
K
parentId
)
{
public
static
<
T
,
K
,
E
,
C
extends
Collection
<
T
>>
List
<
E
>
buildTree
(
C
nodes
,
Function
<
T
,
K
>
keyExtractor
,
Function
<
T
,
K
>
parentKeyExtractor
,
Function
<
T
,
E
>
childMapper
,
BiConsumer
<
E
,
List
<
E
>>
childrenSetter
,
@Nullable
K
parentId
)
{
return
buildTree
(
nodes
,
keyExtractor
,
parentKeyExtractor
,
childMapper
,
childrenSetter
,
null
,
parentId
);
}
public
static
<
T
,
K
,
E
>
List
<
E
>
buildTree
(
Collection
<
T
>
nodes
,
Function
<
T
,
K
>
keyExtractor
,
Function
<
T
,
K
>
parentKeyExtractor
,
Function
<
T
,
E
>
childMapper
,
BiConsumer
<
E
,
Collection
<
E
>>
childrenSetter
,
@Nullable
Comparator
<
E
>
childrenComparator
,
@Nullable
K
parentId
)
{
public
static
<
T
,
K
,
E
,
C
extends
Collection
<
T
>>
List
<
E
>
buildTree
(
C
nodes
,
Function
<
T
,
K
>
keyExtractor
,
Function
<
T
,
K
>
parentKeyExtractor
,
Function
<
T
,
E
>
childMapper
,
BiConsumer
<
E
,
List
<
E
>>
childrenSetter
,
@Nullable
Comparator
<
E
>
childrenComparator
,
@Nullable
K
parentId
)
{
if
(
CollectionUtils
.
isEmpty
(
nodes
))
{
return
new
ArrayList
<>();
}
...
...
@@ -54,24 +54,24 @@ public class TreeUtils {
return
doBuildTree
(
keyExtractor
,
childrenSetter
,
childMapper
,
parentWithSubNodes
,
childrenComparator
,
parentId
);
}
public
static
<
T
,
E
>
List
<
E
>
getParents
(
E
id
,
Collection
<
T
>
nodes
,
Function
<
T
,
E
>
keyMapper
,
Function
<
T
,
E
>
parentMapper
,
boolean
includeSelf
)
{
public
static
<
T
,
E
>
List
<
E
>
getParents
(
E
self
,
Collection
<
T
>
nodes
,
Function
<
T
,
E
>
keyMapper
,
Function
<
T
,
E
>
parentMapper
,
boolean
includeSelf
)
{
// toMap 不允许 value 为空,当 parentId 为空时,单独处理下
Map
<
E
,
E
>
parentMap
=
Maps
.
newHashMapWithExpectedSize
(
nodes
.
size
());
for
(
T
node
:
nodes
)
{
parentMap
.
put
(
keyMapper
.
apply
(
node
),
parentMapper
.
apply
(
node
));
}
return
getParents
(
id
,
parentMap
,
includeSelf
);
return
getParents
(
self
,
parentMap
,
includeSelf
);
}
public
static
<
T
>
List
<
T
>
getParents
(
T
id
,
Map
<
T
,
T
>
parentMap
,
boolean
includeSelf
)
{
public
static
<
T
>
List
<
T
>
getParents
(
T
self
,
Map
<
T
,
T
>
parentMap
,
boolean
includeSelf
)
{
List
<
T
>
parentIds
=
new
LinkedList
<>();
if
(
includeSelf
)
{
parentIds
.
add
(
id
);
parentIds
.
add
(
self
);
}
if
(
MapUtils
.
isEmpty
(
parentMap
))
{
return
parentIds
;
}
return
getParents
(
id
,
parentMap
,
parentIds
);
return
getParents
(
self
,
parentMap
,
parentIds
);
}
public
static
<
T
,
E
>
List
<
E
>
buildNodeList
(
Collection
<
T
>
tree
,
Function
<
T
,
Collection
<
T
>>
childGetter
,
Function
<
T
,
E
>
mapper
)
{
...
...
@@ -90,8 +90,8 @@ public class TreeUtils {
});
}
private
static
<
T
>
List
<
T
>
getParents
(
T
id
,
Map
<
T
,
T
>
parentMap
,
List
<
T
>
parentIds
)
{
T
parentId
=
parentMap
.
get
(
id
);
private
static
<
T
>
List
<
T
>
getParents
(
T
self
,
Map
<
T
,
T
>
parentMap
,
List
<
T
>
parentIds
)
{
T
parentId
=
parentMap
.
get
(
self
);
if
(
parentId
==
null
)
{
return
parentIds
;
}
...
...
@@ -100,15 +100,15 @@ public class TreeUtils {
}
private
static
<
E
,
K
,
T
>
List
<
E
>
doBuildTree
(
Function
<
T
,
K
>
keyExtractor
,
BiConsumer
<
E
,
Collection
<
E
>>
childrenSetter
,
BiConsumer
<
E
,
List
<
E
>>
childrenSetter
,
Function
<
T
,
E
>
childMapper
,
Map
<
K
,
List
<
T
>>
parentWithSubNodes
,
Comparator
<
E
>
childrenComparator
,
K
parentId
)
{
Collection
<
T
>
subNodes
=
parentWithSubNodes
.
remove
(
parentId
);
List
<
T
>
subNodes
=
parentWithSubNodes
.
remove
(
parentId
);
List
<
E
>
treeList
=
StreamUtils
.
toList
(
subNodes
,
subNode
->
{
E
child
=
childMapper
.
apply
(
subNode
);
Collection
<
E
>
children
=
doBuildTree
(
keyExtractor
,
childrenSetter
,
childMapper
,
parentWithSubNodes
,
childrenComparator
,
keyExtractor
.
apply
(
subNode
));
List
<
E
>
children
=
doBuildTree
(
keyExtractor
,
childrenSetter
,
childMapper
,
parentWithSubNodes
,
childrenComparator
,
keyExtractor
.
apply
(
subNode
));
sort
(
children
,
childrenComparator
);
childrenSetter
.
accept
(
child
,
children
);
return
child
;
...
...
@@ -117,9 +117,9 @@ public class TreeUtils {
return
treeList
;
}
private
static
<
E
>
void
sort
(
Collection
<
E
>
data
,
Comparator
<
E
>
comparator
)
{
private
static
<
E
>
void
sort
(
List
<
E
>
data
,
Comparator
<
E
>
comparator
)
{
if
(
comparator
!=
null
&&
CollectionUtils
.
isNotEmpty
(
data
))
{
Collection
Util
.
sort
(
data
,
comparator
);
List
Util
.
sort
(
data
,
comparator
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment