Skip to content

Commit

Permalink
Merge pull request #18 from little3201/develop
Browse files Browse the repository at this point in the history
更新版本0.1.2,解决反射使用的错误操作,bug修复
  • Loading branch information
little3201 authored Jul 5, 2021
2 parents b7faff6 + d7f59a4 commit df94de9
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Leafage Common

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=little3201_leafage-common&metric=alert_status)](https://sonarcloud.io/dashboard?id=little3201_leafage-common)

### 介绍:

leafage 开源项目的公共模块,提供通用工具和抽象接口;
Expand Down
10 changes: 2 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>top.leafage</groupId>
<artifactId>leafage-starter-parent</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<artifactId>leafage-common</artifactId>
<version>0.1.0</version>
<version>0.1.2</version>
<packaging>jar</packaging>

<name>leafage-common</name>
Expand All @@ -21,12 +21,6 @@
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- reactor -->
<dependency>
<groupId>io.projectreactor</groupId>
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/top/leafage/common/basic/TreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package top.leafage.common.basic;

import java.util.List;
import java.util.Map;

public class TreeNode {

/**
* 代码
*/
private String code;
/**
* 名称
*/
private String name;
/**
* 上级
*/
private String superior;
/**
* 扩展属性
*/
private Map<String, String> expand;

private List<TreeNode> children;

public TreeNode(String code, String name) {
this.code = code;
this.name = name;
}

public String getCode() {
return code;
}

public String getName() {
return name;
}

public String getSuperior() {
return superior;
}

public Map<String, String> getExpand() {
return expand;
}

public List<TreeNode> getChildren() {
return children;
}

public void setCode(String code) {
this.code = code;
}

public void setName(String name) {
this.name = name;
}

public void setSuperior(String superior) {
this.superior = superior;
}

public void setExpand(Map<String, String> expand) {
this.expand = expand;
}

public void setChildren(List<TreeNode> children) {
this.children = children;
}

}
33 changes: 33 additions & 0 deletions src/main/java/top/leafage/common/basic/TreeNodeAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package top.leafage.common.basic;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public interface TreeNodeAware<T> {

/**
* 处理扩展数据
*
* @param treeNode 当前节点
* @param clazz 数据类型
* @param t 数据实例
* @param expand 扩展字段
*/
default void expand(TreeNode treeNode, Class<?> clazz, T t, Set<String> expand) {
if (expand != null && !expand.isEmpty()) {
Map<String, String> map = new HashMap<>(expand.size());
expand.forEach(filed -> {
try {
String methodSuffix = filed.substring(0, 1).toUpperCase() + filed.substring(1);
Object value = clazz.getMethod("get" + methodSuffix).invoke(t);
map.put(filed, value != null ? value.toString() : null);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
});
treeNode.setExpand(map);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/top/leafage/common/package-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* package-info
* common interfaces or methods with webmvc and webflux
*
* @author liwenqiang 2019/10/23 9:51
**/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

public interface ReactiveBasicService<D, V> {
Expand Down Expand Up @@ -62,6 +61,7 @@ default Mono<Long> count() {
* 根据code删除entity
*
* @param code 代码
* @return Void no return
*/
default Mono<Void> remove(String code) {
return Mono.empty().then();
Expand All @@ -71,6 +71,7 @@ default Mono<Void> remove(String code) {
* 批量删除
*
* @param dList 实例集合
* @return Void no return
*/
default Mono<Void> removeAll(List<D> dList) {
return Mono.empty().then();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package top.leafage.common.reactive;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import top.leafage.common.basic.TreeNode;
import top.leafage.common.basic.TreeNodeAware;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;

public interface ReactiveTreeNodeAware<T> extends TreeNodeAware<T> {

/**
* 处理子节点
*
* @param superior 上级数据
* @param children 子节点
* @return 树节点数据集
*/
default Flux<TreeNode> children(T superior, Flux<T> children) {
return this.children(superior, children, null);
}

/**
* 处理子节点
*
* @param superior 上级数据
* @param children 子节点
* @param expand 扩展属性
* @return 树节点数据集
*/
default Flux<TreeNode> children(T superior, Flux<T> children, Set<String> expand) {
Class<?> aClass = superior.getClass();
try {
Object superiorId = aClass.getSuperclass().getMethod("getId").invoke(superior);
Object superiorName = aClass.getMethod("getName").invoke(superior);

return children.filter(child -> this.check(superiorId, child)).flatMap(child -> {
Class<?> childClass = child.getClass();
try {
String code = childClass.getMethod("getCode").invoke(child).toString();
Object name = childClass.getMethod("getName").invoke(child);

TreeNode treeNode = new TreeNode(code, name != null ? name.toString() : null);
treeNode.setSuperior(superiorName != null ? superiorName.toString() : null);

this.children(child, children, expand).collectList().map(treeNodes -> {
treeNode.setChildren(treeNodes);
return treeNode;
});

// deal expand
this.expand(treeNode, childClass, child, expand);

return Mono.just(treeNode);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return Mono.empty();
});
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return Flux.empty();
}

/**
* 检查是否上下级节点
*
* @param superiorId 上级节点ID
* @param child 对象实例
* @return true-是,false-否
*/
default boolean check(Object superiorId, T child) {
Class<?> childClass = child.getClass();
try {
Object superior = childClass.getMethod("getSuperior").invoke(child);
return superiorId.equals(superior);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
return false;
}
}
}
80 changes: 80 additions & 0 deletions src/main/java/top/leafage/common/servlet/ServletTreeNodeAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package top.leafage.common.servlet;

import top.leafage.common.basic.TreeNode;
import top.leafage.common.basic.TreeNodeAware;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public interface ServletTreeNodeAware<T> extends TreeNodeAware<T> {

/**
* 处理子节点
*
* @param superior 上级数据
* @param children 子节点
* @return 树节点数据集
*/
default List<TreeNode> children(T superior, List<T> children) {
return this.children(superior, children, null);
}

/**
* 处理子节点
*
* @param superior 上级数据
* @param children 子节点
* @param expand 扩展属性
* @return 树节点数据集
*/
default List<TreeNode> children(T superior, List<T> children, Set<String> expand) {
Class<?> aClass = superior.getClass();
try {
Long superiorId = (Long) aClass.getSuperclass().getMethod("getId").invoke(superior);
String superiorName = aClass.getMethod("getName").invoke(superior).toString();

return children.stream().filter(child -> this.check(superiorId, child))
.map(child -> {
Class<?> childClass = child.getClass();
try {
String name = childClass.getMethod("getName").invoke(child).toString();
String code = childClass.getMethod("getCode").invoke(child).toString();

TreeNode servletTreeNode = new TreeNode(code, name);
servletTreeNode.setSuperior(superiorName);
servletTreeNode.setChildren(this.children(child, children));

// deal expand
this.expand(servletTreeNode, childClass, child, expand);

return servletTreeNode;
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return Collections.emptyList();
}

/**
* 检查是否上下级节点
*
* @param superiorId 上级节点ID
* @param child 对象实例
* @return true-是,false-否
*/
default boolean check(Long superiorId, T child) {
Class<?> childClass = child.getClass();
try {
Long superior = (Long) childClass.getMethod("getSuperior").invoke(child);
return superiorId.equals(superior);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
return false;
}
}
}
6 changes: 0 additions & 6 deletions src/main/resources/META-INF/MANIFEST.MF

This file was deleted.

2 changes: 0 additions & 2 deletions src/main/resources/META-INF/spring.factories

This file was deleted.

1 change: 0 additions & 1 deletion src/main/resources/META-INFO/spring.factories

This file was deleted.

0 comments on commit df94de9

Please sign in to comment.