Commit da6ee3ea authored by liaozan's avatar liaozan 🏀

Update pom.xml

parent e1bdb729
...@@ -51,34 +51,19 @@ public class TreeUtils { ...@@ -51,34 +51,19 @@ public class TreeUtils {
return doBuildTree(keyExtractor, childrenSetter, childMapper, parentWithSubNodes, childrenComparator, parentId); return doBuildTree(keyExtractor, childrenSetter, childMapper, parentWithSubNodes, childrenComparator, parentId);
} }
private static <T, K, C extends Collection<T>> Map<K, List<T>> buildParentWithSubNodes(C nodes,
Function<T, K> parentKeyExtractor,
@Nullable K parentId) {
Map<K, List<T>> parentWithSubNodes = StreamUtils.groupBy(nodes, parentKeyExtractor, true);
if (parentId == null) {
// groupBy 不允许 key 为空,当 parentId 为空时,单独处理下
List<T> subNodes = StreamUtils.filterToList(nodes, node -> parentKeyExtractor.apply(node) == null);
parentWithSubNodes.put(null, subNodes);
}
return parentWithSubNodes;
}
/**
* @param preBrotherFlag true: previousId false:nextId
*/
public static <T, K, C extends Collection<T>> List<T> buildTree(C nodes, public static <T, K, C extends Collection<T>> List<T> buildTree(C nodes,
Function<T, K> keyExtractor, Function<T, K> keyExtractor,
Function<T, K> parentKeyExtractor, Function<T, K> parentKeyExtractor,
Function<T, K> brotherKeyExtractor, Function<T, K> brotherKeyExtractor,
BiConsumer<T, List<T>> childrenSetter, BiConsumer<T, List<T>> childrenSetter,
@Nullable K parentId, @Nullable K rootBrotherId, @Nullable K parentId,
@Nullable K rootBrotherId,
boolean preBrotherFlag) { boolean preBrotherFlag) {
if (CollectionUtils.isEmpty(nodes)) { if (CollectionUtils.isEmpty(nodes)) {
return new ArrayList<>(); return new ArrayList<>();
} }
Map<K, List<T>> parentWithSubNodes = buildParentWithSubNodes(nodes, parentKeyExtractor, parentId); Map<K, List<T>> parentWithSubNodes = buildParentWithSubNodes(nodes, parentKeyExtractor, parentId);
return doBuildTree(keyExtractor, brotherKeyExtractor, childrenSetter, parentWithSubNodes, parentId, return doBuildTree(keyExtractor, brotherKeyExtractor, childrenSetter, parentWithSubNodes, parentId, rootBrotherId, preBrotherFlag);
rootBrotherId, preBrotherFlag);
} }
public static <T, E> List<E> getParents(E self, 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) {
...@@ -107,6 +92,16 @@ public class TreeUtils { ...@@ -107,6 +92,16 @@ public class TreeUtils {
return nodes; return nodes;
} }
private static <T, K, C extends Collection<T>> Map<K, List<T>> buildParentWithSubNodes(C nodes, Function<T, K> parentKeyExtractor, @Nullable K parentId) {
Map<K, List<T>> parentWithSubNodes = StreamUtils.groupBy(nodes, parentKeyExtractor, true);
if (parentId == null) {
// groupBy 不允许 key 为空,当 parentId 为空时,单独处理下
List<T> subNodes = StreamUtils.filterToList(nodes, node -> parentKeyExtractor.apply(node) == null);
parentWithSubNodes.put(null, subNodes);
}
return parentWithSubNodes;
}
private static <E, T> void doBuildNodeList(Collection<T> tree, Function<T, Collection<T>> childGetter, Function<T, E> mapper, List<E> nodes) { private static <E, T> void doBuildNodeList(Collection<T> tree, Function<T, Collection<T>> childGetter, Function<T, E> mapper, List<E> nodes) {
if (CollectionUtils.isEmpty(tree)) { if (CollectionUtils.isEmpty(tree)) {
return; return;
...@@ -144,32 +139,31 @@ public class TreeUtils { ...@@ -144,32 +139,31 @@ public class TreeUtils {
return treeList; return treeList;
} }
private static <E> void sort(List<E> data, Comparator<E> comparator) {
if (comparator != null && CollectionUtils.isNotEmpty(data)) {
ListUtil.sort(data, comparator);
}
}
private static <K, T> List<T> doBuildTree(Function<T, K> keyExtractor, private static <K, T> List<T> doBuildTree(Function<T, K> keyExtractor,
Function<T, K> brotherKeyExtractor, Function<T, K> brotherKeyExtractor,
BiConsumer<T, List<T>> childrenSetter, BiConsumer<T, List<T>> childrenSetter,
Map<K, List<T>> parentWithSubNodes, Map<K, List<T>> parentWithSubNodes,
K parentId, K rootBrotherId, boolean preBrotherFlag) { K parentId, K rootBrotherId,
boolean preBrotherFlag) {
List<T> subNodes = parentWithSubNodes.remove(parentId); List<T> subNodes = parentWithSubNodes.remove(parentId);
if (CollectionUtils.isEmpty(subNodes)) { if (CollectionUtils.isEmpty(subNodes)) {
return Collections.emptyList(); return Collections.emptyList();
} }
subNodes.forEach(subNode -> { subNodes.forEach(subNode -> {
List<T> children = doBuildTree(keyExtractor, brotherKeyExtractor, childrenSetter, List<T> children = doBuildTree(keyExtractor, brotherKeyExtractor, childrenSetter, parentWithSubNodes, keyExtractor.apply(subNode), rootBrotherId, preBrotherFlag);
parentWithSubNodes, keyExtractor.apply(subNode), rootBrotherId, preBrotherFlag);
childrenSetter.accept(subNode, children); childrenSetter.accept(subNode, children);
}); });
sort(subNodes, keyExtractor, brotherKeyExtractor, rootBrotherId, preBrotherFlag); sort(subNodes, keyExtractor, brotherKeyExtractor, rootBrotherId, preBrotherFlag);
return subNodes; return subNodes;
} }
private static <K, T> void sort(List<T> dataList, Function<T, K> keyExtractor, Function<T, K> brotherKeyExtractor, private static <E> void sort(List<E> data, Comparator<E> comparator) {
K rootBrotherId, boolean preBrotherFlag) { if (comparator != null && CollectionUtils.isNotEmpty(data)) {
ListUtil.sort(data, comparator);
}
}
private static <K, T> void sort(List<T> dataList, Function<T, K> keyExtractor, Function<T, K> brotherKeyExtractor, K rootBrotherId, boolean preBrotherFlag) {
if (CollectionUtils.isEmpty(dataList)) { if (CollectionUtils.isEmpty(dataList)) {
return; return;
} }
...@@ -185,11 +179,10 @@ public class TreeUtils { ...@@ -185,11 +179,10 @@ public class TreeUtils {
break; break;
} }
} }
//避免脏数据,则直接返回原列表 // 避免脏数据,则直接返回原列表
if (sortList.size() != dataList.size()) { if (sortList.size() != dataList.size()) {
System.out.println(JacksonUtils.toJsonString(sortList));
log.warn("Sort Warning:{}", JacksonUtils.toJsonString(dataList)); log.warn("Sort Warning:{}", JacksonUtils.toJsonString(dataList));
return ; return;
} }
if (!preBrotherFlag) { if (!preBrotherFlag) {
Collections.reverse(sortList); Collections.reverse(sortList);
......
...@@ -705,13 +705,6 @@ ...@@ -705,13 +705,6 @@
</plugins> </plugins>
</build> </build>
<profiles>
<!-- 公司环境基本配置 -->
<profile>
<id>schbrain</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories> <repositories>
<repository> <repository>
<id>libs-public</id> <id>libs-public</id>
...@@ -739,7 +732,8 @@ ...@@ -739,7 +732,8 @@
</snapshots> </snapshots>
</pluginRepository> </pluginRepository>
</pluginRepositories> </pluginRepositories>
</profile>
<profiles>
<!-- 开发环境 --> <!-- 开发环境 -->
<profile> <profile>
<id>dev</id> <id>dev</id>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment