-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from little3201/develop
更新版本0.1.2,解决反射使用的错误操作,bug修复
- Loading branch information
Showing
11 changed files
with
274 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/top/leafage/common/reactive/ReactiveTreeNodeAware.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
80
src/main/java/top/leafage/common/servlet/ServletTreeNodeAware.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.